1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::ops::Not;
use crate::colour::Colour;
pub type GradientStop = (f64, Colour);
pub struct Gradient(pub Vec<GradientStop>);
impl Gradient {
/// Inserts (t: f64, colour: Colour) in the region that `t` resides
/// if `t` exists, this will replace the colour.
pub fn insert(&mut self, t: f64, colour: Colour) {
if let Some(index) = self
.0
.iter()
.enumerate()
.skip_while(|(_, (v, _))| *v < t)
.next()
.map(|(i, (_, _))| i)
{
if self.0[index].0 == t {
self.0[index].1 = colour;
} else {
self.0.insert(index, (t, colour));
}
} else {
self.0.push((t, colour));
};
}
/// Returns the two gradient stops that `t` resides between.
///
/// Much like a quadratic solution, if `t` resides before the first
/// gradient stop, that gradient stop will be returned twice
/// and similarly with the last gradient stop. Interpolation between
/// these, no matter the `t` value and it's validity, will always return
/// the colour of that gradient stop.
///
/// # Example
///
/// ```
/// use tcolour::{Gradient, Colour};
/// let gradient = Gradient(vec![
/// (0.5, Colour::solid(1.0, 0.0, 0.0)),
/// (0.7, Colour::solid(0.0, 1.0, 0.0)),
/// (0.8, Colour::solid(0.0, 0.0, 1.0)),
/// ]);
///
/// assert_eq!(gradient.subgradient(0.1), ((0.5, Colour::red(1.0)), (0.5, Colour::red(1.0))));
/// assert_eq!(gradient.subgradient(0.6), ((0.5, Colour::red(1.0)), (0.7, Colour::green(1.0))));
/// assert_eq!(gradient.subgradient(0.9), ((0.8, Colour::blue(1.0)), (0.8, Colour::blue(1.0))));
/// ```
pub fn subgradient(&self, t: f64) -> (GradientStop, GradientStop) {
self.0
.iter()
.scan(None, |prev, &curr| {
let old = prev.take();
*prev = Some(curr);
if curr.0 > t {
Some(Some((old.unwrap_or(curr), curr)))
} else {
Some(None)
}
})
.find_map(|x| x)
.or_else(|| self.0.last().map(|&last| (last, last)))
.unwrap()
}
/// Gets a colour from the gradient using
/// linear interpolation. Use `.interpolate()`
/// for your own interpolation function and
/// use `.pick()`` to just get the colour that
/// `t` lands on. The alpha value is also
/// interpolated.
///
/// # Example
///
/// ```
/// use approx::assert_relative_eq;
/// use tcolour::{Gradient, Colour};
/// let gradient = Gradient(vec![
/// (0.5, Colour::solid(1.0, 0.0, 0.0)),
/// (0.7, Colour::solid(0.0, 1.0, 0.0)),
/// (0.8, Colour::solid(0.0, 0.0, 1.0)),
/// ]);
///
/// assert_eq!(gradient.get(0.6), Colour::solid(0.5, 0.5, 0.0));
/// assert_relative_eq!(gradient.get(0.65), Colour::solid(0.25, 0.75, 0.0));
/// assert_eq!(gradient.get(0.9), Colour::solid(0.0, 0.0, 1.0));
/// ```
pub fn get(&self, t: f64) -> Colour {
self.interpolate(t, |from, to, t| {
(from + (to - from) * t).with_alpha(from.a + (to.a - from.a) * t)
})
}
/// Gets a colour from the gradient by finding
/// the region that contains `t` and then interpolating
/// using the function that is given.
///
/// `t` is normalised between `t_from` and `t_to` by
/// `t = (t - t_from)/(t_to - t_from)` such that
/// with `t = 0.7`, `t_from = 0.6` and `t_to = 0.8`
/// we actually interpolate between `[0, 1]` with `t = 0.5`
pub fn interpolate<F: FnOnce(Colour, Colour, f64) -> Colour>(
&self,
t: f64,
interpolator: F,
) -> Colour {
let ((t_from, from), (t_to, to)) = self.subgradient(t);
let normalised_t = (t - t_from) / (t_to - t_from);
interpolator(
from,
to,
normalised_t.is_normal().not().then_some(1f64).unwrap_or(normalised_t)
)
}
/// Select the lower bound colour, use `.select_upper()` for the upper
/// bound colour
pub fn select(&self, t: f64) -> Colour {
self.subgradient(t).0.1
}
/// Select the upper bound colour, use `.select()` for the lower bound
/// colour
pub fn select_upper(&self, t: f64) -> Colour {
self.subgradient(t).1.1
}
}
#[cfg(test)]
mod test {
use approx::assert_relative_eq;
use super::Gradient;
use crate::Colour;
#[test]
pub fn subgradient_test() {
let gradient = Gradient(vec![
(0.5, Colour::solid(1.0, 0.0, 0.0)),
(0.7, Colour::solid(0.0, 1.0, 0.0)),
(0.8, Colour::solid(0.0, 0.0, 1.0)),
]);
assert_eq!(
gradient.subgradient(0.1),
(
(0.5, Colour::solid(1.0, 0.0, 0.0)),
(0.5, Colour::solid(1.0, 0.0, 0.0))
)
);
assert_eq!(
gradient.subgradient(0.6),
(
(0.5, Colour::solid(1.0, 0.0, 0.0)),
(0.7, Colour::solid(0.0, 1.0, 0.0))
)
);
assert_eq!(
gradient.subgradient(0.75),
(
(0.7, Colour::solid(0.0, 1.0, 0.0)),
(0.8, Colour::solid(0.0, 0.0, 1.0))
)
);
assert_eq!(
gradient.subgradient(0.9),
(
(0.8, Colour::solid(0.0, 0.0, 1.0)),
(0.8, Colour::solid(0.0, 0.0, 1.0))
)
);
}
#[test]
pub fn insertion_test() {
let mut gradient = Gradient(vec![
(0.5, Colour::solid(1.0, 0.0, 0.0)),
(0.7, Colour::solid(0.0, 1.0, 0.0)),
(0.8, Colour::solid(0.0, 0.0, 1.0)),
]);
gradient.insert(0.3, Colour::grey(0.5));
assert_eq!(gradient.0.first().unwrap().1, Colour::grey(0.5));
gradient.insert(0.9, Colour::grey(0.2));
assert_eq!(gradient.0.last().unwrap().1, Colour::grey(0.2));
gradient.insert(0.6, Colour::red(0.8));
assert_eq!(gradient.0[2].1, Colour::red(0.8));
gradient.insert(0.85, Colour::transparent());
assert_eq!(gradient.0[5].1, Colour::transparent());
assert_eq!(gradient.0[6].1, Colour::grey(0.2));
gradient.insert(0.5, Colour::red(1.0).with_blue(1.0));
assert_eq!(
gradient.0,
vec![
(0.3, Colour::grey(0.5)),
(0.5, Colour::red(1.0).with_blue(1.0)),
(0.6, Colour::red(0.8)),
(0.7, Colour::green(1.0)),
(0.8, Colour::blue(1.0)),
(0.85, Colour::transparent()),
(0.9, Colour::grey(0.2))
]
);
}
#[test]
pub fn interpolation_test() {
let gradient = Gradient(vec![
(0.5, Colour::solid(1.0, 0.0, 0.0)),
(0.7, Colour::solid(0.0, 1.0, 0.0)),
(0.8, Colour::solid(0.0, 0.0, 1.0)),
]);
let other_gradient = Gradient(vec![(0.4, Colour::grey(1.0)), (0.6, Colour::transparent())]);
assert_eq!(other_gradient.get(0.5), Colour::grey(0.5).with_alpha(0.5));
assert_eq!(gradient.get(0.6), Colour::solid(0.5, 0.5, 0.0));
assert_relative_eq!(other_gradient.get(0.8), Colour::transparent());
}
}