-
Notifications
You must be signed in to change notification settings - Fork 0
/
average.js
44 lines (39 loc) · 1.15 KB
/
average.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
import Jimp from 'jimp';
import fs from 'fs';
import path from 'path';
import { makePackFolder, getAllFiles, splitPath, rgba, zip, run } from './util.js';
const processImage = async (inPath, outPath) => {
const img = await Jimp.read(inPath)
let r = g = b = a = pxs = 0n;
for (let x = 0; x < img.getWidth(); ++x) {
for (let y = 0; y < img.getHeight(); ++y) {
let c = img.getPixelColour(x, y);
if (c & 0xff) {
const co = rgba(c);
r += co.r;
g += co.g;
b += co.b;
a += co.a;
++pxs;
}
}
}
if(pxs > 0) {
r /= pxs;
g /= pxs;
b /= pxs;
a /= pxs;
} else {
r = g = b = a = 0n;
}
const c = Number((r << 24n) | (g << 16n) | (b << 8n) | a)
for (let x = 0; x < img.getWidth(); ++x) {
for (let y = 0; y < img.getHeight(); ++y) {
if (img.getPixelColour(x, y) & 0xff) {
img.setPixelColour(c, x, y);
}
}
}
await img.writeAsync(outPath);
}
run('Average', '§6Average Colour of Textures', processImage);