use crate::Error;
#[derive(Debug, Clone, Copy)]
pub struct Canvas {
r: u8,
g: u8,
b: u8,
}
impl Canvas {
pub fn from_hex_color(hex: &str) -> Result<Self, Error> {
let hex = hex.strip_prefix('#').unwrap_or(hex);
if hex.len() != 6 {
return Err(Error::ConnectionFailed(format!(
"invalid hex color: expected 6 hex digits, got '{hex}'"
)));
}
let r = u8::from_str_radix(&hex[0..2], 16)
.map_err(|e| Error::ConnectionFailed(format!("invalid hex color: {e}")))?;
let g = u8::from_str_radix(&hex[2..4], 16)
.map_err(|e| Error::ConnectionFailed(format!("invalid hex color: {e}")))?;
let b = u8::from_str_radix(&hex[4..6], 16)
.map_err(|e| Error::ConnectionFailed(format!("invalid hex color: {e}")))?;
Ok(Self { r, g, b })
}
pub fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
pub fn black() -> Self {
Self { r: 0, g: 0, b: 0 }
}
pub fn white() -> Self {
Self {
r: 255,
g: 255,
b: 255,
}
}
pub(crate) fn rgb(&self) -> [u8; 3] {
[self.r, self.g, self.b]
}
}