-
Notifications
You must be signed in to change notification settings - Fork 2
/
hsl.js
77 lines (68 loc) · 1.76 KB
/
hsl.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
import { setAlpha } from "./utils.js";
/**
* @typedef {number[]} hsl hue, saturation, lightness.
*
* All components in the range 0 <= x <= 1
* @see {@link https://en.wikipedia.org/wiki/HSL_and_HSV}
*/
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}
/**
* Updates a color based on HSL values and alpha.
* @alias module:pex-color.fromHSL
* @param {import("./color.js").color} color
* @param {number} h
* @param {number} s
* @param {number} l
* @param {number} [a]
* @returns {import("./color.js").color}
*/
export function fromHSL(color, h, s, l, a) {
if (s === 0) {
color[0] = color[1] = color[2] = l; // achromatic
} else {
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
color[0] = hue2rgb(p, q, h + 1 / 3);
color[1] = hue2rgb(p, q, h);
color[2] = hue2rgb(p, q, h - 1 / 3);
}
return setAlpha(color, a);
}
/**
* Returns a HSL representation of a given color.
* @alias module:pex-color.toHSL
* @param {import("./color.js").color} color
* @param {Array} out
* @returns {hsl}
*/
export function toHSL([r, g, b, a], out = []) {
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
out[2] = (max + min) / 2;
if (max === min) {
out[0] = out[1] = 0; // achromatic
} else {
const d = max - min;
out[1] = out[2] > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
out[0] = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
out[0] = (b - r) / d + 2;
break;
case b:
out[0] = (r - g) / d + 4;
break;
}
out[0] /= 6;
}
return setAlpha(out, a);
}