From 9df948721c7fbee4414523e28d3f4d26b8109995 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 21 Mar 2023 05:04:17 -0700 Subject: [PATCH] wip --- dither-image.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/dither-image.js b/dither-image.js index 49d7ef4..c6f2348 100644 --- a/dither-image.js +++ b/dither-image.js @@ -131,6 +131,10 @@ class RGB { this.g * this.g + this.b * this.b; } + + luma() { + return this.r * 0.299 + this.g * 0.587 + this.b * 0.114; + } } const maxDist = (new RGB(255, 255, 255)).magnitude(); @@ -576,12 +580,22 @@ function decimate(input, palette, n, inputError, y) { // .reduce((acc, rgb) => acc.inc(rgb), new RGB(0, 0, 0)) // .divide(bucket.length); - // Take the brightest color in the bucket + // Take the channel-brightest color in the bucket //let rgb = bucket[bucket.length - 1]; + // Take the luma-brightest color in the bucket + //let rgb = bucket.slice().sort((a, b) => b.luma() - a.luma())[bucket.length - 1]; + // Take the median color in the bucket let rgb = bucket[bucket.length >> 1]; + // Take the luma-median color in the bucket + //let rgb = bucket.slice().sort((a, b) => b.luma() - a.luma())[bucket.length >> 1]; + + // Take the brightest-channel median + //let rgb = bucket.slice() + // .sort((a, b) => Math.max(b.r, b.g, b.b) - Math.max(a.r, b.g, b.b))[bucket.length >> 1]; + // And map into the Atari palette let dists = palette.map(( i) => rgb.difference(atariRGB[i]).magnitude()); let closest = Math.min(...dists);