diff --git a/palette/src/alpha.rs b/palette/src/alpha.rs index b20ad0228..5912034bf 100644 --- a/palette/src/alpha.rs +++ b/palette/src/alpha.rs @@ -1,4 +1,5 @@ use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub}; +use std::fmt; use num_traits::Float; @@ -312,24 +313,157 @@ impl From for Alpha { } } +impl fmt::LowerHex for Alpha +where + T: fmt::LowerHex, + C: fmt::LowerHex, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let size = f.width().unwrap_or(::std::mem::size_of::() * 2); + write!( + f, + "{:0width$x}{:0width$x}", + self.color, + self.alpha, + width = size + ) + } +} + +impl fmt::UpperHex for Alpha +where + T: fmt::UpperHex, + C: fmt::UpperHex, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let size = f.width().unwrap_or(::std::mem::size_of::() * 2); + write!( + f, + "{:0width$X}{:0width$X}", + self.color, + self.alpha, + width = size + ) + } +} + #[cfg(test)] -#[cfg(feature = "serde")] mod test { use rgb::Rgba; use encoding::Srgb; + #[test] + fn lower_hex() { + assert_eq!( + format!("{:x}", Rgba::::new(171, 193, 35, 161)), + "abc123a1" + ); + } + + #[test] + fn lower_hex_small_numbers() { + assert_eq!( + format!("{:x}", Rgba::::new(1, 2, 3, 4)), + "01020304" + ); + assert_eq!( + format!("{:x}", Rgba::::new(1, 2, 3, 4)), + "0001000200030004" + ); + assert_eq!( + format!("{:x}", Rgba::::new(1, 2, 3, 4)), + "00000001000000020000000300000004" + ); + assert_eq!( + format!("{:x}", Rgba::::new(1, 2, 3, 4)), + "0000000000000001000000000000000200000000000000030000000000000004" + ); + } + + #[test] + fn lower_hex_custom_width() { + assert_eq!( + format!("{:03x}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + assert_eq!( + format!("{:03x}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + assert_eq!( + format!("{:03x}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + assert_eq!( + format!("{:03x}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + } + + #[test] + fn upper_hex() { + assert_eq!( + format!("{:X}", Rgba::::new(171, 193, 35, 161)), + "ABC123A1" + ); + } + + #[test] + fn upper_hex_small_numbers() { + assert_eq!( + format!("{:X}", Rgba::::new(1, 2, 3, 4)), + "01020304" + ); + assert_eq!( + format!("{:X}", Rgba::::new(1, 2, 3, 4)), + "0001000200030004" + ); + assert_eq!( + format!("{:X}", Rgba::::new(1, 2, 3, 4)), + "00000001000000020000000300000004" + ); + assert_eq!( + format!("{:X}", Rgba::::new(1, 2, 3, 4)), + "0000000000000001000000000000000200000000000000030000000000000004" + ); + } + + #[test] + fn upper_hex_custom_width() { + assert_eq!( + format!("{:03X}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + assert_eq!( + format!("{:03X}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + assert_eq!( + format!("{:03X}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + assert_eq!( + format!("{:03X}", Rgba::::new(1, 2, 3, 4)), + "001002003004" + ); + } + #[cfg(feature = "serde")] #[test] fn serialize() { let serialized = ::serde_json::to_string(&Rgba::::new(0.3, 0.8, 0.1, 0.5)).unwrap(); - assert_eq!(serialized, r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#); + assert_eq!( + serialized, + r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"# + ); } #[cfg(feature = "serde")] #[test] fn deserialize() { - let deserialized: Rgba = ::serde_json::from_str(r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#).unwrap(); + let deserialized: Rgba = + ::serde_json::from_str(r#"{"red":0.3,"green":0.8,"blue":0.1,"alpha":0.5}"#).unwrap(); assert_eq!(deserialized, Rgba::::new(0.3, 0.8, 0.1, 0.5)); } diff --git a/palette/src/luma/luma.rs b/palette/src/luma/luma.rs index 74274d4d3..51e492c3e 100644 --- a/palette/src/luma/luma.rs +++ b/palette/src/luma/luma.rs @@ -1,10 +1,11 @@ +use std::ops::{Add, Div, Mul, Sub}; +use std::marker::PhantomData; +use std::fmt; + use approx::ApproxEq; use num_traits::Float; -use std::ops::{Add, Div, Mul, Sub}; -use std::marker::PhantomData; - use {Alpha, Xyz, Yxy}; use {Blend, Component, ComponentWise, FromColor, IntoColor, Limited, Mix, Pixel, Shade}; use luma::LumaStandard; @@ -533,6 +534,28 @@ where } } +impl fmt::LowerHex for Luma +where + T: Component + fmt::LowerHex, + S: LumaStandard, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let size = f.width().unwrap_or(::std::mem::size_of::() * 2); + write!(f, "{:0width$x}", self.luma, width = size) + } +} + +impl fmt::UpperHex for Luma +where + T: Component + fmt::UpperHex, + S: LumaStandard, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let size = f.width().unwrap_or(::std::mem::size_of::() * 2); + write!(f, "{:0width$X}", self.luma, width = size) + } +} + #[cfg(test)] mod test { use Luma; @@ -552,6 +575,54 @@ mod test { raw_pixel_conversion_tests!(Luma: luma); + #[test] + fn lower_hex() { + assert_eq!(format!("{:x}", Luma::::new(161)), "a1"); + } + + #[test] + fn lower_hex_small_numbers() { + assert_eq!(format!("{:x}", Luma::::new(1)), "01"); + assert_eq!(format!("{:x}", Luma::::new(1)), "0001"); + assert_eq!(format!("{:x}", Luma::::new(1)), "00000001"); + assert_eq!( + format!("{:x}", Luma::::new(1)), + "0000000000000001" + ); + } + + #[test] + fn lower_hex_custom_width() { + assert_eq!(format!("{:03x}", Luma::::new(1)), "001"); + assert_eq!(format!("{:03x}", Luma::::new(1)), "001"); + assert_eq!(format!("{:03x}", Luma::::new(1)), "001"); + assert_eq!(format!("{:03x}", Luma::::new(1)), "001"); + } + + #[test] + fn upper_hex() { + assert_eq!(format!("{:X}", Luma::::new(161)), "A1"); + } + + #[test] + fn upper_hex_small_numbers() { + assert_eq!(format!("{:X}", Luma::::new(1)), "01"); + assert_eq!(format!("{:X}", Luma::::new(1)), "0001"); + assert_eq!(format!("{:X}", Luma::::new(1)), "00000001"); + assert_eq!( + format!("{:X}", Luma::::new(1)), + "0000000000000001" + ); + } + + #[test] + fn upper_hex_custom_width() { + assert_eq!(format!("{:03X}", Luma::::new(1)), "001"); + assert_eq!(format!("{:03X}", Luma::::new(1)), "001"); + assert_eq!(format!("{:03X}", Luma::::new(1)), "001"); + assert_eq!(format!("{:03X}", Luma::::new(1)), "001"); + } + #[cfg(feature = "serde")] #[test] fn serialize() { diff --git a/palette/src/rgb/rgb.rs b/palette/src/rgb/rgb.rs index 7a61cb12a..41d66e20f 100644 --- a/palette/src/rgb/rgb.rs +++ b/palette/src/rgb/rgb.rs @@ -1,6 +1,7 @@ use std::marker::PhantomData; use std::ops::{Add, Div, Mul, Sub}; use std::any::TypeId; +use std::fmt; use num_traits::Float; use approx::ApproxEq; @@ -753,6 +754,42 @@ where } } +impl fmt::LowerHex for Rgb +where + T: Component + fmt::LowerHex, + S: RgbStandard, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let size = f.width().unwrap_or(::std::mem::size_of::() * 2); + write!( + f, + "{:0width$x}{:0width$x}{:0width$x}", + self.red, + self.green, + self.blue, + width = size + ) + } +} + +impl fmt::UpperHex for Rgb +where + T: Component + fmt::UpperHex, + S: RgbStandard, +{ + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let size = f.width().unwrap_or(::std::mem::size_of::() * 2); + write!( + f, + "{:0width$X}{:0width$X}{:0width$X}", + self.red, + self.green, + self.blue, + width = size + ) + } +} + #[cfg(test)] mod test { use super::Rgb; @@ -775,6 +812,96 @@ mod test { raw_pixel_conversion_tests!(Rgb: red, green, blue); raw_pixel_conversion_fail_tests!(Rgb: red, green, blue); + #[test] + fn lower_hex() { + assert_eq!( + format!("{:x}", Rgb::::new(171, 193, 35)), + "abc123" + ); + } + + #[test] + fn lower_hex_small_numbers() { + assert_eq!(format!("{:x}", Rgb::::new(1, 2, 3)), "010203"); + assert_eq!( + format!("{:x}", Rgb::::new(1, 2, 3)), + "000100020003" + ); + assert_eq!( + format!("{:x}", Rgb::::new(1, 2, 3)), + "000000010000000200000003" + ); + assert_eq!( + format!("{:x}", Rgb::::new(1, 2, 3)), + "000000000000000100000000000000020000000000000003" + ); + } + + #[test] + fn lower_hex_custom_width() { + assert_eq!( + format!("{:03x}", Rgb::::new(1, 2, 3)), + "001002003" + ); + assert_eq!( + format!("{:03x}", Rgb::::new(1, 2, 3)), + "001002003" + ); + assert_eq!( + format!("{:03x}", Rgb::::new(1, 2, 3)), + "001002003" + ); + assert_eq!( + format!("{:03x}", Rgb::::new(1, 2, 3)), + "001002003" + ); + } + + #[test] + fn upper_hex() { + assert_eq!( + format!("{:X}", Rgb::::new(171, 193, 35)), + "ABC123" + ); + } + + #[test] + fn upper_hex_small_numbers() { + assert_eq!(format!("{:X}", Rgb::::new(1, 2, 3)), "010203"); + assert_eq!( + format!("{:X}", Rgb::::new(1, 2, 3)), + "000100020003" + ); + assert_eq!( + format!("{:X}", Rgb::::new(1, 2, 3)), + "000000010000000200000003" + ); + assert_eq!( + format!("{:X}", Rgb::::new(1, 2, 3)), + "000000000000000100000000000000020000000000000003" + ); + } + + #[test] + fn upper_hex_custom_width() { + assert_eq!( + format!("{:03X}", Rgb::::new(1, 2, 3)), + "001002003" + ); + assert_eq!( + format!("{:03X}", Rgb::::new(1, 2, 3)), + "001002003" + ); + assert_eq!( + format!("{:03X}", Rgb::::new(1, 2, 3)), + "001002003" + ); + assert_eq!( + format!("{:03X}", Rgb::::new(1, 2, 3)), + "001002003" + ); + } + #[cfg(feature = "serde")] #[test] fn serialize() { @@ -786,7 +913,8 @@ mod test { #[cfg(feature = "serde")] #[test] fn deserialize() { - let deserialized: Rgb = ::serde_json::from_str(r#"{"red":0.3,"green":0.8,"blue":0.1}"#).unwrap(); + let deserialized: Rgb = + ::serde_json::from_str(r#"{"red":0.3,"green":0.8,"blue":0.1}"#).unwrap(); assert_eq!(deserialized, Rgb::::new(0.3, 0.8, 0.1)); }