-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor Picker from Image.html
More file actions
121 lines (99 loc) · 2.86 KB
/
Copy pathColor Picker from Image.html
File metadata and controls
121 lines (99 loc) · 2.86 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Color Picker from Image</title>
<link rel="icon" href="favicon.png">
<style>
body {
font-family: Arial, sans-serif;
background: #f0f8ff;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
h2 {
color: #007bff;
margin-bottom: 20px;
}
input[type="file"] {
padding: 10px;
border-radius: 6px;
border: 1px solid #ccc;
margin-bottom: 15px;
}
canvas {
max-width: 100%;
width: 300px;
height: auto;
border: 1px solid #ccc;
margin-bottom: 20px;
}
.color-info {
display: flex;
align-items: center;
gap: 10px;
}
.color-box {
width: 40px;
height: 40px;
border: 2px solid #000;
border-radius: 6px;
}
.color-code {
font-weight: bold;
}
</style>
</head>
<body>
<h2>Color Picker from Image</h2>
<input type="file" id="imageInput" accept="image/*" />
<canvas id="canvas"></canvas>
<div class="color-info">
<div class="color-box" id="pickedColor"></div>
<div class="color-code" id="colorCode">Click on image</div>
</div>
<script>
const imageInput = document.getElementById("imageInput");
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const pickedColor = document.getElementById("pickedColor");
const colorCode = document.getElementById("colorCode");
imageInput.addEventListener("change", function () {
const file = this.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
const scale = 300 / img.width; // scale to fit width
const scaledWidth = 300;
const scaledHeight = img.height * scale;
canvas.width = scaledWidth;
canvas.height = scaledHeight;
ctx.drawImage(img, 0, 0, scaledWidth, scaledHeight);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
});
canvas.addEventListener("click", function (e) {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const pixel = ctx.getImageData(x, y, 1, 1).data;
const hex = rgbToHex(pixel[0], pixel[1], pixel[2]);
pickedColor.style.backgroundColor = hex;
colorCode.textContent = `HEX: ${hex} | RGB: (${pixel[0]}, ${pixel[1]}, ${pixel[2]})`;
});
function rgbToHex(r, g, b) {
return "#" + [r, g, b].map(x => {
const hex = x.toString(16);
return hex.length === 1 ? "0" + hex : hex;
}).join("");
}
</script>
</body>
</html>