line_ui/style/
color.rs

1/*
2 * Copyright (c) 2025 Jasmine Tai. All rights reserved.
3 */
4
5/// A terminal color, which can be applied to the foreground or background.
6///
7/// Note that some distinct values of [`Color`] could end up appearing as the
8/// same color when displayed in the terminal. However, the [`PartialEq`]
9/// implementation does not account for this; it only compares the
10/// representation of the enum.
11///
12/// [`From`] implementations are provided for convenience.
13///
14/// # Example
15///
16/// ```
17/// use line_ui::Color;
18///
19/// assert_eq!(Color::Default, Color::default());
20/// assert_eq!(Color::Ansi(42), 42.into());
21/// assert_eq!(Color::Rgb(20, 40, 90), (20, 40, 90).into());
22/// ```
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[non_exhaustive]
26pub enum Color {
27    /// The default color used by the terminal.
28    #[default]
29    Default,
30    /// An ANSI color code.
31    Ansi(u8),
32    /// An RGB color.
33    Rgb(u8, u8, u8),
34}
35
36impl From<u8> for Color {
37    fn from(value: u8) -> Self {
38        Color::Ansi(value)
39    }
40}
41
42impl From<(u8, u8, u8)> for Color {
43    fn from((r, g, b): (u8, u8, u8)) -> Self {
44        Color::Rgb(r, g, b)
45    }
46}