/
index.js
33 lines (29 loc) · 876 Bytes
/
index.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
/**
* @param h hue in range of [0..2π]
* @param c chroma in [0..1]
* @param l luminosity in range of [0..1]
* @returns Array of [r, g, b] values.
*/
export function interpolate(h, c, l) {
// unpack for the lazy
if (Array.isArray(h)) {
[h, c, l] = h;
}
// put red at the right
h = h + (Math.PI / 2 - (Math.PI - 2));
// keep chroma in range for this lightness
if (c > 1 - 2 * Math.abs(l - 0.5)) {
c = 1 - 2 * Math.abs(l - 0.5);
}
const r1 = Math.sin(h + Math.PI - 2.0) * 0.417211 * c + l,
g1 = Math.sin(h + Math.PI + 1.5) * 0.158136 * c + l,
b1 = Math.sin(h + Math.PI) * 0.455928 * c + l;
return [
// red
Math.exp(Math.log(r1 * 0.923166 + 0.0791025) * 1.25) * 255,
// green
Math.exp(Math.log(g1 * 0.923166 + 0.0791025) * 1.25) * 255,
// blue
Math.exp(Math.log(b1 * 0.923166 + 0.0791025) * 1.25) * 255
];
}