summaryrefslogtreecommitdiff
path: root/engine/src/image.rs
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/image.rs
parent649d4d4ab2f54ca12f75d2cba50e1adef2350dbe (diff)
refactor(engine): change Color struct into a enum
Diffstat (limited to 'engine/src/image.rs')
-rw-r--r--engine/src/image.rs70
1 files changed, 35 insertions, 35 deletions
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<Dimens<u32>>, color: impl Into<Color<u8>>)
- -> Self
- {
- let dimens: Dimens<u32> = dimens.into();
-
- let color: Color<u8> = 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<Dimens<u32>>,
- color: impl Into<Color<u8>>,
- alpha: u8,
- ) -> Self
+ pub fn from_color<PixelT: ColorPixel>(color: PixelT, size: Dimens<u32>) -> Self
+ where
+ Self: From<(PixelT, Dimens<u32>)>,
{
- let dimens: Dimens<u32> = dimens.into();
-
- let color: Color<u8> = 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<u32>
@@ -153,6 +123,36 @@ impl Image
}
}
+impl From<(Rgb<u8>, Dimens<u32>)> for Image
+{
+ fn from((color, size): (Rgb<u8>, Dimens<u32>)) -> 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<u8>, Dimens<u32>)> for Image
+{
+ fn from((color, size): (Rgba<u8>, Dimens<u32>)) -> 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<Buf> TryFrom<PixelBuffer<$pixel<$component>, Buf>> for Image