diff options
author | HampusM <hampus@hampusmat.com> | 2024-05-12 19:00:35 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-05-12 19:00:35 +0200 |
commit | bf5cf271702b1ed00cf3475c29011cb1b5de7345 (patch) | |
tree | 6c612716cd917909bfc4e6a180c4a1f8a9180498 /engine/src/data_types | |
parent | 6b41d3ccef1ff4f56ab28974a74e5a3a664105bb (diff) |
feat(engine): implement operation traits for Color
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, + } + } +} |