diff --git a/colorizer/src/color/mod.rs b/colorizer/src/color/mod.rs index 18d0ea4..e30ad25 100644 --- a/colorizer/src/color/mod.rs +++ b/colorizer/src/color/mod.rs @@ -5,6 +5,8 @@ mod test; pub mod hsl; pub mod rgb; +use std::fmt::{UpperHex, write}; + use crate::core::ranged::RangedInt; pub type ColorIntensity = RangedInt<0, 255>; @@ -24,6 +26,12 @@ impl Color { } } +impl UpperHex for Color { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:X}", self.0) + } +} + impl PartialEq for Color { fn eq(&self, other: &Self) -> bool { self.0 == other.0 diff --git a/colorizer/src/color/rgb.rs b/colorizer/src/color/rgb.rs index a156aad..36cf818 100644 --- a/colorizer/src/color/rgb.rs +++ b/colorizer/src/color/rgb.rs @@ -1,3 +1,5 @@ +use std::fmt::UpperHex; + use crate::{ color::{ColorIntensity, HSL, RGB}, core::ranged::BaseNumber, @@ -23,6 +25,12 @@ impl PartialEq for RGB { } } +impl UpperHex for RGB { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:0>2X}{:0>2X}{:0>2X}", self.0, self.1, self.2) + } +} + fn hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 { if t < 0.0 { t += 1.0; diff --git a/colorizer/src/color/test/colors.test.rs b/colorizer/src/color/test/colors.test.rs index 98682ed..b8a4b44 100644 --- a/colorizer/src/color/test/colors.test.rs +++ b/colorizer/src/color/test/colors.test.rs @@ -1,5 +1,7 @@ #[cfg(test)] pub mod tests { + use std::fmt::format; + use crate::color::{Color, HSL, RGB}; #[test] @@ -89,6 +91,31 @@ pub mod tests { assert_eq!(purple_rgb, RGB::from(purple_hsl)); } + #[test] + fn test_basic_hex_convertion() { + let red_color = Color::from(RGB::new(255, 0, 0)); + let green_color = Color::from(RGB::new(0, 255, 0)); + let blue_color = Color::from(RGB::new(0, 0, 255)); + + assert_eq!(format!("{:X}", red_color), "FF0000"); + assert_eq!(format!("{:X}", green_color), "00FF00"); + assert_eq!(format!("{:X}", blue_color), "0000FF"); + } + + #[test] + fn test_complex_hex_convertion() { + let color = Color::from(RGB::new(255, 183, 3)); + assert_eq!(format!("{:X}", color), "FFB703"); + let color = Color::from(RGB::new(88, 129, 87)); + assert_eq!(format!("{:X}", color), "588157"); + let color = Color::from(RGB::new(251, 133, 0)); + assert_eq!(format!("{:X}", color), "FB8500"); + let color = Color::from(RGB::new(131, 56, 236)); + assert_eq!(format!("{:X}", color), "8338EC"); + let color = Color::from(RGB::new(157, 129, 137)); + assert_eq!(format!("{:X}", color), "9D8189"); + } + #[test] fn test_color_initialization() { let red_hsl = Color::from(HSL::new(0, 100, 50)); diff --git a/colorizer/src/core/ranged/mod.rs b/colorizer/src/core/ranged/mod.rs index 06f51ec..8f14c46 100644 --- a/colorizer/src/core/ranged/mod.rs +++ b/colorizer/src/core/ranged/mod.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, UpperHex}; + mod foreign_operations; mod self_operations; @@ -18,3 +20,15 @@ impl RangedInt<{ LOW }, { HIGH }> self.0 as f32 } } + +impl Display for RangedInt<{ LOW }, { HIGH }> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl UpperHex for RangedInt<{ LOW }, { HIGH }> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + UpperHex::fmt(&self.0, f) + } +} diff --git a/colorizer/src/main.rs b/colorizer/src/main.rs index 743fa6a..1b2912f 100644 --- a/colorizer/src/main.rs +++ b/colorizer/src/main.rs @@ -20,6 +20,6 @@ fn main() { 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: {:X}", hsl_color); // println!("RGB Color: {}", rgb_color.format()); }