-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelementRandomColor.js
67 lines (57 loc) · 1.89 KB
/
elementRandomColor.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
const disclaimer = "I did not create this ; Origin = https://cassidoo.co/script.js"
const colorsLight =
[
"rgb(0, 54, 150)", //blue
"rgb(0, 98, 106)", //teal
"rgb(138, 71, 0)", //orange
"rgb(174, 0, 122)", //pink
"rgb(35, 106, 0)", //green
"rgb(135, 0, 0)" //red
]
const colorsDark =
[
"rgb(139, 168, 220)", //blue
"#24a6b0", //teal
"#cb8736", //orange
"rgb(199, 177, 0)", //yellow
"rgb(76, 191, 20)", //green
"rgb(229, 145, 156)" //red
]
function setRandomElementColor() {
let currentTheme = localStorage.getItem("data-theme") || "default";
elements = Array.from(document.querySelectorAll("a, h1, h2, h3, h4, h5, strong, .specialButton .randomcolor"))
if (currentTheme == "light"){
shuffle(colorsLight)
} else {
shuffle(colorsDark)
}
for (let index = 0; index < elements.length; index++) { // Faster than foreach
elements[index].style.color = getColorRoundRobin(currentTheme, index);
}
}
function shuffle(array) {
let currentIndex = array.length;
// While there remain elements to shuffle...
while (currentIndex != 0) {
// Pick a remaining element...
let randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
}
function getColorRoundRobin(theme="light", index) {
if (theme == "light"){
return colorsLight[index % colorsLight.length];
} else {
return colorsDark[index % colorsDark.length];
}
}
function setColorHoverListener() {
Array.from(document.querySelectorAll("a, button")).forEach((e) => {
e.addEventListener("mouseover", setRandomElementColor);
});
}
setRandomElementColor();
setColorHoverListener();