use crate::error::{QRError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl Color {
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b, a: 255 }
}
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Self { r, g, b, a }
}
pub fn from_hex(hex: &str) -> Result<Self> {
let hex = hex.trim_start_matches('#');
match hex.len() {
3 => {
let r = u8::from_str_radix(&hex[0..1], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let g = u8::from_str_radix(&hex[1..2], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let b = u8::from_str_radix(&hex[2..3], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
Ok(Self::rgb(r * 17, g * 17, b * 17))
}
6 => {
let r = u8::from_str_radix(&hex[0..2], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let g = u8::from_str_radix(&hex[2..4], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let b = u8::from_str_radix(&hex[4..6], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
Ok(Self::rgb(r, g, b))
}
8 => {
let r = u8::from_str_radix(&hex[0..2], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let g = u8::from_str_radix(&hex[2..4], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let b = u8::from_str_radix(&hex[4..6], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
let a = u8::from_str_radix(&hex[6..8], 16)
.map_err(|_| QRError::InvalidColor(hex.to_string()))?;
Ok(Self::rgba(r, g, b, a))
}
_ => Err(QRError::InvalidColor(hex.to_string())),
}
}
pub fn to_hex(&self) -> String {
if self.a == 255 {
format!("#{:02X}{:02X}{:02X}", self.r, self.g, self.b)
} else {
format!("#{:02X}{:02X}{:02X}{:02X}", self.r, self.g, self.b, self.a)
}
}
pub fn to_rgba_string(&self) -> String {
if self.a == 255 {
format!("rgb({}, {}, {})", self.r, self.g, self.b)
} else {
format!(
"rgba({}, {}, {}, {:.3})",
self.r,
self.g,
self.b,
self.a as f64 / 255.0
)
}
}
pub const BLACK: Color = Color::rgb(0, 0, 0);
pub const WHITE: Color = Color::rgb(255, 255, 255);
pub const TRANSPARENT: Color = Color::rgba(0, 0, 0, 0);
}
impl Default for Color {
fn default() -> Self {
Self::BLACK
}
}
impl std::fmt::Display for Color {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_hex())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_hex() {
assert_eq!(Color::from_hex("#FF0000").unwrap(), Color::rgb(255, 0, 0));
assert_eq!(Color::from_hex("#00FF00").unwrap(), Color::rgb(0, 255, 0));
assert_eq!(Color::from_hex("#0000FF").unwrap(), Color::rgb(0, 0, 255));
assert_eq!(Color::from_hex("000000").unwrap(), Color::rgb(0, 0, 0));
assert_eq!(Color::from_hex("#FFF").unwrap(), Color::rgb(255, 255, 255));
}
#[test]
fn test_to_hex() {
assert_eq!(Color::rgb(255, 0, 0).to_hex(), "#FF0000");
assert_eq!(Color::rgba(255, 0, 0, 128).to_hex(), "#FF000080");
}
}