1#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5pub struct Color(pub u8, pub u8, pub u8);
6
7impl Color {
8 pub const BEAM_GOLD: Self = Self(0xff, 0xe2, 0x95);
10 pub const BEAM_ORANGE: Self = Self(0xff, 0x81, 0x05);
12 pub const BLACK: Self = Self(0x00, 0x00, 0x00);
14 pub const BRIGHT_YELLOW: Self = Self(0xff, 0xd9, 0x00);
16 pub const CARD_DARK: Self = Self(0x2a, 0x2a, 0x2a);
18 pub const CREAM: Self = Self(0xff, 0xf0, 0xc2);
20 pub const SUNBEAM_BLACK: Self = Self(0x1f, 0x1f, 0x1f);
22 pub const SUNBEAM_FLAME: Self = Self(0xfb, 0x64, 0x24);
24 pub const SUNBEAM_ORANGE: Self = Self(0xfa, 0x52, 0x0f);
27 pub const SUNSHINE_300: Self = Self(0xff, 0xd0, 0x6a);
29 pub const SUNSHINE_500: Self = Self(0xff, 0xb8, 0x3e);
31 pub const SUNSHINE_700: Self = Self(0xff, 0xa1, 0x10);
33 pub const SUNSHINE_900: Self = Self(0xff, 0x8a, 0x00);
36 pub const WARM_IVORY: Self = Self(0xff, 0xfa, 0xed);
39 pub const WHITE: Self = Self(0xff, 0xff, 0xff);
42
43 pub fn from_hex(hex: &str) -> Option<Self> {
45 let s = hex.strip_prefix('#').unwrap_or(hex);
46 if s.len() != 6 {
47 return None;
48 }
49 let r = u8::from_str_radix(&s[0..2], 0x10).ok()?;
50 let g = u8::from_str_radix(&s[2..4], 0x10).ok()?;
51 let b = u8::from_str_radix(&s[4..6], 0x10).ok()?;
52 Some(Self(r, g, b))
53 }
54
55 pub fn to_hex(&self) -> String {
57 format!("#{:02x}{:02x}{:02x}", self.0, self.1, self.2)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn color_from_hex() {
67 assert_eq!(Color::from_hex("#fa520f"), Some(Color::SUNBEAM_ORANGE));
68 assert_eq!(Color::from_hex("fa520f"), Some(Color::SUNBEAM_ORANGE));
69 assert_eq!(Color::from_hex("fff"), None);
70 }
71
72 #[test]
73 fn color_to_hex() {
74 assert_eq!(Color::SUNBEAM_ORANGE.to_hex(), "#fa520f");
75 }
76}