From 1b8f8956809685d0ae9bbb55fd7eab040a68034a Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 9 Mar 2024 23:17:08 +0100 Subject: [PATCH] Switch to nonlinear color difference --- showbits-common/src/widgets/image.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/showbits-common/src/widgets/image.rs b/showbits-common/src/widgets/image.rs index a3474ec..2a517c1 100644 --- a/showbits-common/src/widgets/image.rs +++ b/showbits-common/src/widgets/image.rs @@ -3,7 +3,7 @@ use image::{ RgbaImage, }; use mark::dither::{AlgoFloydSteinberg, AlgoStucki, Algorithm, DiffEuclid, Palette}; -use palette::{IntoColor, LinSrgb, Srgba}; +use palette::{IntoColor, Srgb, Srgba}; use taffy::prelude::{AvailableSpace, Layout, Size}; use crate::Widget; @@ -15,12 +15,12 @@ pub enum DitherAlgorithm { } impl DitherAlgorithm { - fn dither(self, image: RgbaImage, palette: &Palette) -> RgbaImage { + fn dither(self, image: RgbaImage, palette: &Palette) -> RgbaImage { match self { Self::FloydSteinberg => { - >::run(image, palette) + >::run(image, palette) } - Self::Stucki => >::run(image, palette), + Self::Stucki => >::run(image, palette), } } } @@ -31,7 +31,11 @@ pub struct Image { grow: bool, filter: FilterType, - dither_palette: Option>, + // The palette uses Srgb instead of LinSrgb because dithering in Srgb + // produces better results when printed, even though color distance + // calculations are objectively wrong. Maybe the printer's black ink + // behaviour makes them correct again. + dither_palette: Option>, dither_algorithm: DitherAlgorithm, } @@ -66,7 +70,7 @@ impl Image { let palette = palette .iter() .map(|c| c.color.into_color()) - .collect::>(); + .collect::>(); self.dither_palette = Some(Palette::new(palette)); self