lib8: fix hsv2rgb implementation by prescaling the hue
This commit is contained in:
parent
be8aa0248d
commit
859177a65c
@ -6,6 +6,8 @@ use palette::encoding::srgb::Srgb;
|
||||
|
||||
use rgb::Rgb;
|
||||
|
||||
use crate::lib8::interpolate::scale8;
|
||||
|
||||
pub trait Rgb8Blend {
|
||||
fn saturating_add<T: Into<Self>>(self, b: T) -> Self where Self: Sized;
|
||||
}
|
||||
@ -32,23 +34,28 @@ impl IntoRgb8 for palette::Hsv<Srgb, u8> {
|
||||
fn into_rgb8(self) -> Rgb<u8> {
|
||||
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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user