From 3dc62b8b15f716fcc94161125685204d4526fa72 Mon Sep 17 00:00:00 2001 From: Innes Anderson-Morrison Date: Fri, 10 Nov 2023 11:13:15 +0000 Subject: [PATCH] #283 fixing generation of hex color strings for passing to xft --- src/lib.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 99525fff..82e32705 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -240,7 +240,7 @@ pub struct Color { } impl Color { - /// Create a new Color from a hex encoded u32: 0xRRGGBB or 0xRRGGBBAA + /// Create a new Color from a hex encoded u32: 0xRRGGBBAA pub fn new_from_hex(rgba_hex: u32) -> Self { Self { rgba_hex } } @@ -268,7 +268,7 @@ impl Color { /// Render this color as a #RRGGBB hew color string pub fn as_rgb_hex_string(&self) -> String { - format!("#{:x}", self.rgb_u32()) + format!("#{:0>6X}", self.rgb_u32()) } /// 0xRRGGBB representation of this Color (no alpha information) @@ -336,3 +336,19 @@ impl TryFrom<&str> for Color { } } } + +#[cfg(test)] +mod tests { + use super::*; + use simple_test_case::test_case; + + #[test_case(0xAABBCCDD, "#AABBCC"; "r g and b are correct")] + #[test_case(0x001122FF, "#001122"; "leading 0s are preserved")] + #[test_case(0x00000000, "#000000"; "black works")] + #[test] + fn as_rgb_hex_string_is_correct(rgba_hex: u32, expected: &str) { + let c: Color = rgba_hex.into(); + + assert_eq!(&c.as_rgb_hex_string(), expected); + } +}