-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (43 loc) · 1.24 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const constructColor = hexString => {
const hex = hexString.replace(/#/g, '');
/* Get the RGB values to calculate the Hue. */
const r = parseInt(hex.substring(0, 2), 16) / 255;
const g = parseInt(hex.substring(2, 4), 16) / 255;
const b = parseInt(hex.substring(4, 6), 16) / 255;
/* Getting the Max and Min values for Chroma. */
const max = Math.max.apply(Math, [r, g, b]);
const min = Math.min.apply(Math, [r, g, b]);
/* Variables for HSV value of hex color. */
let chr = max - min;
let hue = 0;
let val = max;
let sat = 0;
if (val > 0) {
/* Calculate Saturation only if Value isn't 0. */
sat = chr / val;
if (sat > 0) {
if (r === max) {
hue = 60 * ((g - min - (b - min)) / chr);
if (hue < 0) {
hue += 360;
}
} else if (g === max) {
hue = 120 + 60 * ((b - min - (r - min)) / chr);
} else if (b === max) {
hue = 240 + 60 * ((r - min - (g - min)) / chr);
}
}
}
const colorObj = {};
colorObj.hue = hue;
colorObj.hex = hexString;
return colorObj;
};
export default sortColorsByHue = colors => {
return colors
.map(color => constructColor(color))
.sort((a, b) => {
return a.hue - b.hue;
})
.map(color => color.hex);
};