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);
decimated = [0];
for (let {count, hue} of popularHues) {
let luma = lumaByHue[hue];
if (luma > 0 && count > 0) {
decimated.push((hue << 4) | luma);
} }
let popular = colorCount
.map((count, color) => {return {count, color}})
.filter(({count}) => count > 0)
.sort((a, b) => b.count - a.count)
decimated = [];
for (let {color} of popular) {
decimated.push(color);
if (decimated.length == n) { if (decimated.length == n) {
break; break;
} }
} }
decimated.sort((a, b) => a - b);
/* /*