From bc3d80cf5378423b1994236da3f614d7b008ea43 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Thu, 14 Aug 2025 14:16:38 +0200 Subject: [PATCH] tests: basic convrsion testing implemented --- colorizer/src/color/hsl.rs | 33 ------------- colorizer/src/color/test/colors.test.rs | 62 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/colorizer/src/color/hsl.rs b/colorizer/src/color/hsl.rs index a5a2850..a10c0a4 100644 --- a/colorizer/src/color/hsl.rs +++ b/colorizer/src/color/hsl.rs @@ -20,38 +20,6 @@ impl PartialEq for HSL { } } -fn min_of_float_vec(vector: Vec) -> Option { - let mut min: Option = None; - - for element in vector.iter() { - if let Some(value) = min { - if element < &value { - min = Some(*element) - } - } else { - min = Some(*element); - } - } - - min -} - -fn max_of_float_vec(vector: Vec) -> Option { - let mut max: Option = None; - - for element in vector.iter() { - if let Some(value) = max { - if element > &value { - max = Some(*element) - } - } else { - max = Some(*element); - } - } - - max -} - impl From for HSL { fn from(value: RGB) -> Self { let r = value.0.to_f32() / 255.0; @@ -78,7 +46,6 @@ impl From for HSL { } // Hue set - // TODO FIX if max == r { let temp = if g < b { 6.0 } else { 0.0 }; h = (g - b) / (max - min) + temp; diff --git a/colorizer/src/color/test/colors.test.rs b/colorizer/src/color/test/colors.test.rs index e6a08da..98682ed 100644 --- a/colorizer/src/color/test/colors.test.rs +++ b/colorizer/src/color/test/colors.test.rs @@ -25,10 +25,72 @@ pub mod tests { let blue_hsl = HSL::new(240, 100, 50); assert_eq!(HSL::from(blue_rgb), blue_hsl); + + // Variant colors + + let pink_rgb = RGB::new(255, 175, 204); + let pink_hsl = HSL::new(338, 100, 84); + + assert_eq!(HSL::from(pink_rgb), pink_hsl); + + let orange_rgb = RGB::new(251, 133, 0); + let orange_hsl = HSL::new(32, 100, 49); + + assert_eq!(HSL::from(orange_rgb), orange_hsl); + + let yellow_rgb = RGB::new(255, 214, 10); + let yellow_hsl = HSL::new(50, 100, 52); + + assert_eq!(HSL::from(yellow_rgb), yellow_hsl); + + let purple_rgb = RGB::new(123, 44, 191); + let purple_hsl = HSL::new(272, 63, 46); + + assert_eq!(HSL::from(purple_rgb), purple_hsl); } #[test] fn test_hsl_to_rgb() { + let red_rgb = RGB::new(255, 0, 0); + let red_hsl = HSL::new(0, 100, 50); + + assert_eq!(red_rgb, RGB::from(red_hsl)); + + let green_rgb = RGB::new(0, 255, 0); + let green_hsl = HSL::new(120, 100, 50); + + assert_eq!(green_rgb, RGB::from(green_hsl)); + + let blue_rgb = RGB::new(0, 0, 255); + let blue_hsl = HSL::new(240, 100, 50); + + assert_eq!(blue_rgb, RGB::from(blue_hsl)); + + // Variant colors + + let pink_rgb = RGB::new(255, 173, 203); + let pink_hsl = HSL::new(338, 100, 84); + + assert_eq!(pink_rgb, RGB::from(pink_hsl)); + + let orange_rgb = RGB::new(250, 133, 0); + let orange_hsl = HSL::new(32, 100, 49); + + assert_eq!(orange_rgb, RGB::from(orange_hsl)); + + let yellow_rgb = RGB::new(255, 214, 10); + let yellow_hsl = HSL::new(50, 100, 52); + + assert_eq!(yellow_rgb, RGB::from(yellow_hsl)); + + let purple_rgb = RGB::new(122, 43, 191); + let purple_hsl = HSL::new(272, 63, 46); + + assert_eq!(purple_rgb, RGB::from(purple_hsl)); + } + + #[test] + fn test_color_initialization() { let red_hsl = Color::from(HSL::new(0, 100, 50)); let red_rgb = Color::from(RGB::new(255, 0, 0)); assert_eq!(red_hsl, red_rgb);