lib8: implement more fastled 8 bit math as lib8 submodules

This commit is contained in:
2024-11-16 12:28:01 +01:00
parent 7d8d59b508
commit b504ce298b
5 changed files with 153 additions and 6 deletions

58
src/lib8/mod.rs Normal file
View File

@ -0,0 +1,58 @@
pub mod interpolate;
pub mod noise;
pub mod trig;
use palette::encoding::srgb::Srgb;
use rgb::RGB8;
pub trait Rgb8Blend {
fn saturating_add(self, b: Self) -> Self;
}
impl Rgb8Blend for RGB8 {
fn saturating_add(self, b: Self) -> Self {
RGB8::new(self.r.saturating_add(b.r), self.g.saturating_add(b.g), self.b.saturating_add(b.b))
}
}
pub trait IntoRgb8 {
fn into_rgb8(self) -> RGB8;
}
impl IntoRgb8 for RGB8 {
fn into_rgb8(self) -> RGB8 {
self
}
}
impl IntoRgb8 for palette::Hsv<Srgb, u8> {
//TODO: Borrowed from FastLED
fn into_rgb8(self) -> RGB8 {
const HSV_SECTION_3: u8 = 0x40;
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 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)
}
}
}