-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatcolor.js
executable file
·48 lines (44 loc) · 1.22 KB
/
flatcolor.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
/**
*
* This package is based on the Mariam Maarouf's flat-color-generator project.
* Repository: https://github.com/mariamrf/flat-color-generator
*/
module.exports = (h) => {
var PHI = 0.618033988749895;
var s, v;
if(h===undefined){
hue = (Math.floor(Math.random()*(360 - 0 + 1)+0))/360;
h = ( hue + ( hue / PHI )) % 360;
}
else h/=360;
v = Math.floor(Math.random() * (100 - 20 + 1) + 20);
s = (v-10)/100;
v = v/100;
var 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;
}
r = Math.round(r * 255);
g = Math.round(g * 255);
b = Math.round(b * 255);
const finalColor = "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
return {
h: h,
s: s,
v: v,
r: r,
g: g,
b: b,
hex: finalColor
}
}