pub mod interpolate; pub mod noise; pub mod trig; use palette::encoding::srgb::Srgb; use rgb::Rgb; pub trait Rgb8Blend { fn saturating_add>(self, b: T) -> Self where Self: Sized; } impl Rgb8Blend for Rgb { fn saturating_add>(self, b: T) -> Self where Self: Sized { let rgb = b.into(); Rgb::new(self.r.saturating_add(rgb.r), self.g.saturating_add(rgb.g), self.b.saturating_add(rgb.b)) } } pub trait IntoRgb8 { fn into_rgb8(self) -> Rgb; } impl IntoRgb8 for Rgb { fn into_rgb8(self) -> Rgb { self } } impl IntoRgb8 for palette::Hsv { //TODO: Borrowed from FastLED fn into_rgb8(self) -> Rgb { 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 => Rgb::new(brightness_floor, rampdown_adj_with_floor, rampup_adj_with_floor), 0 => Rgb::new(rampdown_adj_with_floor, rampup_adj_with_floor, brightness_floor), _ => Rgb::new(rampup_adj_with_floor, brightness_floor, rampdown_adj_with_floor) } } }