lib8: interpolate: provide a trait to handle fract8 operations

This commit is contained in:
Victoria Fischer 2024-11-23 14:51:27 +01:00
parent 9dff0119a4
commit 5fed51fbd1

View File

@ -1,8 +1,19 @@
use num::PrimInt;
pub fn scale8(i: u8, scale: u8) -> u8 {
type Fract8 = u8;
pub trait Fract8Ops {
fn scale8(self, scale: Fract8) -> Self;
}
impl Fract8Ops for u8 {
fn scale8(self, scale: Fract8) -> Self {
// borrowed from FastLED
(i as u16 * scale as u16).wrapping_shr(8) as u8
(self as u16 * (1 + scale as u16)).unsigned_shr(8) as u8
}
}
pub fn scale8<T: Fract8Ops>(i: T, scale: Fract8) -> T {
i.scale8(scale)
}
pub fn avg7(i: i8, j: i8) -> i8 {