From 859177a65c9e873ceb2525c1c2f1650d0b468a41 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sun, 24 Nov 2024 18:54:25 +0100 Subject: [PATCH] lib8: fix hsv2rgb implementation by prescaling the hue --- src/lib8/mod.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib8/mod.rs b/src/lib8/mod.rs index 36b0c71..a509383 100644 --- a/src/lib8/mod.rs +++ b/src/lib8/mod.rs @@ -6,6 +6,8 @@ use palette::encoding::srgb::Srgb; use rgb::Rgb; +use crate::lib8::interpolate::scale8; + pub trait Rgb8Blend { fn saturating_add>(self, b: T) -> Self where Self: Sized; } @@ -32,23 +34,28 @@ impl IntoRgb8 for palette::Hsv { fn into_rgb8(self) -> Rgb { const HSV_SECTION_3: u8 = 0x40; + if self.saturation == 0 { + return Rgb::new(self.value, self.value, self.value) + } + + let mock_hue = scale8(191, self.hue.into_inner()); let value: u8 = self.value; let saturation: u8 = self.saturation; let invsat: u8 = 255 - saturation; let brightness_floor: u8 = (value as u16 * invsat as u16 / 256) as u8; let color_amplitude: u8 = value - brightness_floor; - let section: u8 = self.hue.into_inner() / HSV_SECTION_3; - let offset: u8 = self.hue.into_inner() % HSV_SECTION_3; + let section: u8 = mock_hue / HSV_SECTION_3; + let offset: u8 = mock_hue % HSV_SECTION_3; let rampup: u8 = offset; let rampdown: u8 = (HSV_SECTION_3 - 1) - offset; - let rampup_amp_adj: u8 = (rampup as u16 * color_amplitude as u16 / (256 / 4)) as u8; - let rampdown_amp_adj: u8 = (rampdown as u16 * color_amplitude as u16 / (256 / 4)) as u8; + let rampup_amp_adj: u8 = (rampup as u16 * color_amplitude as u16 / 64) as u8; + let rampdown_amp_adj: u8 = (rampdown as u16 * color_amplitude as u16 / 64) as u8; - let rampup_adj_with_floor: u8 = rampup_amp_adj + brightness_floor; - let rampdown_adj_with_floor: u8 = rampdown_amp_adj + brightness_floor; + let rampup_adj_with_floor: u8 = rampup_amp_adj.saturating_add(brightness_floor); + let rampdown_adj_with_floor: u8 = rampdown_amp_adj.saturating_add(brightness_floor); match section { 1 => Rgb::new(brightness_floor, rampdown_adj_with_floor, rampup_adj_with_floor),