From 89db2f3d1d15c19a58ee600f11f9ba121353ab7b Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 19 Mar 2023 18:29:06 -0700 Subject: [PATCH] wip in progress --- dither-image.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/dither-image.js b/dither-image.js index 5b2756b..4d19a30 100644 --- a/dither-image.js +++ b/dither-image.js @@ -566,23 +566,36 @@ function decimate(input, palette, n, inputError, y) { console.log('end', decimated); */ - // popularity? not really working right + // fitness // first, dither to the total atari palette - /* while (decimated.length > n) { - //console.log(y); - let {popularity} = dither(decimated); - let a = decimated; - // temporarily strip the reserved items - a = a.filter((color) => !keepers[color]); - a = a.sort((a, b) => popularity[b] - popularity[a]); - a = reserved.concat(a); - decimated = a.slice(0, n); - //console.log(decimated); + console.log(`y ${y}`); + let worstFitness = Infinity; + let nextPalette = null; + for (let i = 0; i < decimated.length; i++) { + if (keepers[i]) { + continue; + } + let without = decimated.slice(); + without.splice(i, 1); + let {popularity, fitness} = dither(decimated); + + let total = 0; + for (let n of fitness) { + total += n; + } + let avg = total / fitness.length; + if (avg < worstFitness) { + worstFitness = avg; + nextPalette = without; + } + } + decimated = nextPalette; } //console.log('end', decimated); - */ + // old algo + /* while (decimated.length > n) { let {popularity, fitness, output} = dither(decimated); @@ -625,6 +638,7 @@ function decimate(input, palette, n, inputError, y) { }); } } + */ // Palette fits return dither(decimated);