use core::fmt::Result;
use crate::{CodeWriter, Plane, color::WriteColorCodes};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EightBitColor(pub u8);
impl EightBitColor {
#[must_use]
pub const fn new(number: u8) -> Self {
EightBitColor(number)
}
#[must_use]
pub const fn get_number(self) -> u8 {
self.0
}
}
impl WriteColorCodes for EightBitColor {
fn write_color_codes(self, plane: Plane, writer: &mut CodeWriter) -> Result {
let plane_code = match plane {
Plane::Foreground => 38,
Plane::Background => 48,
};
writer.write_code(plane_code)?;
writer.write_code(5)?;
writer.write_code(self.0)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::{
ColorInAPlane, Plane,
color::{Color, ColorKind as _},
};
use super::*;
#[test]
fn eight_bit_color() {
let color_1 = EightBitColor(7);
assert_eq!(color_1.get_number(), 7u8);
let color_2 = EightBitColor::new(7);
assert_eq!(color_2.get_number(), 7u8);
assert_eq!(color_1, color_2);
}
#[test]
fn in_fg() {
let color = EightBitColor(7);
assert_eq!(color.in_fg(), ColorInAPlane::new(color, Plane::Foreground));
assert_eq!(
color.in_plane(Plane::Foreground),
ColorInAPlane::new(color, Plane::Foreground)
);
}
#[test]
fn in_bg() {
let color = EightBitColor(7);
assert_eq!(color.in_bg(), ColorInAPlane::new(color, Plane::Background));
assert_eq!(
color.in_plane(Plane::Background),
ColorInAPlane::new(color, Plane::Background)
);
}
#[test]
fn to_color() {
assert_eq!(
EightBitColor(7).to_color(),
Color::EightBit(EightBitColor(7))
);
}
}