mirror of
https://github.com/LucasDower/ObjToSchematic.git
synced 2025-02-17 13:39:28 +08:00
Alpha-weighted colour averaging
This commit is contained in:
parent
687ed7159a
commit
0b7fa7da36
File diff suppressed because it is too large
Load Diff
@ -37,19 +37,20 @@ export function getAverageColour(image: PNG): RGBA {
|
||||
for (let y = 0; y < image.height; ++y) {
|
||||
const index = 4 * (image.width * y + x);
|
||||
const rgba = image.data.slice(index, index + 4);
|
||||
r += rgba[0];
|
||||
g += rgba[1];
|
||||
b += rgba[2];
|
||||
a += rgba[3];
|
||||
weight += rgba[3];
|
||||
const alpha = rgba[3] / 255;
|
||||
r += (rgba[0] / 255) * alpha;
|
||||
g += (rgba[1] / 255) * alpha;
|
||||
b += (rgba[2] / 255) * alpha;
|
||||
a += alpha;
|
||||
weight += alpha;
|
||||
}
|
||||
}
|
||||
const numPixels = image.width * image.height;
|
||||
return {
|
||||
r: r / (255 * numPixels),
|
||||
g: g / (255 * numPixels),
|
||||
b: b / (255 * numPixels),
|
||||
a: a / (255 * numPixels),
|
||||
r: r / weight,
|
||||
g: g / weight,
|
||||
b: b / weight,
|
||||
a: a / numPixels,
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user