-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (52 loc) · 2.06 KB
/
script.js
File metadata and controls
57 lines (52 loc) · 2.06 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
const pickBtn = document.getElementById("pick-btn");
const fileInput = document.getElementById("file");
const image = document.getElementById("image");
const hexInput = document.getElementById("hex-input");
const rgbInput = document.getElementById("rgb-input");
const pickedColor = document.getElementById("picked-color");
// Initialize Eyedropper if supported
const initEyeDropper = () => {
if ("EyeDropper" in window) {
pickBtn.classList.remove("hide");
const eyeDropper = new EyeDropper();
// Event listener for color selection
pickBtn.addEventListener("click", async () => {
try {
const colorValue = await eyeDropper.open();
// Convert colorValue.sRGBHex to lowercase to ensure propper parsing
const hexValue = colorValue.sRGBHex.toLowerCase();
const rgbValue = hexToRgb(hexValue);
result.style.display = "grid";
hexInput.value = hexValue;
rgbInput.value = rgbValue;
pickedColor.style.backgroundColor = hexValue;
} catch {
alert("Your browser doesn't support Eyedropper Api!");
}
});
} else {
alert("Your browser doesn't support Eyedropper Api!");
}
};
// Event listener for file input
fileInput.addEventListener("change", () => {
result.style.display = "none";
const reader = new FileReader();
reader.onload = () => image.setAttribute("src", reader.result);
reader.readAsDataURL(fileInput.files[0]);
});
// Function to copy text to clipboard
const copyToClipboard = (textId) => {
const textElement = document.getElementById(textId);
textElement.select();
document.execCommand("copy");
};
// RGB conversion function
const hexToRgb = (hex) => {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return `rgb(${r}, ${g}, ${b})`;
};
// Initialize Eyedropper
window.onload = initEyeDropper;