-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (84 loc) · 2.81 KB
/
Copy pathscript.js
File metadata and controls
94 lines (84 loc) · 2.81 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
document.addEventListener("DOMContentLoaded", () => {
const fileInput = document.getElementById("file-upload");
const image = document.getElementById("image");
const pickColor = document.getElementById("color-picker");
const hexValue = document.getElementById("hex-value");
const rgbValue = document.getElementById("rgb-value");
const colorPlate = document.getElementById("color-plate");
const hexCopyIcon = document.getElementById("hex-copy");
const rgbCopyIcon = document.getElementById("rgb-copy");
const message = document.getElementById("message");
// Handle file upload and set image source
fileInput.addEventListener("change", (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
image.src = event.target.result;
};
reader.readAsDataURL(file);
}
hexValue.value = "";
rgbValue.value = "";
colorPlate.style.backgroundColor = "#FFFFFF";
});
// Use the Eyedropper API for color picking
pickColor.addEventListener("click", async () => {
if (!("EyeDropper" in window)) {
alert("Eyedropper API is not supported in your browser.");
return;
}
const eyedropper = new EyeDropper();
try {
const result = await eyedropper.open();
const hexColor = result.sRGBHex;
//convert Hex to RGB
const r = parseInt(hexColor.substring(1, 3), 16);
const g = parseInt(hexColor.substring(3, 5), 16);
const b = parseInt(hexColor.substring(5, 7), 16);
const rgbColor = `rgb(${r}, ${g}, ${b})`;
// Display the colors in input fields
rgbValue.value = rgbColor;
hexValue.value = hexColor;
colorPlate.style.backgroundColor = hexColor;
} catch (err) {
console.error("Eyedropper error:", err);
}
});
// Copy Hex value to clipboard
hexCopyIcon.addEventListener("click", () => {
navigator.clipboard
.writeText(hexValue.value)
.then(() => {
if (hexValue.value) {
message.innerText = "HEX value copied";
} else {
message.innerText = "You did not select a color";
}
setTimeout(() => {
message.innerText = "";
}, 3000);
})
.catch((err) => {
message.innerText = "Failed to copy HEX value: " + err;
});
});
// Copy Hex value to clipboard
rgbCopyIcon.addEventListener("click", () => {
navigator.clipboard
.writeText(rgbValue.value)
.then(() => {
if (rgbValue.value) {
message.innerText = "RGB value copied";
} else {
message.innerText = "You did not select a color";
}
setTimeout(() => {
message.innerText = "";
}, 3000);
})
.catch((err) => {
message.innerText = "Failed to copy RGB value: " + err;
});
});
});