-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
69 lines (58 loc) · 2.63 KB
/
utility.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
const fs = require('fs');
const PNG = require('pngjs').PNG;
const XMLHttpRequest = require('xhr2');
const INPUT_IMAGE_PATH = 'input.png'; // Path to the input PNG file
const OUTPUT_IMAGE_PATH = 'output.png'; // Path to the output PNG file
const ZPLACE_API_URL = "https://place-api.zevent.fr/graphql";
function hexToRgb(hex) {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function getDistance(color1, r, g, b) {
return Math.sqrt(Math.pow(color1.r - r, 2) + Math.pow(color1.g - g, 2) + Math.pow(color1.b - b, 2));
}
function getClosestColorIndex(colorsRGB, rgb) {
let closestColor = colorsRGB.reduce((prev, curr) => {
return (getDistance(prev, rgb.r, rgb.g, rgb.b) < getDistance(curr, rgb.r, rgb.g, rgb.b) ? prev : curr);
});
return colorsRGB.findIndex((color) => {
return color.r === closestColor.r && color.g === closestColor.g && color.b === closestColor.b;
});
}
function parseColors(callback) {
let xhrColor = new XMLHttpRequest();
xhrColor.open("POST", ZPLACE_API_URL);
xhrColor.setRequestHeader("Accept", "application/json");
xhrColor.setRequestHeader("Content-Type", "application/json");
xhrColor.send('{"operationName":"getAvailableColors","variables":{},"query":"query getAvailableColors {\\n getAvailableColors {\\n colorCode\\n name\\n __typename\\n }\\n}"}');
xhrColor.onreadystatechange = function () {
if (xhrColor.readyState === 4) {
let colors = JSON.parse(xhrColor.responseText).data.getAvailableColors;
let colorsRGB = colors.map((color) => hexToRgb(color.colorCode));
callback(colorsRGB);
}
};
}
function modifyImageColors(colorsRGB) {
fs.createReadStream(INPUT_IMAGE_PATH)
.pipe(new PNG())
.on('parsed', function () {
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
let idx = (this.width * y + x) << 2;
let rgb = { r: this.data[idx], g: this.data[idx + 1], b: this.data[idx + 2] };
let closestColorIndex = getClosestColorIndex(colorsRGB, rgb);
let closestColor = colorsRGB[closestColorIndex];
this.data[idx] = closestColor.r;
this.data[idx + 1] = closestColor.g;
this.data[idx + 2] = closestColor.b;
}
}
this.pack().pipe(fs.createWriteStream(OUTPUT_IMAGE_PATH));
});
}
parseColors(modifyImageColors);