generated from the-pudding/lambda-node-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorSort.js
83 lines (69 loc) · 2.61 KB
/
colorSort.js
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
import Jimp from "jimp";
import {groups} from "d3";
async function getAverageColor(imagePath) {
try {
const image = await Jimp.read(imagePath);
const width = image.bitmap.width;
const height = image.bitmap.height;
const centerX = Math.floor(width / 2);
const centerY = Math.floor(height / 2);
const sampleSize = 400; // You can adjust the sample size as needed
let totalRed = 0, totalGreen = 0, totalBlue = 0, totalAlpha = 0;
let count = 0;
for (let x = centerX - Math.floor(sampleSize / 2); x < centerX + Math.ceil(sampleSize / 2); x++) {
for (let y = centerY - Math.floor(sampleSize / 2); y < centerY + Math.ceil(sampleSize / 2); y++) {
if (x >= 0 && x < width && y >= 0 && y < height) {
const pixelColor = Jimp.intToRGBA(image.getPixelColor(x, y));
totalRed += pixelColor.r;
totalGreen += pixelColor.g;
totalBlue += pixelColor.b;
totalAlpha += pixelColor.a;
count++;
}
}
}
const averageRed = totalRed / count;
const averageGreen = totalGreen / count;
const averageBlue = totalBlue / count;
const averageAlpha = totalAlpha / count;
return {
r: Math.round(averageRed),
g: Math.round(averageGreen),
b: Math.round(averageBlue),
a: Math.round(averageAlpha)
};
} catch (err) {
console.error('Error processing the image:', err);
return null;
}
}
function rgbToHex(r, g, b) {
return `${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase()}`;
}
function getRow(court){
return new Promise(async (resolve, reject) => {
const path = `../../../Volumes/My Passport/raw_images/${court.properties.geo}/google_way_${court.id}.png`
const averageColor = await getAverageColor(path);
court.color = rgbToHex(averageColor.r, averageColor.g, averageColor.b);
console.log(averageColor)
resolve(court);
})
}
export default async function getColor(courts,state){
let toOutput = [];
let chunks = groups(courts, (d,i) => {
return Math.floor(i/500);
})
// console.log(chunks)
for (let chunk of chunks){
await Promise.all(chunk[1].map(async d => {
let court = await getRow(d);
toOutput.push(court)
}));
console.log("promise finished");
}
// // for(let court of courts){
// // }
// console.log(toOutput)
// return toOutput;
}