style_term/color/
eight_bit.rs1use std::fmt::{Display, Formatter};
2use std::num::ParseIntError;
3use std::str::FromStr;
4use crate::color::{Color, DisplayColor};
5#[cfg(not(feature = "default_four_bit"))]
6use crate::DefaultColor;
7
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[derive(Debug, Clone)]
11pub struct EightBitColor(u8);
12
13
14impl From<u8> for EightBitColor {
15 fn from(v: u8) -> Self {
16 EightBitColor(v)
17 }
18}
19
20impl FromStr for EightBitColor {
21 type Err = ParseIntError;
22
23 fn from_str(s: &str) -> Result<Self, Self::Err> {
24 let result = u8::from_str(s)?;
25 Ok(EightBitColor(result))
26 }
27}
28
29impl Display for EightBitColor {
30 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31 write!(f, "5;{}", self.0)
32 }
33}
34
35#[cfg(not(feature = "default_four_bit"))]
36impl From<DefaultColor> for Color {
37 fn from(value: DefaultColor) -> Self {
38 let value = match value {
39 DefaultColor::Black => { 0 }
40 DefaultColor::Red => { 1 }
41 DefaultColor::Green => { 2 }
42 DefaultColor::Yellow => { 3 }
43 DefaultColor::Blue => { 4 }
44 DefaultColor::Magenta => { 5 }
45 DefaultColor::Cyan => { 6 }
46 DefaultColor::White => { 7 }
47 DefaultColor::Gray => { 8 }
48 DefaultColor::BrightRed => { 9 }
49 DefaultColor::BrightGreen => { 10 }
50 DefaultColor::BrightYellow => { 11 }
51 DefaultColor::BrightBlue => { 12 }
52 DefaultColor::BrightMagenta => { 13 }
53 DefaultColor::BrightCyan => { 14 }
54 DefaultColor::BrightWhite => { 15 }
55 };
56 Color::EightBitColor(EightBitColor(value))
57 }
58}
59
60
61impl DisplayColor for EightBitColor {}
62
63
64impl From<EightBitColor> for Color {
65 fn from(color: EightBitColor) -> Self {
66 Color::EightBitColor(color)
67 }
68}