1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*!
# Foreground colors
```rust
use ansi_rgb::{ Colorable, red };
println!("{}", "Hello, world!".fg(red()));
```
Output:
<code style="color: red">Hello, world!</code>
# Background colors
```rust
use ansi_rgb::{ Colorable, red };
println!("{}", "Hello, world!".bg(red()));
```
Output:
<code style="background: red">Hello, world!</code>
# Nesting
```rust
use ansi_rgb::{ Colorable, blue, green, red };
let formatted = format!(
"Hello, world! {}",
format!(
"{} is an interesting {}",
"This".fg(blue()),
"day".fg(red())
).bg(green())
);
println!("{}", formatted);
# assert_eq!(
# "Hello, world! \u{1b}[48;2;0;255;0m\u{1b}[38;2;0;0;255mThis\u{1b}[39m is an interesting \u{1b}[38;2;255;0;0mday\u{1b}[39m\u{1b}[49m",
# formatted
# )
```
Output:
<code>Hello, world! <span style="background: #00FF00"><span style="color: #0000FF">This</span> is an interesting <span style="color: #FF0000">day</span></span></code>
# Anything formattable
```rust
use ansi_rgb::*;
#[derive(Debug)]
struct Foo(i32, i32);
let foo = Foo(1, 2);
println!("{:?}", foo.fg(green()));
```
Output:
<code style="color: #00FF00">Foo(1, 2)</code>
# 3-bit colors
```rust
use ansi_rgb::{ Colorable, Color3 };
println!("{}", "Hello, world!".fg(Color3::RED).bg(Color3::BLACK));
```
Output:
<code style="color: #800000; background: #000000">Hello, world!</code>
# 4-bit colors
```rust
use ansi_rgb::{ Colorable, Color4 };
println!("{}", "Hello, world!".fg(Color4::BRIGHT_RED).bg(Color4::BLACK));
```
Output:
<code style="color: #ff0000; background: #000000">Hello, world!</code>
# 8-bit colors
```rust
use ansi_rgb::{ Colorable, Color8 };
println!("{}", "Hello, world!".fg(Color8::new(160)).bg(Color8::new(0)));
```
Output:
<code style="color: #d70000; background: #000000">Hello, world!</code>
# 24-bit colors
Built-in support for [the `rgb` crate](https://crates.io/crates/rgb).
```toml
# Cargo.toml
[dependencies]
rgb = { version = "0.8", default-features = false }
```
```rust
use ansi_rgb::{ Colorable };
use rgb::RGB8;
let fg = RGB8::new(123, 231, 111);
let bg = RGB8::new(10, 100, 20);
println!("{}", "Yuck".fg(fg).bg(bg));
```
Output:
<code style="color: #7BE76F; background: #0A6414">Yuck</code>
# Extending to other color types
If you have your own color type and you know how to turn it into ANSI escape
sequences then just implement `FormatColor`:
```rust
use ansi_rgb::{ Canvas, Colorable, FormatColor };
use core::fmt;
enum FavoriteColors {
SkyBlue,
RocketPlumeYellow,
TitaniumGray
}
impl FormatColor for FavoriteColors {
fn prelude(&self, f: &mut fmt::Formatter, canvas: Canvas) -> fmt::Result {
let (r, g, b) = match self {
FavoriteColors::SkyBlue => (135, 206, 235),
FavoriteColors::RocketPlumeYellow => (255, 255, 0),
FavoriteColors::TitaniumGray => (86, 95, 107)
};
write!(
f,
"\x1B[{};2;{};{};{}m",
match canvas {
Canvas::Foreground => 38,
Canvas::Background => 48
},
r,
g,
b
)
}
}
println!(
"The sky is {}",
"blue".fg(FavoriteColors::SkyBlue)
);
# assert_eq!("The sky is \x1B[38;2;135;206;235mblue\x1B[39m", format!("The sky is {}", "blue".fg(FavoriteColors::SkyBlue)))
```
Output:
<code><span>The sky is </span><span style="color: #87CEEB">blue</span></code>
# Features
`default` includes 3-, 4-, 8-, and 24-bit colors and depends on the `rgb` crate,
giving you the following things:
* Dependency on `rgb` crate
* Implementation of `FormatColor` for `rgb::RGB8` type
* `Color3` enum and its implementation of `FormatColor`
* `Color4` struct and its implementation of `FormatColor`
* `Color8` struct and its implementation of `FormatColor`
* Color functions (`red()`, `orange()`, etc)
# Windows users
You need to [set your console mode](https://docs.microsoft.com/en-us/windows/console/console-modes). Otherwise you'll get garbage like this:
`�[48;2;159;114;0m �[0m`
*/
pub use *;
pub use *;