From 448ce11dbc80a4a49984d54da1ba37b8f6729f77 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 30 Apr 2023 16:41:38 -0400 Subject: [PATCH] add color interpolation for ARGBu8x4 --- ggdt/src/graphics/color.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ggdt/src/graphics/color.rs b/ggdt/src/graphics/color.rs index 17095e0..119c2bc 100644 --- a/ggdt/src/graphics/color.rs +++ b/ggdt/src/graphics/color.rs @@ -610,6 +610,10 @@ impl ARGBu8x4 { self.0.to_array() } + #[inline] + pub fn lerp(&self, other: Self, t: f32) -> Self { + ARGBu8x4((self.0.cast() + (other.0 - self.0).cast() * simd::f32x4::splat(t)).cast()) + } } impl Mul for ARGBu8x4 { @@ -923,6 +927,22 @@ mod tests { assert_eq!([0x7f, 0x03, 0x00, 0x14], color.to_array()); } + #[test] + fn argbu8x4_lerping() { + assert_eq!( + [0x7f, 0x11, 0x22, 0x33], + (ARGBu8x4::from(0x7f112233).lerp(ARGBu8x4::from(0xffaabbcc), 0.0).to_array()) + ); + assert_eq!( + [0xbf, 0x5d, 0x6e, 0x7f], + (ARGBu8x4::from(0x7f112233).lerp(ARGBu8x4::from(0xffaabbcc), 0.5).to_array()) + ); + assert_eq!( + [0xff, 0xaa, 0xbb, 0xcc], + (ARGBu8x4::from(0x7f112233).lerp(ARGBu8x4::from(0xffaabbcc), 1.0).to_array()) + ); + } + #[test] fn argbf32x4() { let mut color = ARGBf32x4(simd::f32x4::from_array([0.5, 0.1, 0.2, 0.3]));