UNPKG

4.3 kBJavaScriptView Raw
1/**
2 * Converts an RGB color value to HSL. Conversion formula
3 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
4 * Assumes r, g, and b are contained in the set [0, 255] and
5 * returns h, s, and l in the set [0, 1].
6 * https://gist.github.com/mjackson/5311256
7 *
8 * @param Number r The red color value
9 * @param Number g The green color value
10 * @param Number b The blue color value
11 * @return Array The HSL representation
12 */
13
14function rgbToHsl(r, g, b) {
15 r /= 255, g /= 255, b /= 255;
16
17 let max = Math.max(r, g, b), min = Math.min(r, g, b);
18 let h, s, l = (max + min) / 2;
19
20 if (max == min) {
21 h = s = 0; // achromatic
22 } else {
23 let d = max - min;
24 s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
25
26 switch (max) {
27 case r: h = (g - b) / d + (g < b ? 6 : 0); break;
28 case g: h = (b - r) / d + 2; break;
29 case b: h = (r - g) / d + 4; break;
30 }
31
32 h /= 6;
33 }
34
35 // return [h, s, l];
36 // return [h * 360, s * 100, l * 100];
37 return [Math.round(h * 360), Math.round(s * 100), Math.round(l * 100)];
38}
39
40/**
41 * Converts an HSL color value to RGB. Conversion formula
42 * adapted from http://en.wikipedia.org/wiki/HSL_color_space.
43 * Assumes h, s, and l are contained in the set [0, 1] and
44 * returns r, g, and b in the set [0, 255].
45 *
46 * @param Number h The hue
47 * @param Number s The saturation
48 * @param Number l The lightness
49 * @return Array The RGB representation
50 */
51function hslToRgb(h, s, l) {
52 h = h / 360;
53 s = s / 100;
54 l = l / 100;
55
56 let r, g, b;
57
58 if (s == 0) {
59 r = g = b = l; // achromatic
60 } else {
61
62 let q = l < 0.5 ? l * (1 + s) : l + s - l * s;
63 let p = 2 * l - q;
64
65 r = hue2rgb(p, q, h + 1 / 3);
66 g = hue2rgb(p, q, h);
67 b = hue2rgb(p, q, h - 1 / 3);
68 }
69
70 // return [r * 255, g * 255, b * 255];
71 return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
72}
73
74/**
75 * Converts an RGB color value to HSV. Conversion formula
76 * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
77 * Assumes r, g, and b are contained in the set [0, 255] and
78 * returns h, s, and v in the set [0, 1].
79 *
80 * @param Number r The red color value
81 * @param Number g The green color value
82 * @param Number b The blue color value
83 * @return Array The HSV representation
84 */
85function rgbToHsv(r, g, b) {
86 r /= 255, g /= 255, b /= 255;
87
88 let max = Math.max(r, g, b), min = Math.min(r, g, b);
89 let h, s, v = max;
90
91 let d = max - min;
92 s = max == 0 ? 0 : d / max;
93
94 if (max == min) {
95 h = 0; // achromatic
96 } else {
97 switch (max) {
98 case r: h = (g - b) / d + (g < b ? 6 : 0); break;
99 case g: h = (b - r) / d + 2; break;
100 case b: h = (r - g) / d + 4; break;
101 }
102
103 h /= 6;
104 }
105
106 // return [h, s, v];
107 // return [h * 360, s * 100, v * 100];
108 return [Math.round(h * 360), Math.round(s * 100), Math.round(v * 100)];
109}
110
111/**
112 * Converts an HSV color value to RGB. Conversion formula
113 * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
114 * Assumes h, s, and v are contained in the set [0, 1] and
115 * returns r, g, and b in the set [0, 255].
116 *
117 * @param Number h The hue
118 * @param Number s The saturation
119 * @param Number v The value
120 * @return Array The RGB representation
121 */
122function hsvToRgb(h, s, v) {
123 h = h / 360;
124 s = s / 100;
125 v = v / 100;
126
127 let r, g, b;
128 let i = Math.floor(h * 6);
129 let f = h * 6 - i;
130 let p = v * (1 - s);
131 let q = v * (1 - f * s);
132 let t = v * (1 - (1 - f) * s);
133
134 switch (i % 6) {
135 case 0: r = v, g = t, b = p; break;
136 case 1: r = q, g = v, b = p; break;
137 case 2: r = p, g = v, b = t; break;
138 case 3: r = p, g = q, b = v; break;
139 case 4: r = t, g = p, b = v; break;
140 case 5: r = v, g = p, b = q; break;
141 }
142
143 // return [r * 255, g * 255, b * 255];
144 return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
145}
146
147/**
148 *
149 * @param {*} p
150 * @param {*} q
151 * @param {*} t
152 */
153function hue2rgb(p, q, t) {
154 if (t < 0) t += 1;
155 if (t > 1) t -= 1;
156 if (t < 1 / 6) return p + (q - p) * 6 * t;
157 if (t < 1 / 2) return q;
158 if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
159 return p;
160}
161
162module.exports = {
163 rgbToHsl,
164 hslToRgb,
165 rgbToHsv,
166 hsvToRgb
167};
\No newline at end of file