From cf9c73bf4ad4ce40a20664d81543c4d64877db35 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 31 May 2023 19:51:32 +1200 Subject: [PATCH] Tweak alpha factor in luma sort --- src/reduction/palette.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/reduction/palette.rs b/src/reduction/palette.rs index d84c06d0..d3eefac2 100644 --- a/src/reduction/palette.rs +++ b/src/reduction/palette.rs @@ -88,7 +88,10 @@ pub fn sorted_palette(png: &PngImage) -> Option { enumerated.sort_by(|a, b| { // Sort by ascending alpha and descending luma let color_val = |color: &RGBA8| { - ((color.a as i32) << 18) + let a = i32::from(color.a); + // Put 7 high bits of alpha first, then luma, then low bit of alpha + // This provides notable improvement in images with a lot of alpha + ((a & 0xFE) << 18) + (a & 0x01) // These are coefficients for standard sRGB to luma conversion - i32::from(color.r) * 299 - i32::from(color.g) * 587