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 { fn into_rgb8(self) -> RGB8 { if self.saturation == 0 { return RGB8::new(self.value, self.value, self.value); } let region = self.hue.into_inner() / 43; let remainder = (self.hue.into_inner() - (region * 43)) * 6; 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); 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) } } }