use std::fmt::{Display, Formatter};
use crate::{color::RGB, FloatNumber};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ansi16 {
pub(crate) code: u8,
}
impl Ansi16 {
#[must_use]
fn new(code: u8) -> Self {
assert!(
(30..=37).contains(&code) || (90..=97).contains(&code),
"Invalid ANSI 16 color code: {}",
code
);
Self { code }
}
#[inline]
#[must_use]
pub fn foreground(&self) -> u8 {
self.code
}
#[inline]
#[must_use]
pub fn background(&self) -> u8 {
self.code + 10
}
#[inline]
#[must_use]
pub fn black() -> Self {
Self::new(30)
}
#[inline]
#[must_use]
pub fn red() -> Self {
Self::new(31)
}
#[inline]
#[must_use]
pub fn green() -> Self {
Self::new(32)
}
#[inline]
#[must_use]
pub fn yellow() -> Self {
Self::new(33)
}
#[inline]
#[must_use]
pub fn blue() -> Self {
Self::new(34)
}
#[inline]
#[must_use]
pub fn magenta() -> Self {
Self::new(35)
}
#[inline]
#[must_use]
pub fn cyan() -> Self {
Self::new(36)
}
#[inline]
#[must_use]
pub fn white() -> Self {
Self::new(37)
}
#[inline]
#[must_use]
pub fn bright_black() -> Self {
Self::new(90)
}
#[inline]
#[must_use]
pub fn bright_red() -> Self {
Self::new(91)
}
#[inline]
#[must_use]
pub fn bright_green() -> Self {
Self::new(92)
}
#[inline]
#[must_use]
pub fn bright_yellow() -> Self {
Self::new(93)
}
#[inline]
#[must_use]
pub fn bright_blue() -> Self {
Self::new(94)
}
#[inline]
#[must_use]
pub fn bright_magenta() -> Self {
Self::new(95)
}
#[inline]
#[must_use]
pub fn bright_cyan() -> Self {
Self::new(96)
}
#[inline]
#[must_use]
pub fn bright_white() -> Self {
Self::new(97)
}
}
impl Display for Ansi16 {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "ANSI16({})", self.code)
}
}
impl From<&RGB> for Ansi16 {
fn from(rgb: &RGB) -> Self {
let code = from_rgb::<f32>(rgb.r, rgb.g, rgb.b);
Self { code }
}
}
#[inline]
#[must_use]
fn from_rgb<T>(r: u8, g: u8, b: u8) -> u8
where
T: FloatNumber,
{
let max = RGB::max_value::<T>();
let r = T::from_u8(r) / max;
let g = T::from_u8(g) / max;
let b = T::from_u8(b) / max;
let value = (r.max(g).max(b) * T::from_f32(100.0) / T::from_f32(50.0))
.round()
.to_u8_unsafe();
if value == 0 {
return 30;
}
let r = r.round().to_u8_unsafe();
let g = g.round().to_u8_unsafe();
let b = b.round().to_u8_unsafe();
let code = 30 + (b << 2 | g << 1 | r);
if value == 2 {
code + 60 } else {
code }
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[test]
fn test_new() {
let actual = Ansi16::new(30);
assert_eq!(actual, Ansi16 { code: 30 });
assert_eq!(actual.foreground(), 30);
assert_eq!(actual.background(), 40);
}
#[test]
#[should_panic(expected = "Invalid ANSI 16 color code: 29")]
fn test_new_invalid() {
let _actual = Ansi16::new(29);
}
#[test]
fn test_fmt() {
let ansi16 = Ansi16::bright_black();
let actual = format!("{}", ansi16);
assert_eq!(actual, "ANSI16(90)");
}
#[rstest]
#[case::black((0, 0, 0), Ansi16::black())]
#[case::red((128, 0, 0), Ansi16::red())]
#[case::green((0, 128, 0), Ansi16::green())]
#[case::yellow((128, 128, 0), Ansi16::yellow())]
#[case::blue((0, 0, 128), Ansi16::blue())]
#[case::magenta((128, 0, 128), Ansi16::magenta())]
#[case::cyan((0, 128, 128), Ansi16::cyan())]
#[case::white((128, 128, 128), Ansi16::white())]
#[case::bright_red((255, 0, 0), Ansi16::bright_red())]
#[case::bright_green((0, 255, 0), Ansi16::bright_green())]
#[case::bright_yellow((255, 255, 0), Ansi16::bright_yellow())]
#[case::bright_blue((0, 0, 255), Ansi16::bright_blue())]
#[case::bright_magenta((255, 0, 255), Ansi16::bright_magenta())]
#[case::bright_cyan((0, 255, 255), Ansi16::bright_cyan())]
#[case::bright_white((255, 255, 255), Ansi16::bright_white())]
fn test_from_rgb(#[case] rgb: (u8, u8, u8), #[case] expected: Ansi16) {
let rgb = RGB::new(rgb.0, rgb.1, rgb.2);
let actual = Ansi16::from(&rgb);
assert_eq!(actual, expected);
}
}