This commit is contained in:
Brooke Vibber 2023-03-18 22:08:22 -07:00
parent 365d968448
commit b74036ef8d

View file

@ -508,9 +508,10 @@ function decimate(input, palette, n, inputError) {
// not happy with this alt yet // not happy with this alt yet
let colorCount = new Int32Array(256); let colorCount = [];
let hues = new Int32Array(16); for (let i = 0; i < 256; i++) {
let lumaByHue = new Int32Array(16); colorCount[i] = 0;
}
const bestColor = (rgb) => { const bestColor = (rgb) => {
let best = -1; let best = -1;
@ -528,26 +529,23 @@ function decimate(input, palette, n, inputError) {
let rgb = inputPixel(x); let rgb = inputPixel(x);
let i = bestColor(rgb); let i = bestColor(rgb);
colorCount[i]++; colorCount[i]++;
let hue = i >> 4;
hues[hue]++;
let luma = i & 0xf;
lumaByHue[hue] = Math.max(luma, lumaByHue[hue]);
} }
let xhues = []; for (let keep of reserved) {
xhues.push.apply(xhues, hues); colorCount[keep] = Infinity;
}
let popularHues = xhues.map((count, hue) => {return {count, hue}}).sort((a, b) => b.count - a.count); let popular = colorCount
decimated = [0]; .map((count, color) => {return {count, color}})
for (let {count, hue} of popularHues) { .filter(({count}) => count > 0)
let luma = lumaByHue[hue]; .sort((a, b) => b.count - a.count)
if (luma > 0 && count > 0) { decimated = [];
decimated.push((hue << 4) | luma); for (let {color} of popular) {
} decimated.push(color);
if (decimated.length == n) { if (decimated.length == n) {
break; break;
} }
} }
decimated.sort((a, b) => a - b);
/* /*