-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcolour-helper.ts
88 lines (77 loc) · 1.84 KB
/
colour-helper.ts
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
78
79
80
81
82
83
84
85
86
87
88
export const presetColours = {
blue: {
hue: 240,
saturation: 100,
color_temp: 0
},
red: {
hue: 0,
saturation: 100,
color_temp: 0
},
yellow: {
hue: 60,
saturation: 100,
color_temp: 0
},
green: {
hue: 120,
saturation: 100,
color_temp: 0
},
white: {
color_temp: 4500
},
daylightwhite: {
color_temp: 5500
},
warmwhite: {
color_temp: 2700
}
}
export const hexToHsl = (hex: string) => {
if (hex.toLowerCase() === '#000000') return console.error('Cannot set light to black');
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
let r = parseInt(result[1], 16);
let g = parseInt(result[2], 16);
let b = parseInt(result[3], 16);
r /= 255, g /= 255, b /= 255;
let max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if(max == min){
h = s = 0; // achromatic
} else {
let d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
s = s * 100;
s = Math.round(s);
l = l * 100;
l = Math.round(l);
h = Math.round(360 * h);
return {
hue: h,
saturation: s,
brightness: l
};
}
export const temperature = (temp: string) => {
let t = parseInt(temp.slice(0,-1));
if(t<2500||t>6500) return console.error('Colour temperature should be between 2500K and 6500K.');
return{
color_temp: t
};
}
export const getColour = (colour: string) => {
colour = colour.toLowerCase();
if (colour.startsWith('#')) return hexToHsl(colour);
if (colour.endsWith('k')) return temperature(colour);
if (Object.keys(presetColours).includes(colour)) return presetColours[colour];
throw new Error('Invalid Colour');
}