Skip to main content

oxi_tui/
cell.rs

1//! Cell primitives — Color type for theme integration.
2//!
3//! Provides the foundational color type used by the theme system
4//! and ratatui style conversion.
5
6use ratatui::style::{Color as RColor, Style};
7
8/// Text color (24-bit true color).
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum Color {
11    /// Black.
12    Black,
13    /// Red.
14    Red,
15    /// Green.
16    Green,
17    /// Yellow.
18    Yellow,
19    /// Blue.
20    Blue,
21    /// Magenta.
22    Magenta,
23    /// Cyan.
24    Cyan,
25    /// White.
26    White,
27    /// 256-color palette (0-255)
28    Indexed(u8),
29    /// 24-bit RGB
30    Rgb(u8, u8, u8),
31    /// Default terminal color
32    #[default]
33    Default,
34}
35
36impl Color {
37    /// Convert to ratatui Color.
38    pub fn to_ratatui(&self) -> RColor {
39        match self {
40            Color::Black => RColor::Black,
41            Color::Red => RColor::Red,
42            Color::Green => RColor::Green,
43            Color::Yellow => RColor::Yellow,
44            Color::Blue => RColor::Blue,
45            Color::Magenta => RColor::Magenta,
46            Color::Cyan => RColor::Cyan,
47            Color::White => RColor::White,
48            Color::Indexed(n) => RColor::Indexed(*n),
49            Color::Rgb(r, g, b) => RColor::Rgb(*r, *g, *b),
50            Color::Default => RColor::Reset,
51        }
52    }
53
54    /// Convert to ratatui Style with optional background.
55    pub fn to_style(&self, bg: Option<Color>) -> Style {
56        let fg = self.to_ratatui();
57        match bg {
58            Some(bg_color) => Style::new().fg(fg).bg(bg_color.to_ratatui()),
59            None => Style::new().fg(fg),
60        }
61    }
62}