-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetColour.js
More file actions
85 lines (85 loc) · 2.83 KB
/
getColour.js
File metadata and controls
85 lines (85 loc) · 2.83 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
export function getDominantColor(imageUrl) {
const img = new Image();
img.crossOrigin = "anonymous"; // Enable cross-origin requests
img.src = imageUrl;
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixels = imageData.data;
const colorFrequency = {};
// Iterate through pixels to collect colors
for (let i = 0; i < pixels.length; i += 4) {
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
// Filter out near-grays and non-vibrant colors
if (Math.abs(r - g) < 15 && Math.abs(g - b) < 15 && Math.abs(b - r) < 15) continue;
// Convert to HSL and only keep vibrant colors
const {
h, s, l
} = rgbToHsl(r, g, b);
if (s > 0.5 && l > 0.3 && l < 0.7) { // Adjusting brightness for contrast
const rgb = `rgb(${r},${g},${b})`;
colorFrequency[rgb] = (colorFrequency[rgb] || 0) + 1;
}
}
// Find the color with the highest frequency
let bestColor = '';
let maxFrequency = 0;
for (let color in colorFrequency) {
if (colorFrequency[color] > maxFrequency) {
maxFrequency = colorFrequency[color];
bestColor = color;
}
}
// Fallback: if no vibrant color found, set to a default color
if (!bestColor) {
bestColor = "#E0E0E0"; //fallback to a light gray
console.log('Falling back to ', bestColor);
}
// Set the color of the progress bar
const progressBar = document.querySelector('#progress-bar');
if (progressBar) {
progressBar.style.backgroundColor = bestColor;
}
console.log('chosen color:', bestColor);
};
img.onerror = function () {
console.error('Error loading image for color extraction.');
};
}
// Helper function to convert RGB to HSL
export function rgbToHsl(r, g, b) {
r /= 255;
g /= 255;
b /= 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;
}
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;
}
return {
h, s, l
};
}