-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
47 lines (35 loc) · 1.46 KB
/
Copy pathmain.js
File metadata and controls
47 lines (35 loc) · 1.46 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
// colorextract.js
const { createCanvas, loadImage } = require('canvas');
async function extractDominantColors(imageUrl, numColors, numSamplesPerRegion) {
return new Promise(async (resolve, reject) => {
try {
const img = await loadImage(imageUrl);
const canvas = createCanvas(img.width, img.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, img.width, img.height);
const pixels = imageData.data;
const regionWidth = Math.floor(img.width / numSamplesPerRegion);
const regionHeight = Math.floor(img.height / numSamplesPerRegion);
const colorCounts = {};
for (let y = 0; y < img.height; y += regionHeight) {
for (let x = 0; x < img.width; x += regionWidth) {
const sampleX = Math.min(x, img.width - 1);
const sampleY = Math.min(y, img.height - 1);
const i = (sampleY * img.width + sampleX) * 4;
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
const color = `rgb(${r},${g},${b})`;
colorCounts[color] = (colorCounts[color] || 0) + 1;
}
}
const sortedColors = Object.keys(colorCounts).sort((a, b) => colorCounts[b] - colorCounts[a]);
const dominantColors = sortedColors.slice(0, numColors);
resolve(dominantColors);
} catch (error) {
reject(error);
}
});
}
module.exports = extractDominantColors;