From 64c28b89fed789577d66b8482397700c7db3fceb Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sun, 24 Nov 2024 18:55:54 +0100 Subject: [PATCH] lib8: interpolate: implement blend8 API --- src/lib8/interpolate.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib8/interpolate.rs b/src/lib8/interpolate.rs index 72f6e25..0619ad4 100644 --- a/src/lib8/interpolate.rs +++ b/src/lib8/interpolate.rs @@ -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 { + 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(i: T, scale: Fract8) -> T {