colory/background.rs
1/*
2Copyright 2021 CoolDeveloper101
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17
18use std::fmt;
19
20/// An enum to manipulate background colors.
21/// # Example
22/// ```
23/// # use colory::{Off, BackgroundColor, colory_init};
24/// #
25/// # fn main() {
26/// # colory_init();
27/// println!("{}The background should be blue.{}", BackgroundColor::Red, Off);
28/// # }
29/// ```
30/// See the [examples](https://github.com/CoolDeveloper101/colory/tree/master/examples) directory for more examples.
31pub enum BackgroundColor {
32 Black,
33 Red,
34 Green,
35 Yellow,
36 Blue,
37 Magenta,
38 Cyan,
39 White,
40 /// Somethimes you want more than 8 colors. Colory also has options to choose from a 8 bit color pallete which contains 256 colors.
41 /// You can check out the color pallete at [Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit)
42 EightBit(u8),
43 /// Heck! You want even more colors. Colory also has an RGB pallete which allows you to have 16777216 colors!
44 RGB(u8, u8, u8),
45 /// Reset the background color to whatever the default background color is.
46 Normal,
47}
48
49impl fmt::Display for BackgroundColor {
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 match self{
52 BackgroundColor::Black => write!(f, "\x1b[40m"),
53 BackgroundColor::Red => write!(f, "\x1b[41m"),
54 BackgroundColor::Green => write!(f, "\x1b[42m"),
55 BackgroundColor::Yellow => write!(f, "\x1b[43m"),
56 BackgroundColor::Blue => write!(f, "\x1b[44m"),
57 BackgroundColor::Magenta => write!(f, "\x1b[45m"),
58 BackgroundColor::Cyan => write!(f, "\x1b[46m"),
59 BackgroundColor::White => write!(f, "\x1b[47m"),
60 BackgroundColor::EightBit(c) => write!(f, "\x1b[48;5;{}m", c),
61 BackgroundColor::RGB(r, g, b) => write!(f, "\x1b[48;2;{};{};{}m", r, g, b),
62 BackgroundColor::Normal => write!(f, "\x1b[49m"),
63 }
64 }
65}