tulpje_framework/
color.rs1use std::{fmt::Formatter, num::ParseIntError, ops::Deref, str::FromStr};
2
3#[derive(Debug, Eq, PartialEq, Clone, Copy)]
8pub struct Color(pub u32);
9
10impl Color {
11 pub fn from_rgb(r: u8, g: u8, b: u8) -> Self {
12 Self(u32::from_le_bytes([0, r, g, b]))
13 }
14}
15
16impl Deref for Color {
17 type Target = u32;
18
19 fn deref(&self) -> &Self::Target {
20 &self.0
21 }
22}
23
24impl FromStr for Color {
25 type Err = ParseIntError;
26
27 fn from_str(s: &str) -> Result<Self, ParseIntError> {
28 Ok(Self(u32::from_str_radix(s.trim_start_matches("#"), 16)?))
29 }
30}
31
32impl std::fmt::Display for Color {
33 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34 f.write_fmt(format_args!("#{:06X}", self.0))
35 }
36}
37
38impl From<u32> for Color {
39 fn from(val: u32) -> Self {
40 Self(val)
41 }
42}
43
44pub mod roles {
45 use super::Color;
46
47 pub const DEFAULT: Color = Color(0x99AAB5);
48 pub const TEAL: Color = Color(0x1ABC9C);
49 pub const DARK_TEAL: Color = Color(0x11806A);
50 pub const GREEN: Color = Color(0x2ECC71);
51 pub const DARK_GREEN: Color = Color(0x1F8B4C);
52 pub const BLUE: Color = Color(0x3498DB);
53 pub const DARK_BLUE: Color = Color(0x206694);
54 pub const PURPLE: Color = Color(0x9B59B6);
55 pub const DARK_PURPLE: Color = Color(0x71368A);
56 pub const MAGENTA: Color = Color(0xE91E63);
57 pub const DARK_MAGENTA: Color = Color(0xAD1457);
58 pub const GOLD: Color = Color(0xF1C40F);
59 pub const DARK_GOLD: Color = Color(0xC27C0E);
60 pub const ORANGE: Color = Color(0xE67E22);
61 pub const DARK_ORANGE: Color = Color(0xA84300);
62 pub const RED: Color = Color(0xE74C3C);
63 pub const DARK_RED: Color = Color(0x992D22);
64 pub const LIGHTER_GREY: Color = Color(0x95A5A6);
65 pub const LIGHT_GREY: Color = Color(0x979C9F);
66 pub const DARK_GREY: Color = Color(0x607D8B);
67 pub const DARKER_GREY: Color = Color(0x546E7A);
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn test_from_str() {
76 assert_eq!(Color::from_str("#EEEEEE").unwrap(), Color(15658734));
77 }
78
79 #[test]
80 fn test_from_u32() {
81 assert_eq!(Color::from(15658734u32), Color(15658734));
82 }
83
84 #[test]
85 fn test_to_string() {
86 assert_eq!(Color(15658734).to_string(), "#EEEEEE");
87 }
88}