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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2024 the Color Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
use crate::;
/// The iterator for gradient approximation.
///
/// This will yield a value for each gradient stop, including `t` values
/// of 0 and 1 at the endpoints.
///
/// Use the [`gradient`] function to generate this iterator.
/// Generate a piecewise linear approximation to a gradient ramp.
///
/// The target gradient ramp is the linear interpolation from `color0` to `color1` in the target
/// color space specified by `interp_cs`. For efficiency, this function returns an
/// [iterator over color stops](GradientIter) in the `CS` color space, such that the gradient ramp
/// created by linearly interpolating between those stops in the `CS` color space is equal within
/// the specified `tolerance` to the target gradient ramp.
///
/// When the target interpolation color space is cylindrical, the hue can be interpolated in
/// multiple ways. The [`direction`](`HueDirection`) parameter controls the way in which the hue is
/// interpolated.
///
/// The given `tolerance` value specifies the maximum perceptual error in the approximation
/// measured as the [Euclidean distance][euclidean-distance] in the [Oklab] color space (see also
/// [`PremulColor::difference`][crate::PremulColor::difference]). This metric is known as
/// [deltaEOK][delta-eok]. A reasonable value is 0.01, which in testing is nearly indistinguishable
/// from the exact ramp. The number of stops scales roughly as the inverse square root of the
/// tolerance.
///
/// The error is measured at the midpoint of each segment, which in some cases may underestimate
/// the error.
///
/// For regular interpolation between two colors, see [`DynamicColor::interpolate`].
///
/// [euclidean-distance]: https://en.wikipedia.org/wiki/Euclidean_distance
/// [delta-eok]: https://www.w3.org/TR/css-color-4/#color-difference-OK
///
/// # Motivation
///
/// A major feature of CSS Color 4 is the ability to specify color interpolation in any
/// interpolation color space [CSS Color Module Level 4 § 12.1][css-sec], which may be quite a bit
/// better than simple linear interpolation in sRGB (for example).
///
/// One strategy for implementing these gradients is to interpolate in the appropriate
/// (premultiplied) space, then map each resulting color to the space used for compositing. That
/// can be expensive. An alternative strategy is to precompute a piecewise linear ramp that closely
/// approximates the desired ramp, then render that using high performance techniques. This method
/// computes such an approximation.
///
/// [css-sec]: https://www.w3.org/TR/css-color-4/#interpolation-space
///
/// # Example
///
/// The following compares interpolating in the target color space Oklab with interpolating
/// piecewise in the color space sRGB.
///
/// ```rust
/// use color::{AlphaColor, ColorSpaceTag, DynamicColor, HueDirection, Oklab, Srgb};
///
/// let start = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([1., 0., 0., 1.]));
/// let end = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([0., 1., 0., 1.]));
///
/// // Interpolation in a target interpolation color space.
/// let interp = start.interpolate(end, ColorSpaceTag::Oklab, HueDirection::default());
/// // Piecewise-approximated interpolation in a compositing color space.
/// let mut gradient = color::gradient::<Srgb>(
/// start,
/// end,
/// ColorSpaceTag::Oklab,
/// HueDirection::default(),
/// 0.01,
/// );
///
/// let (mut t0, mut stop0) = gradient.next().unwrap();
/// for (t1, stop1) in gradient {
/// // Compare a few points between the piecewise stops.
/// for point in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] {
/// let interpolated_point = interp
/// .eval(t0 + (t1 - t0) * point)
/// .to_alpha_color::<Srgb>()
/// .discard_alpha();
/// let approximated_point = stop0.lerp_rect(stop1, point).discard_alpha();
///
/// // The perceptual deltaEOK between the two is lower than the tolerance.
/// assert!(
/// approximated_point
/// .convert::<Oklab>()
/// .difference(interpolated_point.convert::<Oklab>())
/// < 0.01
/// );
/// }
///
/// t0 = t1;
/// stop0 = stop1;
/// }
/// ```
/// The iterator for gradient approximation.
///
/// This will yield a value for each gradient stop, including `t` values
/// of 0 and 1 at the endpoints.
///
/// Use the [`gradient_unpremultiplied`] function to generate this iterator.
///
/// Similar to [`GradientIter`], but does interpolation in unpremultiplied (straight) alpha space
/// as specified in [HTML 2D Canvas].
///
/// [HTML 2D Canvas]: https://html.spec.whatwg.org/multipage/#interpolation
/// Generate a piecewise linear approximation to a gradient ramp without alpha premultiplication.
///
/// Similar to [`gradient`], but colors are interpolated without premultiplying their color
/// channels by the alpha channel. This is almost never what you want.
///
/// This causes color information to leak out of transparent colors. For example, when
/// interpolating from a fully transparent red to a fully opaque blue in sRGB, this
/// method will go through an intermediate purple.
///
/// This matches behavior of gradients in the HTML `canvas` element.
/// See [The 2D rendering context § Fill and stroke styles][HTML 2D Canvas] of the
/// HTML 2D Canvas specification.
///
/// [HTML 2D Canvas]: <https://html.spec.whatwg.org/multipage/#interpolation>
///
/// # Example
///
/// The following compares interpolating in the target color space Oklab with interpolating
/// piecewise in the color space sRGB.
///
/// ```rust
/// use color::{AlphaColor, ColorSpaceTag, DynamicColor, HueDirection, Oklab, Srgb};
///
/// let start = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([1., 0., 0., 1.]));
/// let end = DynamicColor::from_alpha_color(AlphaColor::<Srgb>::new([0., 1., 0., 1.]));
///
/// // Interpolation in a target interpolation color space.
/// let interp = start.interpolate_unpremultiplied(end, ColorSpaceTag::Oklab, HueDirection::default());
/// // Piecewise-approximated interpolation in a compositing color space.
/// let mut gradient = color::gradient_unpremultiplied::<Srgb>(
/// start,
/// end,
/// ColorSpaceTag::Oklab,
/// HueDirection::default(),
/// 0.01,
/// );
///
/// let (mut t0, mut stop0) = gradient.next().unwrap();
/// for (t1, stop1) in gradient {
/// // Compare a few points between the piecewise stops.
/// for point in [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] {
/// let interpolated_point = interp
/// .eval(t0 + (t1 - t0) * point)
/// .to_alpha_color::<Srgb>()
/// .discard_alpha();
/// let approximated_point = stop0.lerp_rect(stop1, point).discard_alpha();
///
/// // The perceptual deltaEOK between the two is lower than the tolerance.
/// assert!(
/// approximated_point
/// .convert::<Oklab>()
/// .difference(interpolated_point.convert::<Oklab>())
/// < 0.01
/// );
/// }
///
/// t0 = t1;
/// stop0 = stop1;
/// }
/// ```