lib8: interpolate: implement blend8 API
This commit is contained in:
parent
facb95c363
commit
64c28b89fe
@ -1,8 +1,13 @@
|
||||
use num::PrimInt;
|
||||
use std::ops::BitOr;
|
||||
|
||||
use rgb::Rgb;
|
||||
|
||||
type Fract8 = u8;
|
||||
|
||||
pub trait Fract8Ops {
|
||||
fn scale8(self, scale: Fract8) -> Self;
|
||||
fn blend8(self, other: Self, scale: Fract8) -> Self;
|
||||
}
|
||||
|
||||
impl Fract8Ops for u8 {
|
||||
@ -10,6 +15,31 @@ impl Fract8Ops for u8 {
|
||||
// borrowed from FastLED
|
||||
(self as u16 * (1 + scale as u16)).unsigned_shr(8) as u8
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
impl Fract8Ops for Rgb<u8> {
|
||||
fn scale8(self, scale: Fract8) -> Self {
|
||||
Rgb::new(
|
||||
self.r.scale8(scale),
|
||||
self.g.scale8(scale),
|
||||
self.b.scale8(scale)
|
||||
)
|
||||
}
|
||||
|
||||
fn blend8(self, other: Self, scale: Fract8) -> Self {
|
||||
match (other.r, other.g, other.b) {
|
||||
(0, 0, 0) => self,
|
||||
_ => Rgb::new(
|
||||
self.r.blend8(other.r, scale),
|
||||
self.g.blend8(other.g, scale),
|
||||
self.b.blend8(other.b, scale)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scale8<T: Fract8Ops>(i: T, scale: Fract8) -> T {
|
||||
|
Loading…
x
Reference in New Issue
Block a user