-
Notifications
You must be signed in to change notification settings - Fork 3
/
HsvToRgb.js
52 lines (48 loc) · 1.11 KB
/
HsvToRgb.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
/* accepts parameters
* h, hue, 0.0 to 1.0
* s, saturation, 0.0 to 1.0
* v, lightness or brightness, 0.0 to 1.0
*/
'use strict';
function hsv2rgb(h, s, v) {
h = bound(h);
s = bound(s);
v = bound(v);
let r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}
function bound(value){
if (value < 0) {
value = 0;
}
if (value >1) {
value = 1;
}
return value;
}
module.exports = {
getRGB: function(h,s,v): ?Object {
return hsv2rgb(h,s,v);
},
getRGBString: function(h,s,v): ?Object {
let rgb = hsv2rgb(h,s,v);
return 'rgb(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ')';
}
};