From fdb20b47ec05817d0f569502d0a5895dd85f2a1f Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 19 Mar 2023 18:59:22 -0700 Subject: [PATCH] wip aslfkjlfjsdlkjd --- dither-image.js | 54 +++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/dither-image.js b/dither-image.js index 4d19a30..554ae0c 100644 --- a/dither-image.js +++ b/dither-image.js @@ -566,31 +566,45 @@ function decimate(input, palette, n, inputError, y) { console.log('end', decimated); */ - // fitness + // popularity? not really working right // first, dither to the total atari palette while (decimated.length > n) { - 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); + //console.log(y); - let total = 0; - for (let n of fitness) { - total += n; - } - let avg = total / fitness.length; - if (avg < worstFitness) { - worstFitness = avg; - nextPalette = without; + let {popularity, fitness, output} = dither(decimated); + let pops = []; + let fits = []; + pops.fill(0, 0, 256); + fits.fill(0, 0, 256); + for (let i = 0; i < decimated.length; i++) { + let c = decimated[i]; + pops[c] = popularity[i]; + for (let x = 0; x < fitness.length; x++) { + if (output[x] === i) { + fits[c] += fitness[x]; + } } } - decimated = nextPalette; + let metric = (c) => { + let rgb = atariRGB[c]; + let max = Math.max(rgb.r, rgb.g, rgb.b); + let fit = fits[c]; + let pop = pops[c]; + return pop; + } + + let a = decimated.slice(); + // temporarily strip the reserved items + a = a.filter((color) => !keepers[color]); + a = a.filter((color) => popularity[color]); + a.sort((a, b) => { + return metric(b) - metric(a); + }); + console.log(a); + a = reserved.concat(a); + decimated = a.slice(0, n); + //decimated = a.slice(0, decimated.length - 1); + //console.log(decimated); } //console.log('end', decimated);