diff options
Diffstat (limited to 'engine/src/data_types')
-rw-r--r-- | engine/src/data_types/color.rs | 68 |
1 files changed, 68 insertions, 0 deletions
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<Value> @@ -28,3 +30,69 @@ impl<Value: Clone> From<Value> for Color<Value> } } } + +macro_rules! impl_math_op { + ($math_op_trait: ident, $function: ident) => { + impl<Value> $math_op_trait for Color<Value> + where + Value: $math_op_trait<Output = Value>, + { + 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<Value> $math_op_trait<Value> for Color<Value> + where + Value: $math_op_trait<Output = Value> + 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<Value> Neg for Color<Value> +where + Value: Neg<Output = Value>, +{ + type Output = Self; + + fn neg(self) -> Self::Output + { + Self { + red: -self.red, + green: -self.green, + blue: -self.blue, + } + } +} |