owo_colors/colors/
dynamic.rs
1use crate::{AnsiColors, DynColor};
2use core::fmt;
3
4#[allow(unused_imports)]
5use crate::OwoColorize;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
10pub struct Rgb(pub u8, pub u8, pub u8);
11
12impl crate::private::Sealed for Rgb {}
13
14impl DynColor for Rgb {
15 fn fmt_ansi_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 let Rgb(r, g, b) = self;
17 write!(f, "\x1b[38;2;{};{};{}m", r, g, b)
18 }
19
20 fn fmt_ansi_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 let Rgb(r, g, b) = self;
22 write!(f, "\x1b[48;2;{};{};{}m", r, g, b)
23 }
24
25 fn fmt_raw_ansi_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 let Rgb(r, g, b) = self;
27 write!(f, "38;2;{};{};{}", r, g, b)
28 }
29
30 fn fmt_raw_ansi_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 let Rgb(r, g, b) = self;
32 write!(f, "48;2;{};{};{}", r, g, b)
33 }
34
35 #[doc(hidden)]
36 fn get_dyncolors_fg(&self) -> crate::DynColors {
37 let Rgb(r, g, b) = self;
38 crate::DynColors::Rgb(*r, *g, *b)
39 }
40
41 #[doc(hidden)]
42 fn get_dyncolors_bg(&self) -> crate::DynColors {
43 self.get_dyncolors_fg()
44 }
45}
46
47impl crate::private::Sealed for str {}
48
49impl DynColor for str {
50 fn fmt_ansi_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 let color: AnsiColors = self.into();
52 color.fmt_ansi_fg(f)
53 }
54
55 fn fmt_ansi_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56 let color: AnsiColors = self.into();
57 color.fmt_ansi_bg(f)
58 }
59
60 fn fmt_raw_ansi_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 let color: AnsiColors = self.into();
62 color.fmt_raw_ansi_fg(f)
63 }
64
65 fn fmt_raw_ansi_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66 let color: AnsiColors = self.into();
67 color.fmt_raw_ansi_bg(f)
68 }
69
70 #[doc(hidden)]
71 fn get_dyncolors_fg(&self) -> crate::DynColors {
72 crate::DynColors::Ansi(self.into())
73 }
74
75 #[doc(hidden)]
76 fn get_dyncolors_bg(&self) -> crate::DynColors {
77 crate::DynColors::Ansi(self.into())
78 }
79}
80
81impl<'a> From<&'a str> for AnsiColors {
83 fn from(color: &'a str) -> Self {
84 #[allow(clippy::match_same_arms)] match color {
86 "black" => AnsiColors::Black,
87 "red" => AnsiColors::Red,
88 "green" => AnsiColors::Green,
89 "yellow" => AnsiColors::Yellow,
90 "blue" => AnsiColors::Blue,
91 "magenta" | "purple" => AnsiColors::Magenta,
92 "cyan" => AnsiColors::Cyan,
93 "white" => AnsiColors::White,
94 "bright black" => AnsiColors::BrightBlack,
95 "bright red" => AnsiColors::BrightRed,
96 "bright green" => AnsiColors::BrightGreen,
97 "bright yellow" => AnsiColors::BrightYellow,
98 "bright blue" => AnsiColors::BrightBlue,
99 "bright magenta" => AnsiColors::BrightMagenta,
100 "bright cyan" => AnsiColors::BrightCyan,
101 "bright white" => AnsiColors::BrightWhite,
102 _ => AnsiColors::White,
103 }
104 }
105}