Lines
100 %
Functions
Branches
use ratatui::style::Color;
use super::ColorDepth;
/// The sixteen CGA colours.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Cga {
#[default]
Black,
Blue,
Green,
Cyan,
Red,
Magenta,
Brown,
LightGray,
DarkGray,
LightBlue,
LightGreen,
LightCyan,
LightRed,
LightMagenta,
Yellow,
White,
}
impl Cga {
pub(super) const ALL: [Cga; 16] = [
Cga::Black,
Cga::Blue,
Cga::Green,
Cga::Cyan,
Cga::Red,
Cga::Magenta,
Cga::Brown,
Cga::LightGray,
Cga::DarkGray,
Cga::LightBlue,
Cga::LightGreen,
Cga::LightCyan,
Cga::LightRed,
Cga::LightMagenta,
Cga::Yellow,
Cga::White,
];
const fn entry(self) -> (&'static str, u32) {
match self {
Cga::Black => ("black", 0x00_00_00),
Cga::Blue => ("blue", 0x00_00_AA),
Cga::Green => ("green", 0x00_AA_00),
Cga::Cyan => ("cyan", 0x00_AA_AA),
Cga::Red => ("red", 0xAA_00_00),
Cga::Magenta => ("magenta", 0xAA_00_AA),
Cga::Brown => ("brown", 0xAA_55_00),
Cga::LightGray => ("lightgray", 0xAA_AA_AA),
Cga::DarkGray => ("darkgray", 0x55_55_55),
Cga::LightBlue => ("lightblue", 0x55_55_FF),
Cga::LightGreen => ("lightgreen", 0x55_FF_55),
Cga::LightCyan => ("lightcyan", 0x55_FF_FF),
Cga::LightRed => ("lightred", 0xFF_55_55),
Cga::LightMagenta => ("lightmagenta", 0xFF_55_FF),
Cga::Yellow => ("yellow", 0xFF_FF_55),
Cga::White => ("white", 0xFF_FF_FF),
/// The colour's own RGB, whatever depth the terminal shows it at.
#[must_use]
pub fn rgb(self) -> [u8; 3] {
let [_, r, g, b] = self.entry().1.to_be_bytes();
[r, g, b]
pub fn from_name(name: &str) -> Option<Cga> {
Self::ALL.into_iter().find(|cga| cga.entry().0 == name)
/// The colour as the terminal shows it at `depth`.
pub fn color(self, depth: ColorDepth) -> Color {
match depth {
ColorDepth::Rgb => Color::from_u32(self.entry().1),
ColorDepth::Indexed256 => Color::Indexed(self.index256()),
ColorDepth::Ansi16 => self.ansi(),
/// Like [`Cga::color`], but ANSI-16 backgrounds only have the eight dark colours.
pub fn background(self, depth: ColorDepth) -> Color {
ColorDepth::Ansi16 => self.dark().ansi(),
_ => self.color(depth),
/// The RGB a cell with this background shows at `depth`: the colour itself at `Rgb`, the
/// xterm palette entry at `Indexed256`, the dark colour at `Ansi16`.
pub fn background_rgb(self, depth: ColorDepth) -> [u8; 3] {
ColorDepth::Rgb => self.rgb(),
ColorDepth::Indexed256 => xterm_rgb(self.index256()),
ColorDepth::Ansi16 => self.dark().rgb(),
/// The nearest cube or grey-ramp index, which terminal themes cannot remap.
const fn index256(self) -> u8 {
Cga::Black => 16,
Cga::Blue => 19,
Cga::Green => 34,
Cga::Cyan => 37,
Cga::Red => 124,
Cga::Magenta => 127,
Cga::Brown => 130,
Cga::LightGray => 248,
Cga::DarkGray => 240,
Cga::LightBlue => 63,
Cga::LightGreen => 83,
Cga::LightCyan => 87,
Cga::LightRed => 203,
Cga::LightMagenta => 207,
Cga::Yellow => 227,
Cga::White => 231,
const fn ansi(self) -> Color {
Cga::Black => Color::Black,
Cga::Blue => Color::Blue,
Cga::Green => Color::Green,
Cga::Cyan => Color::Cyan,
Cga::Red => Color::Red,
Cga::Magenta => Color::Magenta,
Cga::Brown => Color::Yellow,
Cga::LightGray => Color::Gray,
Cga::DarkGray => Color::DarkGray,
Cga::LightBlue => Color::LightBlue,
Cga::LightGreen => Color::LightGreen,
Cga::LightCyan => Color::LightCyan,
Cga::LightRed => Color::LightRed,
Cga::LightMagenta => Color::LightMagenta,
Cga::Yellow => Color::LightYellow,
Cga::White => Color::White,
const fn dark(self) -> Cga {
Cga::DarkGray => Cga::Black,
Cga::LightBlue => Cga::Blue,
Cga::LightGreen => Cga::Green,
Cga::LightCyan => Cga::Cyan,
Cga::LightRed => Cga::Red,
Cga::LightMagenta => Cga::Magenta,
Cga::Yellow => Cga::Brown,
Cga::White => Cga::LightGray,
other => other,
/// The colour of xterm palette entry `index` in the cube and grey ramp (16..=255).
fn xterm_rgb(index: u8) -> [u8; 3] {
const LEVELS: [u8; 6] = [0, 95, 135, 175, 215, 255];
match index {
232.. => [8 + 10 * (index - 232); 3],
_ => {
let cube = usize::from(index.saturating_sub(16));
[
LEVELS[cube / 36 % 6],
LEVELS[cube / 6 % 6],
LEVELS[cube % 6],
]