-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (42 loc) · 1.65 KB
/
script.js
File metadata and controls
52 lines (42 loc) · 1.65 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
const generateBtn = document.getElementById("generateBtn");
const copyHexBtn = document.getElementById("copyHexBtn");
const copyRgbBtn = document.getElementById("copyRgbBtn");
const darkModeBtn = document.getElementById("darkModeBtn");
const colorPreview = document.getElementById("colorPreview");
const hexValue = document.getElementById("hexValue");
const rgbValue = document.getElementById("rgbValue");
const copyMsg = document.getElementById("copyMsg");
function getRandomColor() {
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
const hex = `#${r.toString(16).padStart(2, "0")}${g
.toString(16)
.padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
return { hex, rgb: `rgb(${r}, ${g}, ${b})` };
}
function generateColor() {
const { hex, rgb } = getRandomColor();
colorPreview.style.background = hex;
hexValue.textContent = hex;
rgbValue.textContent = rgb;
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text);
copyMsg.textContent = "Copied!";
setTimeout(() => (copyMsg.textContent = ""), 1500);
}
generateBtn.addEventListener("click", generateColor);
copyHexBtn.addEventListener("click", () => copyToClipboard(hexValue.textContent));
copyRgbBtn.addEventListener("click", () => copyToClipboard(rgbValue.textContent));
darkModeBtn.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
darkModeBtn.textContent = "☀️ Light Mode";
}
else {
darkModeBtn.textContent = "🌙 Dark Mode";
}
});
// Generate a random color on page load
generateColor();