wip back to old method about to change it

This commit is contained in:
Brooke Vibber 2023-03-18 23:19:01 -07:00
parent 1d77bb3b22
commit bc354d9f05

View file

@ -27,6 +27,10 @@ class RGB {
this.b = b; this.b = b;
} }
clone() {
return new RGB(this.r, this.g, this.b);
}
static fromHex(val) { static fromHex(val) {
let r = (val >>> 16) & 0xff; let r = (val >>> 16) & 0xff;
let g = (val >>> 8) & 0xff; let g = (val >>> 8) & 0xff;
@ -408,7 +412,7 @@ function decimate(input, palette, n, inputError) {
let width = input.length; let width = input.length;
let inputPixel = (x, error) => { let inputPixel = (x, error) => {
let rgb = input[x]; let rgb = input[x].clone();
if (error) { if (error) {
rgb.add(error.cur[x]); rgb.add(error.cur[x]);
} }
@ -495,7 +499,7 @@ function decimate(input, palette, n, inputError) {
//decimated = [0, 0x36, 0x0f, 0x86]; //decimated = [0, 0x36, 0x0f, 0x86];
let reserved = [0]; // black let reserved = [0]; // black
reserved = [0, 15]; // black, white //reserved = [0, 15]; // black, white
//reserved = [0, 5, 10, 15]; // grayscale //reserved = [0, 5, 10, 15]; // grayscale
//reserved = [0, 0x48, 0x78, 15]; // vaporwave //reserved = [0, 0x48, 0x78, 15]; // vaporwave
//reserved = [0, 0x3c, 0x78, 15]; // red/blue/white //reserved = [0, 0x3c, 0x78, 15]; // red/blue/white
@ -505,57 +509,13 @@ function decimate(input, palette, n, inputError) {
keepers[i & 0xfe] = 1; // drop that 0 luminance bit! keepers[i & 0xfe] = 1; // drop that 0 luminance bit!
} }
// not happy with this alt yet
let colorCount = [];
for (let i = 0; i < 256; i++) {
colorCount[i] = 0;
}
const bestColor = (rgb) => {
let best = -1;
let closest = Infinity;
for (let i = 0; i < 256; i += 2) {
let distance = rgb.difference(atariRGB[i]).magnitude();
if (distance < closest) {
closest = distance;
best = i;
}
}
return best;
};
for (let x = 0; x < width; x++ ) {
let rgb = inputPixel(x);
let i = bestColor(rgb);
colorCount[i]++;
}
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 = decimated.sort((a, b) => a - b);
/*
while (decimated.length > n) { while (decimated.length > n) {
let {popularity, fitness, output} = dither(decimated); let {popularity, fitness, output} = dither(decimated);
// Try dropping least used color on each iteration // Try dropping least used color on each iteration
let least = Infinity; let least = Infinity;
let pick = -1; let pick = -1;
for (let i = 0; i < decimated.length; i++) { for (let i = 1; i < decimated.length; i++) {
let color = decimated[i]; let color = decimated[i];
if (keepers[color]) { if (keepers[color]) {
continue; continue;
@ -587,7 +547,6 @@ function decimate(input, palette, n, inputError) {
throw new Error('this should not happen'); throw new Error('this should not happen');
} }
} }
*/
// Palette fits // Palette fits
return dither(decimated); return dither(decimated);