-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
54 lines (47 loc) · 1.61 KB
/
app.js
File metadata and controls
54 lines (47 loc) · 1.61 KB
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
const colorDisplay = document.querySelector("#color");
const newColorBtn = document.querySelector("#generator");
let showRGB = true;
let rgbColor;
let hexColor;
colorDisplay.addEventListener("dblclick", () => {
if (showRGB) {
colorDisplay.innerText = hexColor;
showRGB = false;
} else {
colorDisplay.innerText = rgbColor;
showRGB = true;
}
});
newColorBtn.addEventListener("click", () => {
const newRGBValues = randomColor();
const newRGBColor = `rgb(${newRGBValues.r}, ${newRGBValues.g}, ${newRGBValues.b})`;
rgbColor = newRGBColor;
const newHexColor = rgbToHex(newRGBValues.r, newRGBValues.g, newRGBValues.b);
hexColor = newHexColor;
if (showRGB) {
colorDisplay.innerText = newRGBColor;
} else {
colorDisplay.innerText = newHexColor;
}
document.body.style.backgroundColor = newRGBColor;
newColorBtn.style.color = newRGBColor;
});
const randomColor = () => {
let randomR = Math.floor(Math.random() * 256);
let randomG = Math.floor(Math.random() * 256);
let randomB = Math.floor(Math.random() * 256);
return {r: randomR, g: randomG, b: randomB};
}
const rgbToHex = (r, g, b) => {
// Ensure the RGB values are within valid ranges (0-255)
r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));
// Convert each RGB component to a hexadecimal string
const rHex = r.toString(16).padStart(2, '0');
const gHex = g.toString(16).padStart(2, '0');
const bHex = b.toString(16).padStart(2, '0');
// Concatenate the hexadecimal values to form the final hex color
const hexColor = `#${rHex}${gHex}${bHex}`;
return hexColor;
}