-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathinterpolation.js
260 lines (221 loc) · 6.62 KB
/
interpolation.js
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
/**
* Functions related to color interpolation
*/
import ColorSpace from "./ColorSpace.js";
import {type, interpolate, isNone} from "./util.js";
import getColor from "./getColor.js";
import clone from "./clone.js";
import to from "./to.js";
import toGamut from "./toGamut.js";
import get from "./get.js";
import set from "./set.js";
import defaults from "./defaults.js";
import * as angles from "./angles.js";
import deltaE from "./deltaE.js";
// Type "imports"
/** @typedef {import("./types.js").ColorTypes} ColorTypes */
/** @typedef {import("./types.js").MixOptions} MixOptions */
/** @typedef {import("./types.js").PlainColorObject} PlainColorObject */
/** @typedef {import("./types.js").Range} Range */
/** @typedef {import("./types.js").RangeOptions} RangeOptions */
/** @typedef {import("./types.js").StepsOptions} StepsOptions */
/** @typedef {import("./types.js").Ref} Ref */
/**
* Return an intermediate color between two colors
* @overload
* @param {ColorTypes} c1
* @param {ColorTypes} c2
* @param {MixOptions} [options]
* @returns {PlainColorObject}
*/
/**
* @overload
* @param {ColorTypes} c1
* @param {ColorTypes} c2
* @param {number} p
* @param {MixOptions} [options]
* @returns {PlainColorObject}
*/
export function mix (c1, c2, p = .5, o = {}) {
[c1, c2] = [getColor(c1), getColor(c2)];
if (type(p) === "object") {
[p, o] = [.5, p];
}
let r = range(c1, c2, o);
return r(p);
}
/**
* Get an array of discrete steps
* @overload
* @param {ColorTypes} c1
* @param {ColorTypes} c2
* @param {StepsOptions} [options]
* @returns {PlainColorObject[]}
*/
/**
* @overload
* @param {Range} range
* @param {StepsOptions} [options]
* @returns {PlainColorObject[]}
*/
export function steps (c1, c2, options = {}) {
let colorRange;
if (isRange(c1)) {
// Tweaking existing range
[colorRange, options] = [c1, c2];
[c1, c2] = colorRange.rangeArgs.colors;
}
let {
maxDeltaE, deltaEMethod,
steps = 2, maxSteps = 1000,
...rangeOptions
} = options;
if (!colorRange) {
[c1, c2] = [getColor(c1), getColor(c2)];
colorRange = range(c1, c2, rangeOptions);
}
let totalDelta = deltaE(c1, c2);
let actualSteps = maxDeltaE > 0 ? Math.max(steps, Math.ceil(totalDelta / maxDeltaE) + 1) : steps;
let ret = [];
if (maxSteps !== undefined) {
actualSteps = Math.min(actualSteps, maxSteps);
}
if (actualSteps === 1) {
ret = [{p: .5, color: colorRange(.5)}];
}
else {
let step = 1 / (actualSteps - 1);
ret = Array.from({length: actualSteps}, (_, i) => {
let p = i * step;
return {p, color: colorRange(p)};
});
}
if (maxDeltaE > 0) {
// Iterate over all stops and find max deltaE
let maxDelta = ret.reduce((acc, cur, i) => {
if (i === 0) {
return 0;
}
let ΔΕ = deltaE(cur.color, ret[i - 1].color, deltaEMethod);
return Math.max(acc, ΔΕ);
}, 0);
while (maxDelta > maxDeltaE) {
// Insert intermediate stops and measure maxDelta again
// We need to do this for all pairs, otherwise the midpoint shifts
maxDelta = 0;
for (let i = 1; (i < ret.length) && (ret.length < maxSteps); i++) {
let prev = ret[i - 1];
let cur = ret[i];
let p = (cur.p + prev.p) / 2;
let color = colorRange(p);
maxDelta = Math.max(maxDelta, deltaE(color, prev.color), deltaE(color, cur.color));
ret.splice(i, 0, {p, color: colorRange(p)});
i++;
}
}
}
ret = ret.map(a => a.color);
return ret;
}
/**
* Creates a function that accepts a number and returns a color.
* For numbers in the range 0 to 1, the function interpolates;
* for numbers outside that range, the function extrapolates
* (and thus may not return the results you expect)
* @overload
* @param {Range} range
* @param {RangeOptions} [options]
* @returns {Range}
*/
/**
* @overload
* @param {ColorTypes} color1
* @param {ColorTypes} color2
* @param {RangeOptions & Record<string, any>} [options]
* @returns {Range}
*/
export function range (color1, color2, options = {}) {
if (isRange(color1)) {
// Tweaking existing range
let [r, options] = [color1, color2];
return range(...r.rangeArgs.colors, {...r.rangeArgs.options, ...options});
}
let {space, outputSpace, progression, premultiplied} = options;
color1 = getColor(color1);
color2 = getColor(color2);
// Make sure we're working on copies of these colors
color1 = clone(color1);
color2 = clone(color2);
let rangeArgs = {colors: [color1, color2], options};
if (space) {
space = ColorSpace.get(space);
}
else {
space = ColorSpace.registry[defaults.interpolationSpace] || color1.space;
}
outputSpace = outputSpace ? ColorSpace.get(outputSpace) : space;
color1 = to(color1, space);
color2 = to(color2, space);
// Gamut map to avoid areas of flat color
color1 = toGamut(color1);
color2 = toGamut(color2);
// Handle hue interpolation
// See https://github.com/w3c/csswg-drafts/issues/4735#issuecomment-635741840
if (space.coords.h && space.coords.h.type === "angle") {
let arc = options.hue = options.hue || "shorter";
let /** @type {Ref} */ hue = [space, "h"];
let [θ1, θ2] = [get(color1, hue), get(color2, hue)];
// Undefined hues must be evaluated before hue fix-up to properly
// calculate hue arcs between undefined and defined hues.
// See https://github.com/w3c/csswg-drafts/issues/9436#issuecomment-1746957545
if (isNone(θ1) && !isNone(θ2)) {
θ1 = θ2;
}
else if (isNone(θ2) && !isNone(θ1)) {
θ2 = θ1;
}
[θ1, θ2] = angles.adjust(arc, [θ1, θ2]);
set(color1, hue, θ1);
set(color2, hue, θ2);
}
if (premultiplied) {
// not coping with polar spaces yet
color1.coords = /** @type {[number, number, number]} */ (color1.coords.map(c => c * color1.alpha));
color2.coords = /** @type {[number, number, number]} */ (color2.coords.map(c => c * color2.alpha));
}
return Object.assign(p => {
p = progression ? progression(p) : p;
let coords = color1.coords.map((start, i) => {
let end = color2.coords[i];
return interpolate(start, end, p);
});
let alpha = interpolate(color1.alpha, color2.alpha, p);
let ret = {space, coords, alpha};
if (premultiplied) {
// undo premultiplication
ret.coords = ret.coords.map(c => c / alpha);
}
if (outputSpace !== space) {
ret = to(ret, outputSpace);
}
return ret;
}, {
rangeArgs,
});
}
/**
* @param {any} val
* @returns {val is Range}
*/
export function isRange (val) {
return type(val) === "function" && !!val.rangeArgs;
}
defaults.interpolationSpace = "lab";
/**
* @param {typeof import("./color.js").default} Color
*/
export function register (Color) {
Color.defineFunction("mix", mix, {returns: "color"});
Color.defineFunction("range", range, {returns: "function<color>"});
Color.defineFunction("steps", steps, {returns: "array<color>"});
}