-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (64 loc) · 1.9 KB
/
Copy pathscript.js
File metadata and controls
69 lines (64 loc) · 1.9 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
function HexConv(decimal) {
decimal = Math.floor(decimal);
let rem;
let Hexa = "";
while (decimal > 0) {
rem = decimal % 16;
decimal = Math.floor(decimal / 16);
if (rem == 10) Hexa = "A" + Hexa;
else if (rem == 11) Hexa = "B" + Hexa;
else if (rem == 12) Hexa = "C" + Hexa;
else if (rem == 13) Hexa = "D" + Hexa;
else if (rem == 14) Hexa = "E" + Hexa;
else if (rem == 15) Hexa = "F" + Hexa;
else Hexa = rem + Hexa;
}
return Hexa;
}
function RanNum() {
let ran = Math.random() * 255;
return ran;
}
let newcolor = "";
let colour = document.querySelector(".color-box");
let copy = document.querySelector(".btncopy");
let generate = document.querySelector(".btngen");
let hexget = document.querySelector(".color-box");
generate.addEventListener("click", function () {
newcolor = `#${HexConv(RanNum())}${HexConv(RanNum())}${HexConv(RanNum())}`;
colour.style.backgroundColor = newcolor;
});
copy.addEventListener("click", function () {
if (!newcolor) {
alert("Please generate code first");
return;
}
navigator.clipboard
.writeText(newcolor)
.then(() => {
alert("Hex Code is Copied Successfully");
})
.catch(() => {
alert("!!! Failed to copy the hex code");
});
});
hexget.addEventListener("mouseenter", function () {
let hexcode = document.createElement("div");
hexcode.className = "hexcode";
let phexcode = document.createElement("p");
phexcode.textContent = newcolor;
hexcode.appendChild(phexcode);
hexget.appendChild(hexcode);
});
hexget.addEventListener("mouseleave", function () {
const parent = document.querySelector(".color-box");
const child = parent.querySelector(".hexcode");
if (child) {
parent.removeChild(child);
}
});
const nightToggle = document.querySelector(".night-mode");
nightToggle.addEventListener("click", () => {
nightToggle.classList.toggle("active");
document.body.classList.toggle("dark");
});