1 | /* Convert between RGB and Hue
|
---|
2 | /* ========================================================================== */
|
---|
3 |
|
---|
4 | export function rgb2hue(rgbR, rgbG, rgbB, fallbackhue = 0) {
|
---|
5 | const value = rgb2value(rgbR, rgbG, rgbB);
|
---|
6 | const whiteness = rgb2whiteness(rgbR, rgbG, rgbB);
|
---|
7 | const delta = value - whiteness;
|
---|
8 |
|
---|
9 | if (delta) {
|
---|
10 | // calculate segment
|
---|
11 | const segment = value === rgbR
|
---|
12 | ? (rgbG - rgbB) / delta
|
---|
13 | : value === rgbG
|
---|
14 | ? (rgbB - rgbR) / delta
|
---|
15 | : (rgbR - rgbG) / delta;
|
---|
16 |
|
---|
17 | // calculate shift
|
---|
18 | const shift = value === rgbR
|
---|
19 | ? segment < 0
|
---|
20 | ? 360 / 60
|
---|
21 | : 0 / 60
|
---|
22 | : value === rgbG
|
---|
23 | ? 120 / 60
|
---|
24 | : 240 / 60;
|
---|
25 |
|
---|
26 | // calculate hue
|
---|
27 | const hue = (segment + shift) * 60;
|
---|
28 |
|
---|
29 | return hue;
|
---|
30 | } else {
|
---|
31 | // otherwise return the fallback hue
|
---|
32 | return fallbackhue;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | export function hue2rgb(t1, t2, hue) {
|
---|
37 | // calculate the ranged hue
|
---|
38 | const rhue = hue < 0 ? hue + 360 : hue > 360 ? hue - 360 : hue;
|
---|
39 |
|
---|
40 | // calculate the rgb value
|
---|
41 | const rgb = rhue * 6 < 360
|
---|
42 | ? t1 + (t2 - t1) * rhue / 60
|
---|
43 | : rhue * 2 < 360
|
---|
44 | ? t2
|
---|
45 | : rhue * 3 < 720
|
---|
46 | ? t1 + (t2 - t1) * (240 - rhue) / 60
|
---|
47 | : t1;
|
---|
48 |
|
---|
49 | return rgb;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* RGB tooling
|
---|
53 | /* ========================================================================== */
|
---|
54 |
|
---|
55 | export function rgb2value(rgbR, rgbG, rgbB) {
|
---|
56 | const value = Math.max(rgbR, rgbG, rgbB);
|
---|
57 |
|
---|
58 | return value;
|
---|
59 | }
|
---|
60 |
|
---|
61 | export function rgb2whiteness(rgbR, rgbG, rgbB) {
|
---|
62 | const whiteness = Math.min(rgbR, rgbG, rgbB);
|
---|
63 |
|
---|
64 | return whiteness;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* Math matrix
|
---|
68 | /* ========================================================================== */
|
---|
69 |
|
---|
70 | export function matrix(params, mats) {
|
---|
71 | return mats.map(mat => mat.reduce((acc, value, index) => acc + params[index] * value, 0));
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* D50 reference white
|
---|
75 | /* ========================================================================== */
|
---|
76 |
|
---|
77 | export const [ wd50X, wd50Y, wd50Z ] = [ 96.42, 100, 82.49 ];
|
---|
78 |
|
---|
79 | /* Epsilon
|
---|
80 | /* ========================================================================== */
|
---|
81 |
|
---|
82 | export const epsilon = Math.pow(6, 3) / Math.pow(29, 3);
|
---|
83 |
|
---|
84 | /* Kappa
|
---|
85 | /* ========================================================================== */
|
---|
86 |
|
---|
87 | export const kappa = Math.pow(29, 3) / Math.pow(3, 3);
|
---|