-
-
Notifications
You must be signed in to change notification settings - Fork 812
/
colorspace.html
408 lines (346 loc) · 13.4 KB
/
colorspace.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE html>
<html>
<head>
<title>Color Space Converter</title>
<style>
div{
display:inline-block;
}
</style>
</head>
<body>
<h1>Color Space Converter</h1>
<!-- Input to select an image -->
<input type="file" id="imageInput" accept="image/*">
<div>
<label for="colorSpaceSelect">Select Color Space:</label>
<select id="colorSpaceSelect">
<option value="rgb">RGB</option>
<option value="yuv2bgr">YUV2RGB</option>
<option value="yuv">YUV</option>
<option value="hsl">HSL</option>
<option value="hsv">HSV</option>
<option value="bgr">BGR</option>
<option value="lab">LAB</option>
<option value="hue1">HUE1</option>
<option value="hue2">HUE2</option>
<option value="hue3">HUE3</option>
<option value="hue4">HUE4</option>
<option value="hue5">HUE5</option>
<option value="hue6">HUE6</option>
<!-- Add more color spaces as needed -->
</select>
</div>
<br />
<!-- Canvas to display the original image -->
<div>
original
<canvas id="originalCanvas" width="400" height="400"></canvas>
</div>
<!-- Canvas to display the converted image -->
<div>
converted
<canvas id="convertedCanvas" width="400" height="400"></canvas>
</div>
<script>
function convertRGBtoYUV(imageData) {
const data = imageData.data;
const width = imageData.width;
const height = imageData.height;
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const y = 0.299 * r + 0.587 * g + 0.114 * b;
const u = -0.14713 * r - 0.288862 * g + 0.436 * b;
const v = 0.615 * r - 0.51498 * g - 0.10001 * b;
data[i] = y;
data[i + 1] = u + 128;
data[i + 2] = v + 128;
}
return imageData;
}
function convertRGBtoXYZ(imageData) {
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
const gamma = 2.4;
const rGamma = Math.pow(r, gamma);
const gGamma = Math.pow(g, gamma);
const bGamma = Math.pow(b, gamma);
const x = rGamma * 0.4124564 + gGamma * 0.3575761 + bGamma * 0.1804375;
const y = rGamma * 0.2126729 + gGamma * 0.7151522 + bGamma * 0.0721750;
const z = rGamma * 0.0193339 + gGamma * 0.1191920 + bGamma * 0.9503041;
data[i] = x * 100;
data[i + 1] = y * 100;
data[i + 2] = z * 100;
}
return imageData;
}
function convertRGBtoHSL(imageData) {
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
data[i] = h * 360;
data[i + 1] = s * 100;
data[i + 2] = l * 100;
}
return imageData;
}
function convertRGBtoBGR(imageData) {
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
data[i] = b;
data[i + 1] = g;
data[i + 2] = r;
}
return imageData;
}
function convertRGBtoHSV(imageData) {
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h, s, v = max;
const d = max - min;
s = max === 0 ? 0 : d / max;
if (max === min) {
h = 0; // achromatic
} else {
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
data[i] = h * 360;
data[i + 1] = s * 100;
data[i + 2] = v * 100;
}
return imageData;
}
function convertRGBtoLAB(imageData) {
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i] / 255;
const g = data[i + 1] / 255;
const b = data[i + 2] / 255;
// Apply gamma correction (assumes gamma = 2.2)
const gamma = 2.2;
const rGamma = Math.pow(r, gamma);
const gGamma = Math.pow(g, gamma);
const bGamma = Math.pow(b, gamma);
// Convert RGB to XYZ
const x = rGamma * 0.4124564 + gGamma * 0.3575761 + bGamma * 0.1804375;
const y = rGamma * 0.2126729 + gGamma * 0.7151522 + bGamma * 0.0721750;
const z = rGamma * 0.0193339 + gGamma * 0.1191920 + bGamma * 0.9503041;
// Normalize XYZ to LAB
const epsilon = 0.008856;
const kappa = 903.3;
const xNorm = x / 0.950456;
const yNorm = y / 1.000000;
const zNorm = z / 1.088754;
const fx = xNorm > epsilon ? Math.pow(xNorm, 1 / 3) : (kappa * xNorm + 16) / 116;
const fy = yNorm > epsilon ? Math.pow(yNorm, 1 / 3) : (kappa * yNorm + 16) / 116;
const fz = zNorm > epsilon ? Math.pow(zNorm, 1 / 3) : (kappa * zNorm + 16) / 116;
const L = (116 * fy) - 16;
const A = (fx - fy) * 500;
const B = (fy - fz) * 200;
// Update image data with LAB values
data[i] = L;
data[i + 1] = A;
data[i + 2] = B;
}
return imageData;
}
function adjustHue(imageData, shiftAmount) {
const data = imageData.data;
const shiftRadians = (shiftAmount / 180) * Math.PI; // Convert degrees to radians
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
// Convert RGB to HSL
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
// Shift the hue
h = (h + (shiftRadians / (2 * Math.PI))) % 1;
if (h < 0) {
h += 1;
}
// Convert HSL back to RGB
let rNew, gNew, bNew;
if (s === 0) {
rNew = gNew = bNew = l; // Achromatic
} else {
const hue2rgb = (p, q, t) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
rNew = hue2rgb(p, q, h + 1 / 3);
gNew = hue2rgb(p, q, h);
bNew = hue2rgb(p, q, h - 1 / 3);
}
data[i] = rNew;
data[i + 1] = gNew;
data[i + 2] = bNew;
}
return imageData;
}
function convertYUVImageToRGB(yuvImageData) {
const yuvData = yuvImageData.data;
const rgbData = new Uint8ClampedArray(yuvData.length);
for (let i = 0; i < yuvData.length; i += 4) {
const y = yuvData[i];
const u = yuvData[i + 1] - 128;
const v = yuvData[i + 2] - 128;
// Convert YUV to RGB
const r = Math.max(0, Math.min(255, (298 * y + 409 * v + 128) >> 8));
const g = Math.max(0, Math.min(255, (298 * y - 100 * u - 208 * v + 128) >> 8));
const b = Math.max(0, Math.min(255, (298 * y + 516 * u + 128) >> 8));
// Store RGB values in the new array
rgbData[i] = r;
rgbData[i + 1] = g;
rgbData[i + 2] = b;
rgbData[i + 3] = 255; // Alpha channel (fully opaque)
}
// Create a new RGB image data object
const rgbImageData = new ImageData(rgbData, yuvImageData.width, yuvImageData.height);
return rgbImageData;
}
// Function to handle image selection and conversion
function handleImage() {
const imageInput = document.getElementById("imageInput");
const originalCanvas = document.getElementById("originalCanvas");
const convertedCanvas = document.getElementById("convertedCanvas");
const colorSpaceSelect = document.getElementById("colorSpaceSelect");
const ctxOriginal = originalCanvas.getContext("2d");
const ctxConverted = convertedCanvas.getContext("2d");
const image = new Image();
image.src = URL.createObjectURL(imageInput.files[0]);
image.onload = function () {
// Display the original image
ctxOriginal.drawImage(image, 0, 0, originalCanvas.width, originalCanvas.height);
// Convert the image based on the selected color space
const colorSpace = colorSpaceSelect.value;
if (colorSpace === "rgb") {
// No conversion needed for RGB
ctxConverted.drawImage(image, 0, 0, convertedCanvas.width, convertedCanvas.height);
} else if (colorSpace === "yuv") {
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertRGBtoYUV(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "yuv2bgr") {
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertYUVImageToRGB(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "bgr") {
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertRGBtoBGR(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "lab") {
// Perform LAB conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertRGBtoLAB(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "hsl") {
// Perform HSL conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertRGBtoHSL(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "hsv") {
// Perform HSV conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertRGBtoHSV(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "xyz") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const convertedImageData = convertRGBtoXYZ(imageData);
ctxConverted.putImageData(convertedImageData, 0, 0);
} else if (colorSpace === "hue1") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const adjustedImageData = adjustHue(imageData, 45);
ctxConverted.putImageData(adjustedImageData, 0, 0);
} else if (colorSpace === "hue2") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const adjustedImageData = adjustHue(imageData, 30);
ctxConverted.putImageData(adjustedImageData, 0, 0);
} else if (colorSpace === "hue3") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const adjustedImageData = adjustHue(imageData, -30);
ctxConverted.putImageData(adjustedImageData, 0, 0);
} else if (colorSpace === "hue4") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const adjustedImageData = adjustHue(imageData, -45);
ctxConverted.putImageData(adjustedImageData, 0, 0);
} else if (colorSpace === "hue5") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const adjustedImageData = adjustHue(imageData, 90);
ctxConverted.putImageData(adjustedImageData, 0, 0);
} else if (colorSpace === "hue6") {
// Perform XYZ conversion
const imageData = ctxOriginal.getImageData(0, 0, originalCanvas.width, originalCanvas.height);
const adjustedImageData = adjustHue(imageData, 180);
ctxConverted.putImageData(adjustedImageData, 0, 0);
}
};
}
// Attach the handleImage function to the input change event
const imageInput = document.getElementById("imageInput");
imageInput.addEventListener("change", handleImage);
const colorSpaceSelect = document.getElementById("colorSpaceSelect");
colorSpaceSelect.addEventListener("change", handleImage);
</script>
</body>
</html>