diff --git a/dither-image.js b/dither-image.js index 1b8f8ee..6999624 100644 --- a/dither-image.js +++ b/dither-image.js @@ -508,9 +508,10 @@ function decimate(input, palette, n, inputError) { // not happy with this alt yet - let colorCount = new Int32Array(256); - let hues = new Int32Array(16); - let lumaByHue = new Int32Array(16); + let colorCount = []; + for (let i = 0; i < 256; i++) { + colorCount[i] = 0; + } const bestColor = (rgb) => { let best = -1; @@ -528,26 +529,23 @@ function decimate(input, palette, n, inputError) { let rgb = inputPixel(x); let i = bestColor(rgb); colorCount[i]++; - let hue = i >> 4; - hues[hue]++; - let luma = i & 0xf; - lumaByHue[hue] = Math.max(luma, lumaByHue[hue]); } - let xhues = []; - xhues.push.apply(xhues, hues); - - 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); - } + for (let keep of reserved) { + colorCount[keep] = Infinity; + } + 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) { break; } } + decimated.sort((a, b) => a - b); /*