diff --git a/colorizer/src/color/rgb.rs b/colorizer/src/color/rgb.rs index 147604a..a156aad 100644 --- a/colorizer/src/color/rgb.rs +++ b/colorizer/src/color/rgb.rs @@ -23,77 +23,53 @@ impl PartialEq for RGB { } } +fn hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 { + if t < 0.0 { + t += 1.0; + } + if t > 1.0 { + t -= 1.0; + } + + if t < 1.0 / 6.0 { + return p + (q - p) * 6.0 * t; + } + if t < 1.0 / 2.0 { + return q; + } + if t < 2.0 / 3.0 { + return p + (q - p) * (2.0 / 3.0 - t) * 6.0; + } + + p +} + impl From for RGB { fn from(color: HSL) -> Self { + let h = color.0.to_f32() / 360.0; + let s = color.1.to_f32() / 100.0; + let l = color.2.to_f32() / 100.0; + // No saturation if color.1 == 0 { - let shade = color.2 * 255 / 100; - let intensity = ColorIntensity::new(shade); + let shade = l * 255.0; + let intensity = ColorIntensity::new(shade as i16); return Self(intensity.clone(), intensity.clone(), intensity.clone()); } - let temp_1: f32; - - if color.2 < 50 { + let temp_1: f32 = if l < 0.5 { // Low lum - temp_1 = (color.2.to_f32() / 100.0) * (color.1.to_f32() / 100.0 + 1.0); + l * (1.0 + s) } else { // High lum - temp_1 = (color.2.to_f32() / 100.0 + color.1.to_f32()) - - (color.2.to_f32() * color.1.to_f32()); - } + l + s - l * s + }; - let temp_2: f32 = (color.2.to_f32() / 100.0) * 2.0 - temp_1; + let temp_2: f32 = 2.0 * l - temp_1; - let hue = color.0.to_f32() / 360.0; - - let mut temp_r = hue + 0.333; - let temp_g = hue; - let temp_b = hue - 0.333; - - // Normalize values - if temp_r > 1.0 { - temp_r = temp_r - 1.0; - } - if temp_b < 0.0 { - temp_r = temp_r + 1.0; - } - - // Calc Red - let red: f32; - if temp_r * 6.0 < 1.0 { - red = temp_2 + (temp_1 - temp_2) * 6.0 * temp_r; - } else if temp_r * 2.0 < 1.0 { - red = temp_1; - } else if temp_r * 3.0 < 2.0 { - red = temp_2 + (temp_1 - temp_2) * (0.666 - temp_r) * 6.0; - } else { - red = temp_2; - } - - // Calc Green - let green: f32; - if temp_g * 6.0 < 1.0 { - green = temp_2 + (temp_1 - temp_2) * 6.0 * temp_g; - } else if temp_g * 2.0 < 1.0 { - green = temp_1; - } else if temp_g * 3.0 < 2.0 { - green = temp_2 + (temp_1 - temp_2) * (0.666 - temp_g) * 6.0; - } else { - green = temp_2; - } - - // Calc blue - let blue: f32; - if temp_b * 6.0 < 1.0 { - blue = temp_2 + (temp_1 - temp_2) * 6.0 * temp_b; - } else if temp_b * 2.0 < 1.0 { - blue = temp_1; - } else if temp_b * 3.0 < 2.0 { - blue = temp_2 + (temp_1 - temp_2) * (0.666 - temp_b) * 6.0; - } else { - blue = temp_2; - } + let red = hue_to_rgb(temp_2, temp_1, h + 1.0 / 3.0); + let green = hue_to_rgb(temp_2, temp_1, h); + let blue = hue_to_rgb(temp_2, temp_1, h - 1.0 / 3.0); Self::new( (red * 255.0).round() as u8, diff --git a/colorizer/src/main.rs b/colorizer/src/main.rs index e9a8f45..743fa6a 100644 --- a/colorizer/src/main.rs +++ b/colorizer/src/main.rs @@ -18,8 +18,8 @@ fn main() { println!("Hello, world!"); example(); - let hsl_color = Color::from(HSL::new(193, 67, 28)); - let rgb_color = Color::from(RGB::from(HSL::new(193, 67, 28))); + let hsl_color = Color::from(HSL::new(0, 100, 50)); + // let rgb_color = Color::from(HSL::new(193, 67, 28)); println!("HSL Color: {}", hsl_color.format()); - println!("RGB Color: {}", rgb_color.format()); + // println!("RGB Color: {}", rgb_color.format()); }