1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.interpolateColors = void 0;
|
8 |
|
9 | const interpolate_1 = require("./interpolate");
|
10 |
|
11 | const NUMBER = '[-+]?\\d*\\.?\\d+';
|
12 | const PERCENTAGE = NUMBER + '%';
|
13 | function call(...args) {
|
14 | return '\\(\\s*(' + args.join(')\\s*,\\s*(') + ')\\s*\\)';
|
15 | }
|
16 | function getMatchers() {
|
17 | const cachedMatchers = {
|
18 | rgb: undefined,
|
19 | rgba: undefined,
|
20 | hsl: undefined,
|
21 | hsla: undefined,
|
22 | hex3: undefined,
|
23 | hex4: undefined,
|
24 | hex5: undefined,
|
25 | hex6: undefined,
|
26 | hex8: undefined,
|
27 | };
|
28 | if (cachedMatchers.rgb === undefined) {
|
29 | cachedMatchers.rgb = new RegExp('rgb' + call(NUMBER, NUMBER, NUMBER));
|
30 | cachedMatchers.rgba = new RegExp('rgba' + call(NUMBER, NUMBER, NUMBER, NUMBER));
|
31 | cachedMatchers.hsl = new RegExp('hsl' + call(NUMBER, PERCENTAGE, PERCENTAGE));
|
32 | cachedMatchers.hsla = new RegExp('hsla' + call(NUMBER, PERCENTAGE, PERCENTAGE, NUMBER));
|
33 | cachedMatchers.hex3 = /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/;
|
34 | cachedMatchers.hex4 = /^#([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/;
|
35 | cachedMatchers.hex6 = /^#([0-9a-fA-F]{6})$/;
|
36 | cachedMatchers.hex8 = /^#([0-9a-fA-F]{8})$/;
|
37 | }
|
38 | return cachedMatchers;
|
39 | }
|
40 | function hue2rgb(p, q, t) {
|
41 | if (t < 0) {
|
42 | t += 1;
|
43 | }
|
44 | if (t > 1) {
|
45 | t -= 1;
|
46 | }
|
47 | if (t < 1 / 6) {
|
48 | return p + (q - p) * 6 * t;
|
49 | }
|
50 | if (t < 1 / 2) {
|
51 | return q;
|
52 | }
|
53 | if (t < 2 / 3) {
|
54 | return p + (q - p) * (2 / 3 - t) * 6;
|
55 | }
|
56 | return p;
|
57 | }
|
58 | function hslToRgb(h, s, l) {
|
59 | const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
60 | const p = 2 * l - q;
|
61 | const r = hue2rgb(p, q, h + 1 / 3);
|
62 | const g = hue2rgb(p, q, h);
|
63 | const b = hue2rgb(p, q, h - 1 / 3);
|
64 | return ((Math.round(r * 255) << 24) |
|
65 | (Math.round(g * 255) << 16) |
|
66 | (Math.round(b * 255) << 8));
|
67 | }
|
68 | function parse255(str) {
|
69 | const int = Number.parseInt(str, 10);
|
70 | if (int < 0) {
|
71 | return 0;
|
72 | }
|
73 | if (int > 255) {
|
74 | return 255;
|
75 | }
|
76 | return int;
|
77 | }
|
78 | function parse360(str) {
|
79 | const int = Number.parseFloat(str);
|
80 | return (((int % 360) + 360) % 360) / 360;
|
81 | }
|
82 | function parse1(str) {
|
83 | const num = Number.parseFloat(str);
|
84 | if (num < 0) {
|
85 | return 0;
|
86 | }
|
87 | if (num > 1) {
|
88 | return 255;
|
89 | }
|
90 | return Math.round(num * 255);
|
91 | }
|
92 | function parsePercentage(str) {
|
93 |
|
94 | const int = Number.parseFloat(str);
|
95 | if (int < 0) {
|
96 | return 0;
|
97 | }
|
98 | if (int > 100) {
|
99 | return 1;
|
100 | }
|
101 | return int / 100;
|
102 | }
|
103 | const names = {
|
104 | transparent: 0x00000000,
|
105 |
|
106 | aliceblue: 0xf0f8ffff,
|
107 | antiquewhite: 0xfaebd7ff,
|
108 | aqua: 0x00ffffff,
|
109 | aquamarine: 0x7fffd4ff,
|
110 | azure: 0xf0ffffff,
|
111 | beige: 0xf5f5dcff,
|
112 | bisque: 0xffe4c4ff,
|
113 | black: 0x000000ff,
|
114 | blanchedalmond: 0xffebcdff,
|
115 | blue: 0x0000ffff,
|
116 | blueviolet: 0x8a2be2ff,
|
117 | brown: 0xa52a2aff,
|
118 | burlywood: 0xdeb887ff,
|
119 | burntsienna: 0xea7e5dff,
|
120 | cadetblue: 0x5f9ea0ff,
|
121 | chartreuse: 0x7fff00ff,
|
122 | chocolate: 0xd2691eff,
|
123 | coral: 0xff7f50ff,
|
124 | cornflowerblue: 0x6495edff,
|
125 | cornsilk: 0xfff8dcff,
|
126 | crimson: 0xdc143cff,
|
127 | cyan: 0x00ffffff,
|
128 | darkblue: 0x00008bff,
|
129 | darkcyan: 0x008b8bff,
|
130 | darkgoldenrod: 0xb8860bff,
|
131 | darkgray: 0xa9a9a9ff,
|
132 | darkgreen: 0x006400ff,
|
133 | darkgrey: 0xa9a9a9ff,
|
134 | darkkhaki: 0xbdb76bff,
|
135 | darkmagenta: 0x8b008bff,
|
136 | darkolivegreen: 0x556b2fff,
|
137 | darkorange: 0xff8c00ff,
|
138 | darkorchid: 0x9932ccff,
|
139 | darkred: 0x8b0000ff,
|
140 | darksalmon: 0xe9967aff,
|
141 | darkseagreen: 0x8fbc8fff,
|
142 | darkslateblue: 0x483d8bff,
|
143 | darkslategray: 0x2f4f4fff,
|
144 | darkslategrey: 0x2f4f4fff,
|
145 | darkturquoise: 0x00ced1ff,
|
146 | darkviolet: 0x9400d3ff,
|
147 | deeppink: 0xff1493ff,
|
148 | deepskyblue: 0x00bfffff,
|
149 | dimgray: 0x696969ff,
|
150 | dimgrey: 0x696969ff,
|
151 | dodgerblue: 0x1e90ffff,
|
152 | firebrick: 0xb22222ff,
|
153 | floralwhite: 0xfffaf0ff,
|
154 | forestgreen: 0x228b22ff,
|
155 | fuchsia: 0xff00ffff,
|
156 | gainsboro: 0xdcdcdcff,
|
157 | ghostwhite: 0xf8f8ffff,
|
158 | gold: 0xffd700ff,
|
159 | goldenrod: 0xdaa520ff,
|
160 | gray: 0x808080ff,
|
161 | green: 0x008000ff,
|
162 | greenyellow: 0xadff2fff,
|
163 | grey: 0x808080ff,
|
164 | honeydew: 0xf0fff0ff,
|
165 | hotpink: 0xff69b4ff,
|
166 | indianred: 0xcd5c5cff,
|
167 | indigo: 0x4b0082ff,
|
168 | ivory: 0xfffff0ff,
|
169 | khaki: 0xf0e68cff,
|
170 | lavender: 0xe6e6faff,
|
171 | lavenderblush: 0xfff0f5ff,
|
172 | lawngreen: 0x7cfc00ff,
|
173 | lemonchiffon: 0xfffacdff,
|
174 | lightblue: 0xadd8e6ff,
|
175 | lightcoral: 0xf08080ff,
|
176 | lightcyan: 0xe0ffffff,
|
177 | lightgoldenrodyellow: 0xfafad2ff,
|
178 | lightgray: 0xd3d3d3ff,
|
179 | lightgreen: 0x90ee90ff,
|
180 | lightgrey: 0xd3d3d3ff,
|
181 | lightpink: 0xffb6c1ff,
|
182 | lightsalmon: 0xffa07aff,
|
183 | lightseagreen: 0x20b2aaff,
|
184 | lightskyblue: 0x87cefaff,
|
185 | lightslategray: 0x778899ff,
|
186 | lightslategrey: 0x778899ff,
|
187 | lightsteelblue: 0xb0c4deff,
|
188 | lightyellow: 0xffffe0ff,
|
189 | lime: 0x00ff00ff,
|
190 | limegreen: 0x32cd32ff,
|
191 | linen: 0xfaf0e6ff,
|
192 | magenta: 0xff00ffff,
|
193 | maroon: 0x800000ff,
|
194 | mediumaquamarine: 0x66cdaaff,
|
195 | mediumblue: 0x0000cdff,
|
196 | mediumorchid: 0xba55d3ff,
|
197 | mediumpurple: 0x9370dbff,
|
198 | mediumseagreen: 0x3cb371ff,
|
199 | mediumslateblue: 0x7b68eeff,
|
200 | mediumspringgreen: 0x00fa9aff,
|
201 | mediumturquoise: 0x48d1ccff,
|
202 | mediumvioletred: 0xc71585ff,
|
203 | midnightblue: 0x191970ff,
|
204 | mintcream: 0xf5fffaff,
|
205 | mistyrose: 0xffe4e1ff,
|
206 | moccasin: 0xffe4b5ff,
|
207 | navajowhite: 0xffdeadff,
|
208 | navy: 0x000080ff,
|
209 | oldlace: 0xfdf5e6ff,
|
210 | olive: 0x808000ff,
|
211 | olivedrab: 0x6b8e23ff,
|
212 | orange: 0xffa500ff,
|
213 | orangered: 0xff4500ff,
|
214 | orchid: 0xda70d6ff,
|
215 | palegoldenrod: 0xeee8aaff,
|
216 | palegreen: 0x98fb98ff,
|
217 | paleturquoise: 0xafeeeeff,
|
218 | palevioletred: 0xdb7093ff,
|
219 | papayawhip: 0xffefd5ff,
|
220 | peachpuff: 0xffdab9ff,
|
221 | peru: 0xcd853fff,
|
222 | pink: 0xffc0cbff,
|
223 | plum: 0xdda0ddff,
|
224 | powderblue: 0xb0e0e6ff,
|
225 | purple: 0x800080ff,
|
226 | rebeccapurple: 0x663399ff,
|
227 | red: 0xff0000ff,
|
228 | rosybrown: 0xbc8f8fff,
|
229 | royalblue: 0x4169e1ff,
|
230 | saddlebrown: 0x8b4513ff,
|
231 | salmon: 0xfa8072ff,
|
232 | sandybrown: 0xf4a460ff,
|
233 | seagreen: 0x2e8b57ff,
|
234 | seashell: 0xfff5eeff,
|
235 | sienna: 0xa0522dff,
|
236 | silver: 0xc0c0c0ff,
|
237 | skyblue: 0x87ceebff,
|
238 | slateblue: 0x6a5acdff,
|
239 | slategray: 0x708090ff,
|
240 | slategrey: 0x708090ff,
|
241 | snow: 0xfffafaff,
|
242 | springgreen: 0x00ff7fff,
|
243 | steelblue: 0x4682b4ff,
|
244 | tan: 0xd2b48cff,
|
245 | teal: 0x008080ff,
|
246 | thistle: 0xd8bfd8ff,
|
247 | tomato: 0xff6347ff,
|
248 | turquoise: 0x40e0d0ff,
|
249 | violet: 0xee82eeff,
|
250 | wheat: 0xf5deb3ff,
|
251 | white: 0xffffffff,
|
252 | whitesmoke: 0xf5f5f5ff,
|
253 | yellow: 0xffff00ff,
|
254 | yellowgreen: 0x9acd32ff,
|
255 | };
|
256 | function normalizeColor(color) {
|
257 | const matchers = getMatchers();
|
258 | let match;
|
259 |
|
260 | if (matchers.hex6) {
|
261 | if ((match = matchers.hex6.exec(color))) {
|
262 | return Number.parseInt(match[1] + 'ff', 16) >>> 0;
|
263 | }
|
264 | }
|
265 | if (names[color] !== undefined) {
|
266 | return names[color];
|
267 | }
|
268 | if (matchers.rgb) {
|
269 | if ((match = matchers.rgb.exec(color))) {
|
270 | return (
|
271 |
|
272 | ((parse255(match[1]) << 24) |
|
273 | (parse255(match[2]) << 16) |
|
274 | (parse255(match[3]) << 8) |
|
275 | 0x000000ff) >>>
|
276 | 0);
|
277 | }
|
278 | }
|
279 | if (matchers.rgba) {
|
280 | if ((match = matchers.rgba.exec(color))) {
|
281 | return (
|
282 |
|
283 | ((parse255(match[1]) << 24) |
|
284 | (parse255(match[2]) << 16) |
|
285 | (parse255(match[3]) << 8) |
|
286 | parse1(match[4])) >>>
|
287 | 0);
|
288 | }
|
289 | }
|
290 | if (matchers.hex3) {
|
291 | if ((match = matchers.hex3.exec(color))) {
|
292 | return (Number.parseInt(match[1] +
|
293 | match[1] +
|
294 | match[2] +
|
295 | match[2] +
|
296 | match[3] +
|
297 | match[3] +
|
298 | 'ff',
|
299 | 16) >>> 0);
|
300 | }
|
301 | }
|
302 |
|
303 | if (matchers.hex8) {
|
304 | if ((match = matchers.hex8.exec(color))) {
|
305 | return Number.parseInt(match[1], 16) >>> 0;
|
306 | }
|
307 | }
|
308 | if (matchers.hex4) {
|
309 | if ((match = matchers.hex4.exec(color))) {
|
310 | return (Number.parseInt(match[1] +
|
311 | match[1] +
|
312 | match[2] +
|
313 | match[2] +
|
314 | match[3] +
|
315 | match[3] +
|
316 | match[4] +
|
317 | match[4],
|
318 | 16) >>> 0);
|
319 | }
|
320 | }
|
321 | if (matchers.hsl) {
|
322 | if ((match = matchers.hsl.exec(color))) {
|
323 | return ((hslToRgb(parse360(match[1]),
|
324 | parsePercentage(match[2]),
|
325 | parsePercentage(match[3])
|
326 | ) |
|
327 | 0x000000ff) >>>
|
328 | 0);
|
329 | }
|
330 | }
|
331 | if (matchers.hsla) {
|
332 | if ((match = matchers.hsla.exec(color))) {
|
333 | return ((hslToRgb(parse360(match[1]),
|
334 | parsePercentage(match[2]),
|
335 | parsePercentage(match[3])
|
336 | ) |
|
337 | parse1(match[4])) >>>
|
338 | 0);
|
339 | }
|
340 | }
|
341 | throw new Error(`invalid color string ${color} provided`);
|
342 | }
|
343 | const opacity = (c) => {
|
344 | return ((c >> 24) & 255) / 255;
|
345 | };
|
346 | const red = (c) => {
|
347 | return (c >> 16) & 255;
|
348 | };
|
349 | const green = (c) => {
|
350 | return (c >> 8) & 255;
|
351 | };
|
352 | const blue = (c) => {
|
353 | return c & 255;
|
354 | };
|
355 | const rgbaColor = (r, g, b, alpha) => {
|
356 | return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
357 | };
|
358 | function processColorInitially(color) {
|
359 | let normalizedColor = normalizeColor(color);
|
360 | normalizedColor = ((normalizedColor << 24) | (normalizedColor >>> 8)) >>> 0;
|
361 | return normalizedColor;
|
362 | }
|
363 | function processColor(color) {
|
364 | const normalizedColor = processColorInitially(color);
|
365 | return normalizedColor;
|
366 | }
|
367 | const interpolateColorsRGB = (value, inputRange, colors) => {
|
368 | const [r, g, b, a] = [red, green, blue, opacity].map((f) => {
|
369 | const unrounded = (0, interpolate_1.interpolate)(value, inputRange, colors.map((c) => f(c)), {
|
370 | extrapolateLeft: 'clamp',
|
371 | extrapolateRight: 'clamp',
|
372 | });
|
373 | if (f === opacity) {
|
374 | return Number(unrounded.toFixed(3));
|
375 | }
|
376 | return Math.round(unrounded);
|
377 | });
|
378 | return rgbaColor(r, g, b, a);
|
379 | };
|
380 | const interpolateColors = (input, inputRange, outputRange) => {
|
381 | if (typeof input === 'undefined') {
|
382 | throw new TypeError('input can not be undefined');
|
383 | }
|
384 | if (typeof inputRange === 'undefined') {
|
385 | throw new TypeError('inputRange can not be undefined');
|
386 | }
|
387 | if (typeof outputRange === 'undefined') {
|
388 | throw new TypeError('outputRange can not be undefined');
|
389 | }
|
390 | if (inputRange.length !== outputRange.length) {
|
391 | throw new TypeError('inputRange (' +
|
392 | inputRange.length +
|
393 | ' values provided) and outputRange (' +
|
394 | outputRange.length +
|
395 | ' values provided) must have the same length');
|
396 | }
|
397 | const processedOutputRange = outputRange.map((c) => processColor(c));
|
398 | return interpolateColorsRGB(input, inputRange, processedOutputRange);
|
399 | };
|
400 | exports.interpolateColors = interpolateColors;
|
401 |
|
\ | No newline at end of file |