1use crate::error::{QRError, Result};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Color {
9 pub r: u8,
11 pub g: u8,
13 pub b: u8,
15 pub a: u8,
17}
18
19impl Color {
20 pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
22 Self { r, g, b, a: 255 }
23 }
24
25 pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
27 Self { r, g, b, a }
28 }
29
30 pub fn from_hex(hex: &str) -> Result<Self> {
32 let hex = hex.trim_start_matches('#');
33
34 match hex.len() {
35 3 => {
36 let r = u8::from_str_radix(&hex[0..1], 16)
38 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
39 let g = u8::from_str_radix(&hex[1..2], 16)
40 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
41 let b = u8::from_str_radix(&hex[2..3], 16)
42 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
43 Ok(Self::rgb(r * 17, g * 17, b * 17))
44 }
45 6 => {
46 let r = u8::from_str_radix(&hex[0..2], 16)
48 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
49 let g = u8::from_str_radix(&hex[2..4], 16)
50 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
51 let b = u8::from_str_radix(&hex[4..6], 16)
52 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
53 Ok(Self::rgb(r, g, b))
54 }
55 8 => {
56 let r = u8::from_str_radix(&hex[0..2], 16)
58 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
59 let g = u8::from_str_radix(&hex[2..4], 16)
60 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
61 let b = u8::from_str_radix(&hex[4..6], 16)
62 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
63 let a = u8::from_str_radix(&hex[6..8], 16)
64 .map_err(|_| QRError::InvalidColor(hex.to_string()))?;
65 Ok(Self::rgba(r, g, b, a))
66 }
67 _ => Err(QRError::InvalidColor(hex.to_string())),
68 }
69 }
70
71 pub fn to_hex(&self) -> String {
73 if self.a == 255 {
74 format!("#{:02X}{:02X}{:02X}", self.r, self.g, self.b)
75 } else {
76 format!("#{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
77 }
78 }
79
80 pub fn to_rgba_string(&self) -> String {
82 if self.a == 255 {
83 format!("rgb({}, {}, {})", self.r, self.g, self.b)
84 } else {
85 format!(
86 "rgba({}, {}, {}, {:.3})",
87 self.r,
88 self.g,
89 self.b,
90 self.a as f64 / 255.0
91 )
92 }
93 }
94
95 pub const BLACK: Color = Color::rgb(0, 0, 0);
97
98 pub const WHITE: Color = Color::rgb(255, 255, 255);
100
101 pub const TRANSPARENT: Color = Color::rgba(0, 0, 0, 0);
103}
104
105impl Default for Color {
106 fn default() -> Self {
107 Self::BLACK
108 }
109}
110
111impl std::fmt::Display for Color {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 write!(f, "{}", self.to_hex())
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 #[test]
122 fn test_from_hex() {
123 assert_eq!(Color::from_hex("#FF0000").unwrap(), Color::rgb(255, 0, 0));
124 assert_eq!(Color::from_hex("#00FF00").unwrap(), Color::rgb(0, 255, 0));
125 assert_eq!(Color::from_hex("#0000FF").unwrap(), Color::rgb(0, 0, 255));
126 assert_eq!(Color::from_hex("000000").unwrap(), Color::rgb(0, 0, 0));
127 assert_eq!(Color::from_hex("#FFF").unwrap(), Color::rgb(255, 255, 255));
128 }
129
130 #[test]
131 fn test_to_hex() {
132 assert_eq!(Color::rgb(255, 0, 0).to_hex(), "#FF0000");
133 assert_eq!(Color::rgba(255, 0, 0, 128).to_hex(), "#FF000080");
134 }
135}