diff --git a/colorizer/src/color/hsl.rs b/colorizer/src/color/hsl.rs index c226cb3..a5a2850 100644 --- a/colorizer/src/color/hsl.rs +++ b/colorizer/src/color/hsl.rs @@ -10,6 +10,16 @@ impl HSL { } } +impl PartialEq for HSL { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 && self.1 == other.1 && self.2 == other.2 + } + + fn ne(&self, other: &Self) -> bool { + self.0 != other.0 || self.1 != other.1 || self.2 != other.2 + } +} + fn min_of_float_vec(vector: Vec) -> Option { let mut min: Option = None; @@ -48,36 +58,39 @@ impl From for HSL { let g = value.1.to_f32() / 255.0; let b = value.2.to_f32() / 255.0; - let min: f32 = min_of_float_vec(vec![r, g, b]).unwrap(); - let max: f32 = max_of_float_vec(vec![r, g, b]).unwrap(); + let min: f32 = r.min(g.min(b)); + let max: f32 = r.max(g.max(b)); - // Luminance - let l = ((min + max) / 2.0).round(); + let h; + let s; + // Luminance set + let l = (min + max) / 2.0; - // Saturation - let s: f32; - if r == g && g == b { + if max == min { s = 0.0; + h = 0.0; } else { + // Saturation set if l <= 0.5 { s = (max - min) / (max + min); } else { s = (max - min) / (2.0 - max - min); } - } - // Hue - let h: f32; - if max == r { - h = (g - b) / (max - min); - } else if max == g { - h = 2.0 + (b - r) / (max - min); - } else { - h = 4.0 + (r - g) / (max - min); + // Hue set + // TODO FIX + if max == r { + let temp = if g < b { 6.0 } else { 0.0 }; + h = (g - b) / (max - min) + temp; + } else if max == g { + h = (b - r) / (max - min) + 2.0; + } else { + h = (r - g) / (max - min) + 4.0; + } } HSL::new( - (h * 60.0).round() as u16, + (h / 6.0 * 360.0).round() as u16, (s * 100.0).round() as u8, (l * 100.0).round() as u8, ) diff --git a/colorizer/src/color/mod.rs b/colorizer/src/color/mod.rs index 8bd4804..18d0ea4 100644 --- a/colorizer/src/color/mod.rs +++ b/colorizer/src/color/mod.rs @@ -12,6 +12,7 @@ pub type ColorHue = RangedInt<0, 360>; pub type Percentage = RangedInt<0, 100>; #[derive(Debug)] pub struct RGB(ColorIntensity, ColorIntensity, ColorIntensity); +#[derive(Debug)] pub struct HSL(ColorHue, Percentage, Percentage); // pub struct HSV(ColorHue, Percentage, Percentage); #[derive(Debug)] diff --git a/colorizer/src/color/test/colors.test.rs b/colorizer/src/color/test/colors.test.rs index f933de1..e6a08da 100644 --- a/colorizer/src/color/test/colors.test.rs +++ b/colorizer/src/color/test/colors.test.rs @@ -10,7 +10,25 @@ pub mod tests { } #[test] - fn test_hsl_variants() { + fn test_rgb_to_hsl() { + let red_rgb = RGB::new(255, 0, 0); + let red_hsl = HSL::new(0, 100, 50); + + assert_eq!(HSL::from(red_rgb), red_hsl); + + let green_rgb = RGB::new(0, 255, 0); + let green_hsl = HSL::new(120, 100, 50); + + assert_eq!(HSL::from(green_rgb), green_hsl); + + let blue_rgb = RGB::new(0, 0, 255); + let blue_hsl = HSL::new(240, 100, 50); + + assert_eq!(HSL::from(blue_rgb), blue_hsl); + } + + #[test] + fn test_hsl_to_rgb() { 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);