use core::fmt::Result;
use crate::{
CodeWriter, ColorTarget,
color::{Color, WriteColorCodes},
impl_macros::color_type::impl_color_type,
};
pub type RGB = RGBColor;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RGBColor {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl RGBColor {
#[must_use]
pub const fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
}
impl_color_type!(RGBColor {
args: [self];
to_color: { Color::RGB(self) }
});
impl WriteColorCodes for RGBColor {
fn write_color_codes(self, target: ColorTarget, writer: &mut CodeWriter) -> Result {
let target_code = match target {
ColorTarget::Foreground => 38,
ColorTarget::Background => 48,
ColorTarget::Underline => 58,
};
writer.write_code(target_code)?;
writer.write_code(2)?;
writer.write_code(self.r)?;
writer.write_code(self.g)?;
writer.write_code(self.b)?;
Ok(())
}
}