lib8: rewrite hsv2rgb implementation based on fastled

This commit is contained in:
Victoria Fischer 2024-11-16 11:39:28 +01:00
parent 67f1c995ce
commit 3a8dd89828

View File

@ -23,25 +23,32 @@ impl IntoRgb8 for RGB8 {
}
impl IntoRgb8 for palette::Hsv<Srgb, u8> {
//TODO: Borrowed from FastLED
fn into_rgb8(self) -> RGB8 {
if self.saturation == 0 {
return RGB8::new(self.value, self.value, self.value);
}
const HSV_SECTION_3: u8 = 0x40;
let region = self.hue.into_inner() / 43;
let remainder = (self.hue.into_inner() - (region * 43)) * 6;
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 p = self.value.wrapping_mul(255 - self.saturation).wrapping_shr(8);
let q = (self.value.wrapping_mul(255 - ((self.saturation.wrapping_mul(remainder)).wrapping_shr(8)))).wrapping_shr(8);
let t = (self.value.wrapping_mul(255 - ((self.saturation.wrapping_mul(255 - remainder)).wrapping_shr(8)))).wrapping_shr(8);
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;
match region {
0 => RGB8::new(self.value, t, p),
1 => RGB8::new(q, self.value, p),
2 => RGB8::new(p, self.value, t),
3 => RGB8::new(p, q, self.value),
4 => RGB8::new(t, p, self.value),
_ => RGB8::new(self.value, p, q)
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_adj_with_floor: u8 = rampup_amp_adj + brightness_floor;
let rampdown_adj_with_floor: u8 = rampdown_amp_adj + brightness_floor;
match section {
1 => RGB8::new(brightness_floor, rampdown_adj_with_floor, rampup_adj_with_floor),
0 => RGB8::new(rampdown_adj_with_floor, rampup_adj_with_floor, brightness_floor),
_ => RGB8::new(rampup_adj_with_floor, brightness_floor, rampdown_adj_with_floor)
}
}
}