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/image.rs | 70 ++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'engine/src/image.rs') diff --git a/engine/src/image.rs b/engine/src/image.rs index 68eac65..18d3015 100644 --- a/engine/src/image.rs +++ b/engine/src/image.rs @@ -6,7 +6,7 @@ use std::path::Path; use image_rs::GenericImageView as _; -use crate::color::{Color, Luma, LumaA, Pixel as ColorPixel, Rgb, Rgba}; +use crate::color::{Luma, LumaA, Pixel as ColorPixel, Rgb, Rgba}; use crate::data_types::dimens::Dimens; use crate::vector::Vec2; @@ -60,41 +60,11 @@ impl Image Self::try_from(pixels) } - pub fn from_color(dimens: impl Into>, color: impl Into>) - -> Self - { - let dimens: Dimens = dimens.into(); - - let color: Color = color.into(); - - Self { - inner: image_rs::RgbImage::from_pixel( - dimens.width, - dimens.height, - image_rs::Rgb([color.red, color.green, color.blue]), - ) - .into(), - } - } - - pub fn from_color_and_alpha( - dimens: impl Into>, - color: impl Into>, - alpha: u8, - ) -> Self + pub fn from_color(color: PixelT, size: Dimens) -> Self + where + Self: From<(PixelT, Dimens)>, { - let dimens: Dimens = dimens.into(); - - let color: Color = color.into(); - - Self { - inner: image_rs::RgbaImage::from_pixel( - dimens.width, - dimens.height, - image_rs::Rgba([color.red, color.green, color.blue, alpha]), - ) - .into(), - } + Self::from((color, size)) } pub fn dimensions(&self) -> Dimens @@ -153,6 +123,36 @@ impl Image } } +impl From<(Rgb, Dimens)> for Image +{ + fn from((color, size): (Rgb, Dimens)) -> Self + { + Self { + inner: image_rs::RgbImage::from_pixel( + size.width, + size.height, + image_rs::Rgb([color.r, color.g, color.b]), + ) + .into(), + } + } +} + +impl From<(Rgba, Dimens)> for Image +{ + fn from((color, size): (Rgba, Dimens)) -> Self + { + Self { + inner: image_rs::RgbaImage::from_pixel( + size.width, + size.height, + image_rs::Rgba([color.r, color.g, color.b, color.a]), + ) + .into(), + } + } +} + macro_rules! gen_try_from_pixel_buffer_impl { ($pixel: ident<$component: ty>) => { impl TryFrom, Buf>> for Image -- cgit v1.2.3-18-g5258