-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (102 loc) · 3.52 KB
/
script.js
File metadata and controls
126 lines (102 loc) · 3.52 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/** @format */
document.addEventListener("DOMContentLoaded", () => {
const leftButton = document.getElementById("leftButton");
const rightButton = document.getElementById("rightButton");
const genGradButton = document.getElementById("genGradButton");
const copyCode = document.getElementById("copyClipBoard");
const leftAngle = document.getElementById("leftAngle");
const rightAngle = document.getElementById("rightAngle");
const topAngle = document.getElementById("topAngle");
const bottomAngle = document.getElementById("bottomAngle");
const main_div = document.getElementById("main_div");
// 🎨 Generate Random Hex Color
const HexValues = () => {
const hexValue = "0123456789abcdef";
let color = "#";
for (let i = 0; i < 6; i++) {
color += hexValue[Math.floor(Math.random() * 16)];
}
return color;
};
// 🧠 Auto Contrast Text Color
const getContrastColor = (hex) => {
hex = hex.replace("#", "");
let r = parseInt(hex.substring(0, 2), 16);
let g = parseInt(hex.substring(2, 4), 16);
let b = parseInt(hex.substring(4, 6), 16);
let brightness = (r * 299 + g * 587 + b * 114) / 1000;
return brightness > 128 ? "#000" : "#fff";
};
let rgbColor1 = HexValues();
let rgbColor2 = HexValues();
let angle = "right";
// 🎯 Apply Gradient + UI Update
const bgc = () => {
// Left Button
leftButton.innerText = rgbColor1;
leftButton.style.backgroundColor = rgbColor1;
leftButton.style.color = getContrastColor(rgbColor1);
leftButton.style.borderColor = getContrastColor(rgbColor1);
// Right Button
rightButton.innerText = rgbColor2;
rightButton.style.backgroundColor = rgbColor2;
rightButton.style.color = getContrastColor(rgbColor2);
rightButton.style.borderColor = getContrastColor(rgbColor2);
// Gradient Preview
main_div.style.background = `linear-gradient(to ${angle}, ${rgbColor1}, ${rgbColor2})`;
// Optional: Full page background
document.body.style.background = `linear-gradient(to ${angle}, ${rgbColor1}, ${rgbColor2})`;
// Copy Code Text
copyCode.innerText = `background: linear-gradient(to ${angle}, ${rgbColor1}, ${rgbColor2});`;
};
bgc();
// 🎮 Handlers
const leftButtonHandle = () => {
rgbColor1 = HexValues();
bgc();
};
const rightButtonHandle = () => {
rgbColor2 = HexValues();
bgc();
};
const genGradButtonHandle = () => {
rgbColor1 = HexValues();
rgbColor2 = HexValues();
bgc();
};
const copyCodeHandle = () => {
navigator.clipboard.writeText(copyCode.innerText);
const toast = document.createElement("div");
toast.innerText = "✅ Copied!";
toast.className = "toastMsg";
document.body.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 2000);
};
const leftAngleHandle = () => {
angle = "left";
bgc();
};
const rightAngleHandle = () => {
angle = "right";
bgc();
};
const topAngleHandle = () => {
angle = "top";
bgc();
};
const bottomAngleHandle = () => {
angle = "bottom";
bgc();
};
// 🎧 Events
leftButton.addEventListener("click", leftButtonHandle);
rightButton.addEventListener("click", rightButtonHandle);
genGradButton.addEventListener("click", genGradButtonHandle);
copyCode.addEventListener("click", copyCodeHandle);
leftAngle.addEventListener("click", leftAngleHandle);
rightAngle.addEventListener("click", rightAngleHandle);
topAngle.addEventListener("click", topAngleHandle);
bottomAngle.addEventListener("click", bottomAngleHandle);
});