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