Skip to content

Commit

Permalink
#283 fixing generation of hex color strings for passing to xft
Browse files Browse the repository at this point in the history
  • Loading branch information
sminez committed Nov 10, 2023
1 parent d70bff7 commit 3dc62b8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 3dc62b8

Please sign in to comment.