From 0112e99fc77f3717e37440f9395a240ac060328c Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 30 Apr 2023 16:37:27 -0400 Subject: [PATCH] add color multiplication via operator overload for ARGBu8x4 of course, here we immediately must question whether this is abuse of operator overloading or not. since you cannot otherwise multiply these types together, i think this is fine. plus we are not providing any other math operations via operator overloading, so this feels like a very explicit thing to do --- ggdt/src/graphics/color.rs | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/ggdt/src/graphics/color.rs b/ggdt/src/graphics/color.rs index 62b6bee..17095e0 100644 --- a/ggdt/src/graphics/color.rs +++ b/ggdt/src/graphics/color.rs @@ -1,3 +1,4 @@ +use std::ops::{Mul, MulAssign}; use std::simd; /// Packed 32-bit color, in the format: 0xAARRGGBB @@ -608,6 +609,23 @@ impl ARGBu8x4 { pub const fn to_array(&self) -> [u8; 4] { self.0.to_array() } + +} + +impl Mul for ARGBu8x4 { + type Output = ARGBu8x4; + + #[inline] + fn mul(self, rhs: Self) -> Self::Output { + ARGBu8x4(((self.0.cast::() * rhs.0.cast::()) / simd::u32x4::splat(255)).cast()) + } +} + +impl MulAssign for ARGBu8x4 { + #[inline] + fn mul_assign(&mut self, rhs: Self) { + self.0 = ((self.0.cast::() * rhs.0.cast::()) / simd::u32x4::splat(255)).cast() + } } impl From for ARGBu8x4 { @@ -884,6 +902,27 @@ mod tests { assert_eq!(color.to_array(), [0x7f, 0x19, 0x33, 0x4c]); } + #[test] + fn argbu8x4_multiplication() { + assert_eq!([0xff, 0x11, 0x22, 0x33], (ARGBu8x4::from(0xffffffff) * ARGBu8x4::from(0xff112233)).to_array()); + assert_eq!([0xff, 0x11, 0x22, 0x33], (ARGBu8x4::from(0xff112233) * ARGBu8x4::from(0xffffffff)).to_array()); + assert_eq!([0x7f, 0x03, 0x00, 0x14], (ARGBu8x4::from(0x7f330066) * ARGBu8x4::from(0xff112233)).to_array()); + assert_eq!([0x7f, 0x03, 0x00, 0x14], (ARGBu8x4::from(0xff112233) * ARGBu8x4::from(0x7f330066)).to_array()); + + let mut color = ARGBu8x4::from(0xffffffff); + color *= ARGBu8x4::from(0xff112233); + assert_eq!([0xff, 0x11, 0x22, 0x33], color.to_array()); + let mut color = ARGBu8x4::from(0xff112233); + color *= ARGBu8x4::from(0xffffffff); + assert_eq!([0xff, 0x11, 0x22, 0x33], color.to_array()); + let mut color = ARGBu8x4::from(0x7f330066); + color *= ARGBu8x4::from(0xff112233); + assert_eq!([0x7f, 0x03, 0x00, 0x14], color.to_array()); + let mut color = ARGBu8x4::from(0xff112233); + color *= ARGBu8x4::from(0x7f330066); + assert_eq!([0x7f, 0x03, 0x00, 0x14], color.to_array()); + } + #[test] fn argbf32x4() { let mut color = ARGBf32x4(simd::f32x4::from_array([0.5, 0.1, 0.2, 0.3]));