use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RgbColor {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl RgbColor {
pub const fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
pub const fn default_face() -> Self {
Self {
r: 180,
g: 200,
b: 230,
}
}
}
impl Default for RgbColor {
fn default() -> Self {
Self::default_face()
}
}
impl fmt::Debug for RgbColor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RgbColor({}, {}, {})", self.r, self.g, self.b)
}
}
impl fmt::Display for RgbColor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "#{:02X}{:02X}{:02X}", self.r, self.g, self.b)
}
}
impl crate::dump::Dump for RgbColor {
fn render(&self, _opts: &crate::dump::DumpOptions) -> String {
format!("{self} (r={}, g={}, b={})", self.r, self.g, self.b)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_light_blue() {
let d = RgbColor::default();
assert_eq!(d, RgbColor::new(180, 200, 230));
}
#[test]
fn display_uses_hex_uppercase() {
let c = RgbColor::new(255, 0, 16);
assert_eq!(format!("{}", c), "#FF0010");
}
#[test]
fn debug_components() {
let c = RgbColor::new(1, 2, 3);
assert_eq!(format!("{:?}", c), "RgbColor(1, 2, 3)");
}
#[test]
fn serde_roundtrip() {
use crate::archive::Portable;
let c = RgbColor::new(10, 20, 30);
let bytes = c.to_bytes().unwrap();
let back = RgbColor::from_bytes(&bytes).unwrap();
assert_eq!(c, back);
}
}