forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RgbHslConversion.js
85 lines (66 loc) · 1.95 KB
/
RgbHslConversion.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
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
/**
* Given a color in RGB format, convert it to HSL format.
*
* For more info: https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
*
* @param {number[]} colorRgb - One dimensional array of integers (RGB color format).
* @returns {number[]} - One dimensional array of integers (HSL color format).
*
* @example
* const colorRgb = [24, 98, 118]
*
* const result = rgbToHsl(colorRgb)
*
* // The function returns the corresponding color in HSL format:
* // result = [193, 66, 28]
*/
const checkRgbFormat = (colorRgb) => colorRgb.every((c) => c >= 0 && c <= 255)
const rgbToHsl = (colorRgb) => {
if (!checkRgbFormat(colorRgb)) {
throw new Error('Input is not a valid RGB color.')
}
let colorHsl = colorRgb
let red = Math.round(colorRgb[0])
let green = Math.round(colorRgb[1])
let blue = Math.round(colorRgb[2])
const limit = 255
colorHsl[0] = red / limit
colorHsl[1] = green / limit
colorHsl[2] = blue / limit
let minValue = Math.min(...colorHsl)
let maxValue = Math.max(...colorHsl)
let channel = 0
if (maxValue === colorHsl[1]) {
channel = 1
} else if (maxValue === colorHsl[2]) {
channel = 2
}
let luminance = (minValue + maxValue) / 2
let saturation = 0
if (minValue !== maxValue) {
if (luminance <= 0.5) {
saturation = (maxValue - minValue) / (maxValue + minValue)
} else {
saturation = (maxValue - minValue) / (2 - maxValue - minValue)
}
}
let hue = 0
if (saturation !== 0) {
if (channel === 0) {
hue = (colorHsl[1] - colorHsl[2]) / (maxValue - minValue)
} else if (channel === 1) {
hue = 2 + (colorHsl[2] - colorHsl[0]) / (maxValue - minValue)
} else {
hue = 4 + (colorHsl[0] - colorHsl[1]) / (maxValue - minValue)
}
}
hue *= 60
if (hue < 0) {
hue += 360
}
colorHsl[0] = Math.round(hue)
colorHsl[1] = Math.round(saturation * 100)
colorHsl[2] = Math.round(luminance * 100)
return colorHsl
}
export { rgbToHsl }