lib8: interpolate: optimize some edge cases for blending
This commit is contained in:
parent
3329ad7b89
commit
b21cdef8b0
@ -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 {
|
||||||
|
match scale {
|
||||||
|
0 => 0,
|
||||||
|
255 => self,
|
||||||
|
_ =>
|
||||||
// borrowed from FastLED
|
// borrowed from FastLED
|
||||||
(self as u16 * (1 + scale as u16)).unsigned_shr(8) as u8
|
(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,7 +40,10 @@ 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 => self,
|
||||||
|
255 => other,
|
||||||
|
_ => match (other.r, other.g, other.b) {
|
||||||
(0, 0, 0) => self,
|
(0, 0, 0) => self,
|
||||||
_ => Rgb::new(
|
_ => Rgb::new(
|
||||||
self.r.blend8(other.r, scale),
|
self.r.blend8(other.r, scale),
|
||||||
@ -40,6 +52,7 @@ impl Fract8Ops for Rgb<u8> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scale8<T: Fract8Ops>(i: T, scale: Fract8) -> T {
|
pub fn scale8<T: Fract8Ops>(i: T, scale: Fract8) -> T {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user