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);
9 pub const BEAM_ORANGE: Self = Self(0xff, 0x81, 0x05);
10 pub const BLACK: Self = Self(0x00, 0x00, 0x00);
11 pub const BRIGHT_YELLOW: Self = Self(0xff, 0xd9, 0x00);
12 pub const CARD_DARK: Self = Self(0x2a, 0x2a, 0x2a);
13 pub const CREAM: Self = Self(0xff, 0xf0, 0xc2);
14 pub const SUNBEAM_BLACK: Self = Self(0x1f, 0x1f, 0x1f);
15 pub const SUNBEAM_FLAME: Self = Self(0xfb, 0x64, 0x24);
16 pub const SUNBEAM_ORANGE: Self = Self(0xfa, 0x52, 0x0f);
18 pub const SUNSHINE_300: Self = Self(0xff, 0xd0, 0x6a);
19 pub const SUNSHINE_500: Self = Self(0xff, 0xb8, 0x3e);
20 pub const SUNSHINE_700: Self = Self(0xff, 0xa1, 0x10);
21 pub const SUNSHINE_900: Self = Self(0xff, 0x8a, 0x00);
23 pub const WARM_IVORY: Self = Self(0xff, 0xfa, 0xed);
25 pub const WHITE: Self = Self(0xff, 0xff, 0xff);
27
28 pub fn from_hex(hex: &str) -> Option<Self> {
30 let s = hex.strip_prefix('#').unwrap_or(hex);
31 if s.len() != 6 {
32 return None;
33 }
34 let r = u8::from_str_radix(&s[0..2], 0x10).ok()?;
35 let g = u8::from_str_radix(&s[2..4], 0x10).ok()?;
36 let b = u8::from_str_radix(&s[4..6], 0x10).ok()?;
37 Some(Self(r, g, b))
38 }
39
40 pub fn to_hex(&self) -> String {
42 format!("#{:02x}{:02x}{:02x}", self.0, self.1, self.2)
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn color_from_hex() {
52 assert_eq!(Color::from_hex("#fa520f"), Some(Color::SUNBEAM_ORANGE));
53 assert_eq!(Color::from_hex("fa520f"), Some(Color::SUNBEAM_ORANGE));
54 assert_eq!(Color::from_hex("fff"), None);
55 }
56
57 #[test]
58 fn color_to_hex() {
59 assert_eq!(Color::SUNBEAM_ORANGE.to_hex(), "#fa520f");
60 }
61}