1 |
|
2 |
|
3 | const convert = {
|
4 | rgb: {channels: 3, labels: 'rgb'},
|
5 | hsl: {channels: 3, labels: 'hsl'},
|
6 | hsv: {channels: 3, labels: 'hsv'},
|
7 | hwb: {channels: 3, labels: 'hwb'},
|
8 | cmyk: {channels: 4, labels: 'cmyk'},
|
9 | xyz: {channels: 3, labels: 'xyz'},
|
10 | lab: {channels: 3, labels: 'lab'},
|
11 | lch: {channels: 3, labels: 'lch'},
|
12 | hex: {channels: 1, labels: ['hex']},
|
13 | ansi16: {channels: 1, labels: ['ansi16']},
|
14 | ansi256: {channels: 1, labels: ['ansi256']},
|
15 | hcg: {channels: 3, labels: ['h', 'c', 'g']},
|
16 | apple: {channels: 3, labels: ['r16', 'g16', 'b16']},
|
17 | gray: {channels: 1, labels: ['gray']}
|
18 | };
|
19 |
|
20 | module.exports = {conv: convert};
|
21 |
|
22 |
|
23 | for (const model of Object.keys(convert)) {
|
24 | if (!('channels' in convert[model])) {
|
25 | throw new Error('missing channels property: ' + model);
|
26 | }
|
27 |
|
28 | if (!('labels' in convert[model])) {
|
29 | throw new Error('missing channel labels property: ' + model);
|
30 | }
|
31 |
|
32 | if (convert[model].labels.length !== convert[model].channels) {
|
33 | throw new Error('channel and label counts mismatch: ' + model);
|
34 | }
|
35 |
|
36 | const {channels, labels} = convert[model];
|
37 | delete convert[model].channels;
|
38 | delete convert[model].labels;
|
39 | Object.defineProperty(convert[model], 'channels', {value: channels});
|
40 | Object.defineProperty(convert[model], 'labels', {value: labels});
|
41 | }
|
42 |
|
43 | convert.rgb.hsl = function (rgb) {
|
44 | const r = rgb[0] / 255;
|
45 | const g = rgb[1] / 255;
|
46 | const b = rgb[2] / 255;
|
47 | const min = Math.min(r, g, b);
|
48 | const max = Math.max(r, g, b);
|
49 | const delta = max - min;
|
50 | let h;
|
51 | let s;
|
52 |
|
53 | if (max === min) {
|
54 | h = 0;
|
55 | } else if (r === max) {
|
56 | h = (g - b) / delta;
|
57 | } else if (g === max) {
|
58 | h = 2 + (b - r) / delta;
|
59 | } else if (b === max) {
|
60 | h = 4 + (r - g) / delta;
|
61 | }
|
62 |
|
63 | h = Math.min(h * 60, 360);
|
64 |
|
65 | if (h < 0) {
|
66 | h += 360;
|
67 | }
|
68 |
|
69 | const l = (min + max) / 2;
|
70 |
|
71 | if (max === min) {
|
72 | s = 0;
|
73 | } else if (l <= 0.5) {
|
74 | s = delta / (max + min);
|
75 | } else {
|
76 | s = delta / (2 - max - min);
|
77 | }
|
78 |
|
79 | return [h, s * 100, l * 100];
|
80 | };
|
81 |
|
82 | convert.rgb.hsv = function (rgb) {
|
83 | let rdif;
|
84 | let gdif;
|
85 | let bdif;
|
86 | let h;
|
87 | let s;
|
88 |
|
89 | const r = rgb[0] / 255;
|
90 | const g = rgb[1] / 255;
|
91 | const b = rgb[2] / 255;
|
92 | const v = Math.max(r, g, b);
|
93 | const diff = v - Math.min(r, g, b);
|
94 | const diffc = function (c) {
|
95 | return (v - c) / 6 / diff + 1 / 2;
|
96 | };
|
97 |
|
98 | if (diff === 0) {
|
99 | h = 0;
|
100 | s = 0;
|
101 | } else {
|
102 | s = diff / v;
|
103 | rdif = diffc(r);
|
104 | gdif = diffc(g);
|
105 | bdif = diffc(b);
|
106 |
|
107 | if (r === v) {
|
108 | h = bdif - gdif;
|
109 | } else if (g === v) {
|
110 | h = (1 / 3) + rdif - bdif;
|
111 | } else if (b === v) {
|
112 | h = (2 / 3) + gdif - rdif;
|
113 | }
|
114 |
|
115 | if (h < 0) {
|
116 | h += 1;
|
117 | } else if (h > 1) {
|
118 | h -= 1;
|
119 | }
|
120 | }
|
121 |
|
122 | return [
|
123 | h * 360,
|
124 | s * 100,
|
125 | v * 100
|
126 | ];
|
127 | };
|
128 |
|
129 | convert.rgb.hwb = function (rgb) {
|
130 | const r = rgb[0];
|
131 | const g = rgb[1];
|
132 | let b = rgb[2];
|
133 | const h = convert.rgb.hsl(rgb)[0];
|
134 | const w = 1 / 255 * Math.min(r, Math.min(g, b));
|
135 |
|
136 | b = 1 - 1 / 255 * Math.max(r, Math.max(g, b));
|
137 |
|
138 | return [h, w * 100, b * 100];
|
139 | };
|
140 |
|
141 | convert.rgb.cmyk = function (rgb) {
|
142 | const r = rgb[0] / 255;
|
143 | const g = rgb[1] / 255;
|
144 | const b = rgb[2] / 255;
|
145 |
|
146 | const k = Math.min(1 - r, 1 - g, 1 - b);
|
147 | const c = (1 - r - k) / (1 - k) || 0;
|
148 | const m = (1 - g - k) / (1 - k) || 0;
|
149 | const y = (1 - b - k) / (1 - k) || 0;
|
150 |
|
151 | return [c * 100, m * 100, y * 100, k * 100];
|
152 | };
|
153 |
|
154 | function comparativeDistance(x, y) {
|
155 | |
156 |
|
157 |
|
158 | return (
|
159 | ((x[0] - y[0]) ** 2) +
|
160 | ((x[1] - y[1]) ** 2) +
|
161 | ((x[2] - y[2]) ** 2)
|
162 | );
|
163 | }
|
164 |
|
165 | convert.rgb.xyz = function (rgb) {
|
166 | let r = rgb[0] / 255;
|
167 | let g = rgb[1] / 255;
|
168 | let b = rgb[2] / 255;
|
169 |
|
170 |
|
171 | r = r > 0.04045 ? (((r + 0.055) / 1.055) ** 2.4) : (r / 12.92);
|
172 | g = g > 0.04045 ? (((g + 0.055) / 1.055) ** 2.4) : (g / 12.92);
|
173 | b = b > 0.04045 ? (((b + 0.055) / 1.055) ** 2.4) : (b / 12.92);
|
174 |
|
175 | const x = (r * 0.4124564) + (g * 0.3575761) + (b * 0.1804375);
|
176 | const y = (r * 0.2126729) + (g * 0.7151522) + (b * 0.072175);
|
177 | const z = (r * 0.0193339) + (g * 0.119192) + (b * 0.9503041);
|
178 |
|
179 | return [x * 100, y * 100, z * 100];
|
180 | };
|
181 |
|
182 | convert.rgb.lab = function (rgb) {
|
183 | const xyz = convert.rgb.xyz(rgb);
|
184 | let x = xyz[0];
|
185 | let y = xyz[1];
|
186 | let z = xyz[2];
|
187 |
|
188 | x /= 95.047;
|
189 | y /= 100;
|
190 | z /= 108.883;
|
191 |
|
192 | x = x > 0.008856 ? (x ** (1 / 3)) : (7.787 * x) + (16 / 116);
|
193 | y = y > 0.008856 ? (y ** (1 / 3)) : (7.787 * y) + (16 / 116);
|
194 | z = z > 0.008856 ? (z ** (1 / 3)) : (7.787 * z) + (16 / 116);
|
195 |
|
196 | const l = (116 * y) - 16;
|
197 | const a = 500 * (x - y);
|
198 | const b = 200 * (y - z);
|
199 |
|
200 | return [l, a, b];
|
201 | };
|
202 |
|
203 | convert.hsl.rgb = function (hsl) {
|
204 | const h = hsl[0] / 360;
|
205 | const s = hsl[1] / 100;
|
206 | const l = hsl[2] / 100;
|
207 | let t2;
|
208 | let t3;
|
209 | let val;
|
210 |
|
211 | if (s === 0) {
|
212 | val = l * 255;
|
213 | return [val, val, val];
|
214 | }
|
215 |
|
216 | if (l < 0.5) {
|
217 | t2 = l * (1 + s);
|
218 | } else {
|
219 | t2 = l + s - l * s;
|
220 | }
|
221 |
|
222 | const t1 = 2 * l - t2;
|
223 |
|
224 | const rgb = [0, 0, 0];
|
225 | for (let i = 0; i < 3; i++) {
|
226 | t3 = h + 1 / 3 * -(i - 1);
|
227 | if (t3 < 0) {
|
228 | t3++;
|
229 | }
|
230 |
|
231 | if (t3 > 1) {
|
232 | t3--;
|
233 | }
|
234 |
|
235 | if (6 * t3 < 1) {
|
236 | val = t1 + (t2 - t1) * 6 * t3;
|
237 | } else if (2 * t3 < 1) {
|
238 | val = t2;
|
239 | } else if (3 * t3 < 2) {
|
240 | val = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
|
241 | } else {
|
242 | val = t1;
|
243 | }
|
244 |
|
245 | rgb[i] = val * 255;
|
246 | }
|
247 |
|
248 | return rgb;
|
249 | };
|
250 |
|
251 | convert.hsl.hsv = function (hsl) {
|
252 | const h = hsl[0];
|
253 | let s = hsl[1] / 100;
|
254 | let l = hsl[2] / 100;
|
255 | let smin = s;
|
256 | const lmin = Math.max(l, 0.01);
|
257 |
|
258 | l *= 2;
|
259 | s *= (l <= 1) ? l : 2 - l;
|
260 | smin *= lmin <= 1 ? lmin : 2 - lmin;
|
261 | const v = (l + s) / 2;
|
262 | const sv = l === 0 ? (2 * smin) / (lmin + smin) : (2 * s) / (l + s);
|
263 |
|
264 | return [h, sv * 100, v * 100];
|
265 | };
|
266 |
|
267 | convert.hsv.rgb = function (hsv) {
|
268 | const h = hsv[0] / 60;
|
269 | const s = hsv[1] / 100;
|
270 | let v = hsv[2] / 100;
|
271 | const hi = Math.floor(h) % 6;
|
272 |
|
273 | const f = h - Math.floor(h);
|
274 | const p = 255 * v * (1 - s);
|
275 | const q = 255 * v * (1 - (s * f));
|
276 | const t = 255 * v * (1 - (s * (1 - f)));
|
277 | v *= 255;
|
278 |
|
279 | switch (hi) {
|
280 | case 0:
|
281 | return [v, t, p];
|
282 | case 1:
|
283 | return [q, v, p];
|
284 | case 2:
|
285 | return [p, v, t];
|
286 | case 3:
|
287 | return [p, q, v];
|
288 | case 4:
|
289 | return [t, p, v];
|
290 | case 5:
|
291 | return [v, p, q];
|
292 | }
|
293 | };
|
294 |
|
295 | convert.hsv.hsl = function (hsv) {
|
296 | const h = hsv[0];
|
297 | const s = hsv[1] / 100;
|
298 | const v = hsv[2] / 100;
|
299 | const vmin = Math.max(v, 0.01);
|
300 | let sl;
|
301 | let l;
|
302 |
|
303 | l = (2 - s) * v;
|
304 | const lmin = (2 - s) * vmin;
|
305 | sl = s * vmin;
|
306 | sl /= (lmin <= 1) ? lmin : 2 - lmin;
|
307 | sl = sl || 0;
|
308 | l /= 2;
|
309 |
|
310 | return [h, sl * 100, l * 100];
|
311 | };
|
312 |
|
313 |
|
314 | convert.hwb.rgb = function (hwb) {
|
315 | const h = hwb[0] / 360;
|
316 | let wh = hwb[1] / 100;
|
317 | let bl = hwb[2] / 100;
|
318 | const ratio = wh + bl;
|
319 | let f;
|
320 |
|
321 |
|
322 | if (ratio > 1) {
|
323 | wh /= ratio;
|
324 | bl /= ratio;
|
325 | }
|
326 |
|
327 | const i = Math.floor(6 * h);
|
328 | const v = 1 - bl;
|
329 | f = 6 * h - i;
|
330 |
|
331 | if ((i & 0x01) !== 0) {
|
332 | f = 1 - f;
|
333 | }
|
334 |
|
335 | const n = wh + f * (v - wh);
|
336 |
|
337 | let r;
|
338 | let g;
|
339 | let b;
|
340 |
|
341 | switch (i) {
|
342 | default:
|
343 | case 6:
|
344 | case 0: r = v; g = n; b = wh; break;
|
345 | case 1: r = n; g = v; b = wh; break;
|
346 | case 2: r = wh; g = v; b = n; break;
|
347 | case 3: r = wh; g = n; b = v; break;
|
348 | case 4: r = n; g = wh; b = v; break;
|
349 | case 5: r = v; g = wh; b = n; break;
|
350 | }
|
351 |
|
352 |
|
353 | return [r * 255, g * 255, b * 255];
|
354 | };
|
355 |
|
356 | convert.cmyk.rgb = function (cmyk) {
|
357 | const c = cmyk[0] / 100;
|
358 | const m = cmyk[1] / 100;
|
359 | const y = cmyk[2] / 100;
|
360 | const k = cmyk[3] / 100;
|
361 |
|
362 | const r = 1 - Math.min(1, c * (1 - k) + k);
|
363 | const g = 1 - Math.min(1, m * (1 - k) + k);
|
364 | const b = 1 - Math.min(1, y * (1 - k) + k);
|
365 |
|
366 | return [r * 255, g * 255, b * 255];
|
367 | };
|
368 |
|
369 | convert.xyz.rgb = function (xyz) {
|
370 | const x = xyz[0] / 100;
|
371 | const y = xyz[1] / 100;
|
372 | const z = xyz[2] / 100;
|
373 | let r;
|
374 | let g;
|
375 | let b;
|
376 |
|
377 | r = (x * 3.2404542) + (y * -1.5371385) + (z * -0.4985314);
|
378 | g = (x * -0.969266) + (y * 1.8760108) + (z * 0.041556);
|
379 | b = (x * 0.0556434) + (y * -0.2040259) + (z * 1.0572252);
|
380 |
|
381 |
|
382 | r = r > 0.0031308
|
383 | ? ((1.055 * (r ** (1.0 / 2.4))) - 0.055)
|
384 | : r * 12.92;
|
385 |
|
386 | g = g > 0.0031308
|
387 | ? ((1.055 * (g ** (1.0 / 2.4))) - 0.055)
|
388 | : g * 12.92;
|
389 |
|
390 | b = b > 0.0031308
|
391 | ? ((1.055 * (b ** (1.0 / 2.4))) - 0.055)
|
392 | : b * 12.92;
|
393 |
|
394 | r = Math.min(Math.max(0, r), 1);
|
395 | g = Math.min(Math.max(0, g), 1);
|
396 | b = Math.min(Math.max(0, b), 1);
|
397 |
|
398 | return [r * 255, g * 255, b * 255];
|
399 | };
|
400 |
|
401 | convert.xyz.lab = function (xyz) {
|
402 | let x = xyz[0];
|
403 | let y = xyz[1];
|
404 | let z = xyz[2];
|
405 |
|
406 | x /= 95.047;
|
407 | y /= 100;
|
408 | z /= 108.883;
|
409 |
|
410 | x = x > 0.008856 ? (x ** (1 / 3)) : (7.787 * x) + (16 / 116);
|
411 | y = y > 0.008856 ? (y ** (1 / 3)) : (7.787 * y) + (16 / 116);
|
412 | z = z > 0.008856 ? (z ** (1 / 3)) : (7.787 * z) + (16 / 116);
|
413 |
|
414 | const l = (116 * y) - 16;
|
415 | const a = 500 * (x - y);
|
416 | const b = 200 * (y - z);
|
417 |
|
418 | return [l, a, b];
|
419 | };
|
420 |
|
421 | convert.lab.xyz = function (lab) {
|
422 | const l = lab[0];
|
423 | const a = lab[1];
|
424 | const b = lab[2];
|
425 | let x;
|
426 | let y;
|
427 | let z;
|
428 |
|
429 | y = (l + 16) / 116;
|
430 | x = a / 500 + y;
|
431 | z = y - b / 200;
|
432 |
|
433 | const y2 = y ** 3;
|
434 | const x2 = x ** 3;
|
435 | const z2 = z ** 3;
|
436 | y = y2 > 0.008856 ? y2 : (y - 16 / 116) / 7.787;
|
437 | x = x2 > 0.008856 ? x2 : (x - 16 / 116) / 7.787;
|
438 | z = z2 > 0.008856 ? z2 : (z - 16 / 116) / 7.787;
|
439 |
|
440 | x *= 95.047;
|
441 | y *= 100;
|
442 | z *= 108.883;
|
443 |
|
444 | return [x, y, z];
|
445 | };
|
446 |
|
447 | convert.lab.lch = function (lab) {
|
448 | const l = lab[0];
|
449 | const a = lab[1];
|
450 | const b = lab[2];
|
451 | let h;
|
452 |
|
453 | const hr = Math.atan2(b, a);
|
454 | h = hr * 360 / 2 / Math.PI;
|
455 |
|
456 | if (h < 0) {
|
457 | h += 360;
|
458 | }
|
459 |
|
460 | const c = Math.sqrt(a * a + b * b);
|
461 |
|
462 | return [l, c, h];
|
463 | };
|
464 |
|
465 | convert.lch.lab = function (lch) {
|
466 | const l = lch[0];
|
467 | const c = lch[1];
|
468 | const h = lch[2];
|
469 |
|
470 | const hr = h / 360 * 2 * Math.PI;
|
471 | const a = c * Math.cos(hr);
|
472 | const b = c * Math.sin(hr);
|
473 |
|
474 | return [l, a, b];
|
475 | };
|
476 |
|
477 | convert.rgb.ansi16 = function (args, saturation = null) {
|
478 | const [r, g, b] = args;
|
479 | let value = saturation === null ? convert.rgb.hsv(args)[2] : saturation;
|
480 |
|
481 | value = Math.round(value / 50);
|
482 |
|
483 | if (value === 0) {
|
484 | return 30;
|
485 | }
|
486 |
|
487 | let ansi = 30
|
488 | + ((Math.round(b / 255) << 2)
|
489 | | (Math.round(g / 255) << 1)
|
490 | | Math.round(r / 255));
|
491 |
|
492 | if (value === 2) {
|
493 | ansi += 60;
|
494 | }
|
495 |
|
496 | return ansi;
|
497 | };
|
498 |
|
499 | convert.hsv.ansi16 = function (args) {
|
500 |
|
501 |
|
502 | return convert.rgb.ansi16(convert.hsv.rgb(args), args[2]);
|
503 | };
|
504 |
|
505 | convert.rgb.ansi256 = function (args) {
|
506 | const r = args[0];
|
507 | const g = args[1];
|
508 | const b = args[2];
|
509 |
|
510 |
|
511 |
|
512 | if (r === g && g === b) {
|
513 | if (r < 8) {
|
514 | return 16;
|
515 | }
|
516 |
|
517 | if (r > 248) {
|
518 | return 231;
|
519 | }
|
520 |
|
521 | return Math.round(((r - 8) / 247) * 24) + 232;
|
522 | }
|
523 |
|
524 | const ansi = 16
|
525 | + (36 * Math.round(r / 255 * 5))
|
526 | + (6 * Math.round(g / 255 * 5))
|
527 | + Math.round(b / 255 * 5);
|
528 |
|
529 | return ansi;
|
530 | };
|
531 |
|
532 | convert.ansi16.rgb = function (args) {
|
533 | let color = args % 10;
|
534 |
|
535 |
|
536 | if (color === 0 || color === 7) {
|
537 | if (args > 50) {
|
538 | color += 3.5;
|
539 | }
|
540 |
|
541 | color = color / 10.5 * 255;
|
542 |
|
543 | return [color, color, color];
|
544 | }
|
545 |
|
546 | const mult = (~~(args > 50) + 1) * 0.5;
|
547 | const r = ((color & 1) * mult) * 255;
|
548 | const g = (((color >> 1) & 1) * mult) * 255;
|
549 | const b = (((color >> 2) & 1) * mult) * 255;
|
550 |
|
551 | return [r, g, b];
|
552 | };
|
553 |
|
554 | convert.ansi256.rgb = function (args) {
|
555 |
|
556 | if (args >= 232) {
|
557 | const c = (args - 232) * 10 + 8;
|
558 | return [c, c, c];
|
559 | }
|
560 |
|
561 | args -= 16;
|
562 |
|
563 | let rem;
|
564 | const r = Math.floor(args / 36) / 5 * 255;
|
565 | const g = Math.floor((rem = args % 36) / 6) / 5 * 255;
|
566 | const b = (rem % 6) / 5 * 255;
|
567 |
|
568 | return [r, g, b];
|
569 | };
|
570 |
|
571 | convert.rgb.hex = function (args) {
|
572 | const integer = ((Math.round(args[0]) & 0xFF) << 16)
|
573 | + ((Math.round(args[1]) & 0xFF) << 8)
|
574 | + (Math.round(args[2]) & 0xFF);
|
575 |
|
576 | const string = integer.toString(16).toUpperCase();
|
577 | return '000000'.substring(string.length) + string;
|
578 | };
|
579 |
|
580 | convert.hex.rgb = function (args) {
|
581 | const match = args.toString(16).match(/[a-f0-9]{6}|[a-f0-9]{3}/i);
|
582 | if (!match) {
|
583 | return [0, 0, 0];
|
584 | }
|
585 |
|
586 | let colorString = match[0];
|
587 |
|
588 | if (match[0].length === 3) {
|
589 | colorString = colorString.split('').map(char => {
|
590 | return char + char;
|
591 | }).join('');
|
592 | }
|
593 |
|
594 | const integer = parseInt(colorString, 16);
|
595 | const r = (integer >> 16) & 0xFF;
|
596 | const g = (integer >> 8) & 0xFF;
|
597 | const b = integer & 0xFF;
|
598 |
|
599 | return [r, g, b];
|
600 | };
|
601 |
|
602 | convert.hex.hsl = function(args){
|
603 | return convert.rgb.hsl(convert.hex.rgb(args));
|
604 | }
|
605 |
|
606 | convert.rgb.hcg = function (rgb) {
|
607 | const r = rgb[0] / 255;
|
608 | const g = rgb[1] / 255;
|
609 | const b = rgb[2] / 255;
|
610 | const max = Math.max(Math.max(r, g), b);
|
611 | const min = Math.min(Math.min(r, g), b);
|
612 | const chroma = (max - min);
|
613 | let grayscale;
|
614 | let hue;
|
615 |
|
616 | if (chroma < 1) {
|
617 | grayscale = min / (1 - chroma);
|
618 | } else {
|
619 | grayscale = 0;
|
620 | }
|
621 |
|
622 | if (chroma <= 0) {
|
623 | hue = 0;
|
624 | } else
|
625 | if (max === r) {
|
626 | hue = ((g - b) / chroma) % 6;
|
627 | } else
|
628 | if (max === g) {
|
629 | hue = 2 + (b - r) / chroma;
|
630 | } else {
|
631 | hue = 4 + (r - g) / chroma;
|
632 | }
|
633 |
|
634 | hue /= 6;
|
635 | hue %= 1;
|
636 |
|
637 | return [hue * 360, chroma * 100, grayscale * 100];
|
638 | };
|
639 |
|
640 | convert.hsl.hcg = function (hsl) {
|
641 | const s = hsl[1] / 100;
|
642 | const l = hsl[2] / 100;
|
643 |
|
644 | const c = l < 0.5 ? (2.0 * s * l) : (2.0 * s * (1.0 - l));
|
645 |
|
646 | let f = 0;
|
647 | if (c < 1.0) {
|
648 | f = (l - 0.5 * c) / (1.0 - c);
|
649 | }
|
650 |
|
651 | return [hsl[0], c * 100, f * 100];
|
652 | };
|
653 |
|
654 | convert.hsv.hcg = function (hsv) {
|
655 | const s = hsv[1] / 100;
|
656 | const v = hsv[2] / 100;
|
657 |
|
658 | const c = s * v;
|
659 | let f = 0;
|
660 |
|
661 | if (c < 1.0) {
|
662 | f = (v - c) / (1 - c);
|
663 | }
|
664 |
|
665 | return [hsv[0], c * 100, f * 100];
|
666 | };
|
667 |
|
668 | convert.hcg.rgb = function (hcg) {
|
669 | const h = hcg[0] / 360;
|
670 | const c = hcg[1] / 100;
|
671 | const g = hcg[2] / 100;
|
672 |
|
673 | if (c === 0.0) {
|
674 | return [g * 255, g * 255, g * 255];
|
675 | }
|
676 |
|
677 | const pure = [0, 0, 0];
|
678 | const hi = (h % 1) * 6;
|
679 | const v = hi % 1;
|
680 | const w = 1 - v;
|
681 | let mg = 0;
|
682 |
|
683 |
|
684 | switch (Math.floor(hi)) {
|
685 | case 0:
|
686 | pure[0] = 1; pure[1] = v; pure[2] = 0; break;
|
687 | case 1:
|
688 | pure[0] = w; pure[1] = 1; pure[2] = 0; break;
|
689 | case 2:
|
690 | pure[0] = 0; pure[1] = 1; pure[2] = v; break;
|
691 | case 3:
|
692 | pure[0] = 0; pure[1] = w; pure[2] = 1; break;
|
693 | case 4:
|
694 | pure[0] = v; pure[1] = 0; pure[2] = 1; break;
|
695 | default:
|
696 | pure[0] = 1; pure[1] = 0; pure[2] = w;
|
697 | }
|
698 |
|
699 |
|
700 | mg = (1.0 - c) * g;
|
701 |
|
702 | return [
|
703 | (c * pure[0] + mg) * 255,
|
704 | (c * pure[1] + mg) * 255,
|
705 | (c * pure[2] + mg) * 255
|
706 | ];
|
707 | };
|
708 |
|
709 | convert.hcg.hsv = function (hcg) {
|
710 | const c = hcg[1] / 100;
|
711 | const g = hcg[2] / 100;
|
712 |
|
713 | const v = c + g * (1.0 - c);
|
714 | let f = 0;
|
715 |
|
716 | if (v > 0.0) {
|
717 | f = c / v;
|
718 | }
|
719 |
|
720 | return [hcg[0], f * 100, v * 100];
|
721 | };
|
722 |
|
723 | convert.hcg.hsl = function (hcg) {
|
724 | const c = hcg[1] / 100;
|
725 | const g = hcg[2] / 100;
|
726 |
|
727 | const l = g * (1.0 - c) + 0.5 * c;
|
728 | let s = 0;
|
729 |
|
730 | if (l > 0.0 && l < 0.5) {
|
731 | s = c / (2 * l);
|
732 | } else
|
733 | if (l >= 0.5 && l < 1.0) {
|
734 | s = c / (2 * (1 - l));
|
735 | }
|
736 |
|
737 | return [hcg[0], s * 100, l * 100];
|
738 | };
|
739 |
|
740 | convert.hcg.hwb = function (hcg) {
|
741 | const c = hcg[1] / 100;
|
742 | const g = hcg[2] / 100;
|
743 | const v = c + g * (1.0 - c);
|
744 | return [hcg[0], (v - c) * 100, (1 - v) * 100];
|
745 | };
|
746 |
|
747 | convert.hwb.hcg = function (hwb) {
|
748 | const w = hwb[1] / 100;
|
749 | const b = hwb[2] / 100;
|
750 | const v = 1 - b;
|
751 | const c = v - w;
|
752 | let g = 0;
|
753 |
|
754 | if (c < 1) {
|
755 | g = (v - c) / (1 - c);
|
756 | }
|
757 |
|
758 | return [hwb[0], c * 100, g * 100];
|
759 | };
|
760 |
|
761 | convert.apple.rgb = function (apple) {
|
762 | return [(apple[0] / 65535) * 255, (apple[1] / 65535) * 255, (apple[2] / 65535) * 255];
|
763 | };
|
764 |
|
765 | convert.rgb.apple = function (rgb) {
|
766 | return [(rgb[0] / 255) * 65535, (rgb[1] / 255) * 65535, (rgb[2] / 255) * 65535];
|
767 | };
|
768 |
|
769 | convert.gray.rgb = function (args) {
|
770 | return [args[0] / 100 * 255, args[0] / 100 * 255, args[0] / 100 * 255];
|
771 | };
|
772 |
|
773 | convert.gray.hsl = function (args) {
|
774 | return [0, 0, args[0]];
|
775 | };
|
776 |
|
777 | convert.gray.hsv = convert.gray.hsl;
|
778 |
|
779 | convert.gray.hwb = function (gray) {
|
780 | return [0, 100, gray[0]];
|
781 | };
|
782 |
|
783 | convert.gray.cmyk = function (gray) {
|
784 | return [0, 0, 0, gray[0]];
|
785 | };
|
786 |
|
787 | convert.gray.lab = function (gray) {
|
788 | return [gray[0], 0, 0];
|
789 | };
|
790 |
|
791 | convert.gray.hex = function (gray) {
|
792 | const val = Math.round(gray[0] / 100 * 255) & 0xFF;
|
793 | const integer = (val << 16) + (val << 8) + val;
|
794 |
|
795 | const string = integer.toString(16).toUpperCase();
|
796 | return '000000'.substring(string.length) + string;
|
797 | };
|
798 |
|
799 | convert.rgb.gray = function (rgb) {
|
800 | const val = (rgb[0] + rgb[1] + rgb[2]) / 3;
|
801 | return [val / 255 * 100];
|
802 | }; |
\ | No newline at end of file |