-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
42 lines (38 loc) · 1.07 KB
/
utils.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
export function hslToHex(h, s, l) {
l /= 100
const a = s * Math.min(l, 1 - l) / 100
const f = n => {
const k = (n + h / 30) % 12
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1)
// convert to Hex and prefix "0" if needed
return Math.round(255 * color).toString(16).padStart(2, '0')
}
return `#${f(0)}${f(8)}${f(4)}`
}
export function rgbToHsl(r, g, b){
r /= 255, g /= 255, b /= 255
var max = Math.max(r, g, b), min = Math.min(r, g, b)
var h, s, l = (max + min) / 2
if(max === min){
h = s = 0 // achromatic
} else {
var 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
}
h *= 100
s *= 100
l *= 100
return [h, s, l]
}
export function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
}