summaryrefslogtreecommitdiff
path: root/engine/src/data_types
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-07-17 03:06:04 +0200
committerHampusM <hampus@hampusmat.com>2026-07-17 03:06:04 +0200
commit65e24308babb2ed4f33fb6f1d7b531df676bd70d (patch)
treedf9477d4b805cc6866a086833d6118505804dd3a /engine/src/data_types
parent649d4d4ab2f54ca12f75d2cba50e1adef2350dbe (diff)
refactor(engine): change Color struct into a enum
Diffstat (limited to 'engine/src/data_types')
-rw-r--r--engine/src/data_types/color.rs143
-rw-r--r--engine/src/data_types/vector.rs13
2 files changed, 79 insertions, 77 deletions
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<Component>
{
$(pub $component: Component),+
}
+ impl $ident<u8>
+ {
+ pub fn white() -> Self
+ {
+ Self {
+ $($component: 255),*
+ }
+ }
+ }
+
+ impl $ident<f32>
+ {
+ pub fn white() -> Self
+ {
+ Self {
+ $($component: 1.0),*
+ }
+ }
+ }
+
impl<Component> Pixel for $ident<Component>
{
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<Value>
-{
- pub red: Value,
- pub green: Value,
- pub blue: Value,
-}
-
-impl Color<f32>
-{
- pub const WHITE_F32: Self = Self { red: 1.0, green: 1.0, blue: 1.0 };
-}
-
-impl Color<u8>
+#[derive(Debug, Clone)]
+pub enum Color<Component>
{
- pub const WHITE_U8: Self = Self { red: 255, green: 255, blue: 255 };
+ Rgb(Rgb<Component>),
+ Rgba(Rgba<Component>),
+ Luma(Luma<Component>),
+ LumaA(LumaA<Component>),
}
-impl<Value: Clone> From<Value> for Color<Value>
+impl<Component: Default> Default for Color<Component>
{
- 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<Value> $math_op_trait for Color<Value>
+macro_rules! gen_scalar_math_op_impl {
+ (
+ $ident: ident,
+ $math_op_trait: ident,
+ $function: ident,
+ components=($($component: ident),+)
+ ) => {
+ impl<Value> $math_op_trait<Value> for $ident<Value>
where
- Value: $math_op_trait<Output = Value>,
+ Value: $math_op_trait<Output = Value> + 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<Value> $math_op_trait<Value> for Color<Value>
+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<Value> $math_op_trait<Value> for $ident<Value>
where
Value: $math_op_trait<Output = Value> + 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<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,
- }
- }
-}
+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<f32> for Vec3<f32>
}
}
-impl<Value> From<Color<Value>> for Vec3<Value>
-{
- fn from(color: Color<Value>) -> Self
- {
- Self {
- x: color.red,
- y: color.green,
- z: color.blue,
- }
- }
-}
-
impl<Value> From<[Value; 3]> for Vec3<Value>
{
fn from([x, y, z]: [Value; 3]) -> Self