From bf5cf271702b1ed00cf3475c29011cb1b5de7345 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 12 May 2024 19:00:35 +0200 Subject: feat(engine): implement operation traits for Color --- engine/src/data_types/color.rs | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'engine/src/data_types') diff --git a/engine/src/data_types/color.rs b/engine/src/data_types/color.rs index 7bab678..cef3b92 100644 --- a/engine/src/data_types/color.rs +++ b/engine/src/data_types/color.rs @@ -1,3 +1,5 @@ +use std::ops::{Add, Div, Mul, Neg, Sub}; + #[derive(Debug, Clone, Default)] #[repr(C)] pub struct Color @@ -28,3 +30,69 @@ impl From for Color } } } + +macro_rules! impl_math_op { + ($math_op_trait: ident, $function: ident) => { + impl $math_op_trait for Color + where + Value: $math_op_trait, + { + type Output = Self; + + fn $function(self, rhs: Self) -> Self::Output + { + Self { + red: self.red.$function(rhs.red), + green: self.green.$function(rhs.green), + blue: self.blue.$function(rhs.blue), + } + } + } + }; +} + +macro_rules! impl_scalar_math_op { + ($math_op_trait: ident, $function: ident) => { + impl $math_op_trait for Color + where + Value: $math_op_trait + Clone, + { + type Output = Self; + + fn $function(self, rhs: Value) -> Self::Output + { + Self { + red: self.red.$function(rhs.clone()), + green: self.green.$function(rhs.clone()), + blue: self.blue.$function(rhs), + } + } + } + }; +} + +impl_math_op!(Add, add); +impl_math_op!(Sub, sub); +impl_math_op!(Mul, mul); +impl_math_op!(Div, div); + +impl_scalar_math_op!(Add, add); +impl_scalar_math_op!(Sub, sub); +impl_scalar_math_op!(Mul, mul); +impl_scalar_math_op!(Div, div); + +impl Neg for Color +where + Value: Neg, +{ + type Output = Self; + + fn neg(self) -> Self::Output + { + Self { + red: -self.red, + green: -self.green, + blue: -self.blue, + } + } +} -- cgit v1.2.3-18-g5258