From 94d1e465c9e28b98483a992efc41dea52cc5de92 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Tue, 12 Jan 2021 20:27:42 -0500 Subject: [PATCH] rewrite random noise knowing how bayer works --- color_quant.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/color_quant.go b/color_quant.go index f32e433..1724c87 100644 --- a/color_quant.go +++ b/color_quant.go @@ -48,15 +48,12 @@ func naivePalette(p color.Palette) quantizerFunction { // randomNoisePalette injects random noise into the quantization step func randomNoisePalette(p color.Palette) quantizerFunction { + r := len(p) return func(_, _ int, c color.Color) color.Color { - // the randomization here is tuned for the sixteen-color palette for now. // I think the proper theory here is probably "only try and randomize within one palette swatch in either direction". // it might be possible to instead permute the color selected _from the palette_ (i.e. modify the result of p.Index(c))... // ...but I think for that to work you'd need a proper "ordering" for the colors. - noise := rand.Intn(64) - 32 - if noise < 0 { - noise = 0 - } + noise := rand.Intn(256) / r rc := permuteColor(c, uint8(noise)) return p[p.Index(rc)] }