-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
65 lines (58 loc) · 2.03 KB
/
main.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
const colorCode = document.querySelector(".color-code");
const generateBtn = document.querySelector(".generate-btn");
let hexCodeStr = "";
let oneDigit = "";
let RGBCode = [];
let RGBFirst = 0;
let RGBSecond = 0;
let counter = 0;
generateRandomColor();
generateBtn.addEventListener("click", generateRandomColor);
function generateRandomColor() {
counter = 0;
hexCodeStr = "";
colorCode.textContent = hexCodeStr;
for (let i = 0; i < 6; i++) {
let randomDigit = Math.floor(Math.random() * 16);
if (randomDigit === 15) {
oneDigit = "f";
hexCodeStr += oneDigit;
} else if (randomDigit === 14) {
oneDigit = "e";
hexCodeStr += oneDigit;
} else if (randomDigit === 13) {
oneDigit = "d";
hexCodeStr += oneDigit;
} else if (randomDigit === 12) {
oneDigit = "c";
hexCodeStr += oneDigit;
} else if (randomDigit === 11) {
oneDigit = "b";
hexCodeStr += oneDigit;
} else if (randomDigit === 10) {
oneDigit = "a";
hexCodeStr += oneDigit;
} else {
oneDigit = randomDigit;
hexCodeStr += randomDigit;
}
// Hex to RGB
if (i % 2 === 0) {
RGBFirst = randomDigit;
} else if (i % 2 === 1) {
RGBSecond = randomDigit;
RGBCode[counter] = RGBFirst * (16 ** 1) + RGBSecond * (16 ** 0);
counter++;
}
}
let chooseColorValue = Math.floor(Math.random() * 2);
if (chooseColorValue === 0) {
document.body.style.background = `#${hexCodeStr}`;
colorCode.style.color = `#${hexCodeStr}`;
colorCode.textContent = `#${hexCodeStr}`;
} else if (chooseColorValue === 1) {
document.body.style.background = `rgb(${RGBCode[0]}, ${RGBCode[1]}, ${RGBCode[2]})`;
colorCode.style.color = `rgb(${RGBCode[0]}, ${RGBCode[1]}, ${RGBCode[2]})`;
colorCode.textContent = `rgb(${RGBCode[0]}, ${RGBCode[1]}, ${RGBCode[2]})`;
}
}