use core::fmt::Result;
use crate::{
CodeWriter, ColorTarget,
color::{BasicColor, Color, IndexedColor, WriteColorCodes},
impl_macros::color_type::impl_color_type,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SimpleColor {
basic_color: BasicColor,
bright: bool,
}
impl SimpleColor {
#[must_use]
pub const fn new(basic_color: BasicColor) -> Self {
Self {
basic_color,
bright: false,
}
}
#[must_use]
pub const fn new_bright(basic_color: BasicColor) -> Self {
Self::new(basic_color).bright()
}
#[must_use]
pub const fn bright(self) -> Self {
Self {
bright: true,
..self
}
}
#[must_use]
pub const fn get_basic_color(self) -> BasicColor {
self.basic_color
}
#[must_use]
pub const fn is_bright(self) -> bool {
self.bright
}
}
impl_color_type!(SimpleColor {
args: [self];
to_color: { Color::Simple(self) }
});
impl WriteColorCodes for SimpleColor {
fn write_color_codes(self, target: ColorTarget, writer: &mut CodeWriter) -> Result {
let offset = self.basic_color.code_offset();
match (target, self.bright) {
(ColorTarget::Foreground, false) => writer.write_code(30 + offset),
(ColorTarget::Background, false) => writer.write_code(40 + offset),
(ColorTarget::Foreground, true) => writer.write_code(90 + offset),
(ColorTarget::Background, true) => writer.write_code(100 + offset),
(ColorTarget::Underline, false) => {
IndexedColor(offset).write_color_codes(target, writer)
}
(ColorTarget::Underline, true) => {
IndexedColor(offset + 8).write_color_codes(target, writer)
}
}
}
}