lib8: interpolate: optimize some edge cases for blending

This commit is contained in:
Torrie Fischer 2024-12-15 17:48:20 +01:00
parent 3329ad7b89
commit b21cdef8b0

View File

@ -12,12 +12,21 @@ pub trait Fract8Ops {
impl Fract8Ops for u8 { impl Fract8Ops for u8 {
fn scale8(self, scale: Fract8) -> Self { fn scale8(self, scale: Fract8) -> Self {
// borrowed from FastLED match scale {
(self as u16 * (1 + scale as u16)).unsigned_shr(8) as u8 0 => 0,
255 => self,
_ =>
// borrowed from FastLED
(self as u16 * (1 + scale as u16)).unsigned_shr(8) as u8
}
} }
fn blend8(self, other: Self, scale: Fract8) -> Self { fn blend8(self, other: Self, scale: Fract8) -> Self {
((((self as u16).unsigned_shl(8).bitor(other as u16)) as u16).wrapping_add(other as u16 * scale as u16).wrapping_sub(self as u16 * scale as u16)).unsigned_shr(8) as u8 match scale {
0 => self,
255 => other,
_ => ((((self as u16).unsigned_shl(8).bitor(other as u16)) as u16).wrapping_add(other as u16 * scale as u16).wrapping_sub(self as u16 * scale as u16)).unsigned_shr(8) as u8
}
} }
} }
@ -31,13 +40,17 @@ impl Fract8Ops for Rgb<u8> {
} }
fn blend8(self, other: Self, scale: Fract8) -> Self { fn blend8(self, other: Self, scale: Fract8) -> Self {
match (other.r, other.g, other.b) { match scale {
(0, 0, 0) => self, 0 => self,
_ => Rgb::new( 255 => other,
self.r.blend8(other.r, scale), _ => match (other.r, other.g, other.b) {
self.g.blend8(other.g, scale), (0, 0, 0) => self,
self.b.blend8(other.b, scale) _ => Rgb::new(
) self.r.blend8(other.r, scale),
self.g.blend8(other.g, scale),
self.b.blend8(other.b, scale)
)
}
} }
} }
} }