-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDarker.js
67 lines (50 loc) · 1.58 KB
/
Darker.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
function darkerColor(hex1, hex2) {
// Convert hex color codes to RGB values
let color1 = hexToRgb(hex1);
let color2 = hexToRgb(hex2);
// Compare the brightness of the two colors
let brightness1 = (color1.r * 299 + color1.g * 587 + color1.b * 114) / 1000;
let brightness2 = (color2.r * 299 + color2.g * 587 + color2.b * 114) / 1000;
if (brightness1 > brightness2) {
return hex2;
} else {
return hex1;
}
}
// Hex to RGB conversion function
function hexToRgb(hex) {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function isDarker(hex1, hex2) {
// Convert hex color codes to RGB values
let color1 = hexToRgb(hex1);
let color2 = hexToRgb(hex2);
// Compare the brightness of the two colors
let brightness1 = (color1.r * 299 + color1.g * 587 + color1.b * 114) / 1000;
let brightness2 = (color2.r * 299 + color2.g * 587 + color2.b * 114) / 1000;
return brightness1 < brightness2;
}
function sortColors(colors) {
colors.sort(function(color1, color2) {
return isDarker(color1, color2);
});
return colors;
}
function setBackgroundColors(colors, ids) {
for (let i = 0; i < ids.length; i++) {
let element = document.getElementById(ids[i]);
element.style.backgroundColor = colors[i];
}
}
module.exports = {
darkerColor,
hexToRgb,
isDarker,
sortColors,
setBackgroundColors,
}