-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (59 loc) · 1.95 KB
/
script.js
File metadata and controls
75 lines (59 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const formatSelect = document.getElementById('format');
const toneSelect = document.getElementById('tone');
const button = document.getElementById('generate-btn');
const palette = document.getElementById('palette');
function randomRGB(tone) {
let min = 0;
let max = 255;
if (tone === "white") {
min = 180;
max = 255;
} else if (tone === "black") {
min = 0;
max = 90;
} else {
min = 0;
max = 255;
}
const r = Math.floor(Math.random() * (max - min) + min);
const g = Math.floor(Math.random() * (max - min) + min);
const b = Math.floor(Math.random() * (max - min) + min);
return { r, g, b };
}
function rgbToHex(r, g, b) {
return "#" + [r, g, b].map((x) => x.toString(16).padStart(2, "0")).join("");
}
function generatePalette() {
palette.innerHTML = "";
for (let i = 0; i < 20; i++) {
const { r, g, b } = randomRGB(toneSelect.value);
let color;
if (formatSelect.value === "hex") {
color = rgbToHex(r, g, b);
}
else {
color = `rgb(${r},${g},${b})`;
}
const div = document.createElement("div");
div.classList.add("color");
div.style.background = `rgb(${r},${g},${b})`;
div.textContent = color;
div.addEventListener("mouseenter", function () {
div.title = "Click to copy " + color;
})
function copyColor() {
navigator.clipboard.writeText(color);
const span = document.createElement("span");
span.textContent = "Copied: " + color;
span.id = "copy-color";
span.style.background = `rgb(${r},${g},${b})`;
document.body.appendChild(span);
setTimeout(() => {
span.remove();
}, 1000);
}
div.addEventListener('click', copyColor);
palette.appendChild(div);
}
}
button.addEventListener('click', generatePalette)