-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (67 loc) · 1.8 KB
/
app.js
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
const hexCodesInput = document.getElementById("hex-codes");
const colorGrid = document.getElementById("color-grid");
const visualiseBtn = document.getElementById("visualise-btn");
visualiseBtn.addEventListener("click", () => {
let hexInput = hexCodesInput.value.replace(
/^[^A-Za-z0-9#]+|[^A-Za-z0-9]+$/g,
""
);
const hexCodes = hexInput
.split(/[\n,:]+/)
.map((code) => code.trim())
.filter(Boolean);
colorGrid.innerHTML = "";
hexCodes.forEach((hexCode) => {
const colorElement = document.createElement("div");
colorElement.classList.add(
"relative",
"w-40",
"h-40",
"min-w-fit",
"min-h-fit",
"rounded-md",
"overflow-hidden",
"transition",
"duration-200",
"transform",
"hover:scale-110",
"hover:shadow-md",
"flex",
"items-center",
"justify-center"
);
colorElement.style.backgroundColor = hexCode;
const overlayElement = document.createElement("div");
overlayElement.classList.add(
"text-base",
"text-white",
"mix-blend-difference",
"absolute",
"inset-0",
"flex",
"items-center",
"justify-center",
"text-white",
"text-xl",
"font-bold",
"text-center",
"bg-black",
"bg-opacity-50",
"opacity-0",
"hover:opacity-100",
"transition",
"duration-200",
"ease-in-out"
);
overlayElement.classList.add("opacity-0", "hover:opacity-100");
overlayElement.textContent = `${hexCode}`;
colorElement.appendChild(overlayElement);
colorGrid.appendChild(colorElement);
});
});
function rgbFromHex(hex) {
const r = parseInt(hex.substring(1, 3), 16);
const g = parseInt(hex.substring(3, 5), 16);
const b = parseInt(hex.substring(5, 7), 16);
return `rgb(${r}, ${g}, ${b})`;
}