From 65e24308babb2ed4f33fb6f1d7b531df676bd70d Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 17 Jul 2026 03:06:04 +0200 Subject: refactor(engine): change Color struct into a enum --- engine/src/data_types/color.rs | 143 ++++++++++++++++++++++------------------ engine/src/data_types/vector.rs | 13 ---- 2 files changed, 79 insertions(+), 77 deletions(-) (limited to 'engine/src/data_types') diff --git a/engine/src/data_types/color.rs b/engine/src/data_types/color.rs index 06df450..18d5334 100644 --- a/engine/src/data_types/color.rs +++ b/engine/src/data_types/color.rs @@ -1,4 +1,4 @@ -use std::ops::{Add, Div, Mul, Neg, Sub}; +use std::ops::{Add, Div, Mul, Sub}; pub trait Pixel: sealed::Sealed { @@ -7,12 +7,32 @@ pub trait Pixel: sealed::Sealed macro_rules! gen_color { ($ident: ident, components=($($component: ident),+)) => { - #[derive(Debug, Clone)] + #[derive(Debug, Clone, Default)] pub struct $ident { $(pub $component: Component),+ } + impl $ident + { + pub fn white() -> Self + { + Self { + $($component: 255),* + } + } + } + + impl $ident + { + pub fn white() -> Self + { + Self { + $($component: 1.0),* + } + } + } + impl Pixel for $ident { type Component = Component; @@ -27,59 +47,74 @@ gen_color!(Rgba, components = (r, g, b, a)); gen_color!(Luma, components = (l)); gen_color!(LumaA, components = (l, a)); -#[derive(Debug, Clone, Default)] -pub struct Color -{ - pub red: Value, - pub green: Value, - pub blue: Value, -} - -impl Color -{ - pub const WHITE_F32: Self = Self { red: 1.0, green: 1.0, blue: 1.0 }; -} - -impl Color +#[derive(Debug, Clone)] +pub enum Color { - pub const WHITE_U8: Self = Self { red: 255, green: 255, blue: 255 }; + Rgb(Rgb), + Rgba(Rgba), + Luma(Luma), + LumaA(LumaA), } -impl From for Color +impl Default for Color { - fn from(value: Value) -> Self + fn default() -> Self { - Self { - red: value.clone(), - green: value.clone(), - blue: value, - } + Self::Rgb(Rgb::default()) } } -macro_rules! impl_math_op { - ($math_op_trait: ident, $function: ident) => { - impl $math_op_trait for Color +macro_rules! gen_scalar_math_op_impl { + ( + $ident: ident, + $math_op_trait: ident, + $function: ident, + components=($($component: ident),+) + ) => { + impl $math_op_trait for $ident where - Value: $math_op_trait, + Value: $math_op_trait + Clone, { type Output = Self; - fn $function(self, rhs: Self) -> Self::Output + fn $function(self, rhs: Value) -> Self::Output { Self { - red: self.red.$function(rhs.red), - green: self.green.$function(rhs.green), - blue: self.blue.$function(rhs.blue), + $($component: self.$component.$function(rhs.clone())),+ } } } }; } -macro_rules! impl_scalar_math_op { - ($math_op_trait: ident, $function: ident) => { - impl $math_op_trait for Color +gen_scalar_math_op_impl!(Rgb, Add, add, components = (r, g, b)); +gen_scalar_math_op_impl!(Rgb, Sub, sub, components = (r, g, b)); +gen_scalar_math_op_impl!(Rgb, Mul, mul, components = (r, g, b)); +gen_scalar_math_op_impl!(Rgb, Div, div, components = (r, g, b)); + +gen_scalar_math_op_impl!(Rgba, Add, add, components = (r, g, b, a)); +gen_scalar_math_op_impl!(Rgba, Sub, sub, components = (r, g, b, a)); +gen_scalar_math_op_impl!(Rgba, Mul, mul, components = (r, g, b, a)); +gen_scalar_math_op_impl!(Rgba, Div, div, components = (r, g, b, a)); + +gen_scalar_math_op_impl!(Luma, Add, add, components = (l)); +gen_scalar_math_op_impl!(Luma, Sub, sub, components = (l)); +gen_scalar_math_op_impl!(Luma, Mul, mul, components = (l)); +gen_scalar_math_op_impl!(Luma, Div, div, components = (l)); + +gen_scalar_math_op_impl!(LumaA, Add, add, components = (l, a)); +gen_scalar_math_op_impl!(LumaA, Sub, sub, components = (l, a)); +gen_scalar_math_op_impl!(LumaA, Mul, mul, components = (l, a)); +gen_scalar_math_op_impl!(LumaA, Div, div, components = (l, a)); + +macro_rules! gen_enum_scalar_math_op_impl { + ( + $ident: ident, + $math_op_trait: ident, + $function: ident, + variants=($($variant: ident),+) + ) => { + impl $math_op_trait for $ident where Value: $math_op_trait + Clone, { @@ -87,41 +122,21 @@ macro_rules! impl_scalar_math_op { 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), + match self { + $( + Self::$variant(inner) => + Self::$variant(inner.$function(rhs.clone())) + ),+ } } } }; } -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, - } - } -} +gen_enum_scalar_math_op_impl!(Color, Add, add, variants = (Rgb, Rgba, Luma, LumaA)); +gen_enum_scalar_math_op_impl!(Color, Sub, sub, variants = (Rgb, Rgba, Luma, LumaA)); +gen_enum_scalar_math_op_impl!(Color, Mul, mul, variants = (Rgb, Rgba, Luma, LumaA)); +gen_enum_scalar_math_op_impl!(Color, Div, div, variants = (Rgb, Rgba, Luma, LumaA)); mod sealed { diff --git a/engine/src/data_types/vector.rs b/engine/src/data_types/vector.rs index dd9e386..8644dd2 100644 --- a/engine/src/data_types/vector.rs +++ b/engine/src/data_types/vector.rs @@ -1,6 +1,5 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; -use crate::color::Color; use crate::reflection::Reflection; macro_rules! impl_math_op_traits { @@ -226,18 +225,6 @@ impl From for Vec3 } } -impl From> for Vec3 -{ - fn from(color: Color) -> Self - { - Self { - x: color.red, - y: color.green, - z: color.blue, - } - } -} - impl From<[Value; 3]> for Vec3 { fn from([x, y, z]: [Value; 3]) -> Self -- cgit v1.2.3-18-g5258