1extern crate nu_ansi_term;
2use nu_ansi_term::Color;
3
4// This example prints out the 256 colors.
5// They're arranged like this:
6//
7// - 0 to 8 are the eight standard colors.
8// - 9 to 15 are the eight bold colors.
9// - 16 to 231 are six blocks of six-by-six color squares.
10// - 232 to 255 are shades of grey.
11
12fn main() {
13 // First two lines
14 for c in 0..8 {
15 glow(c, c != 0);
16 print!(" ");
17 }
18 println!();
19 for c in 8..16 {
20 glow(c, c != 8);
21 print!(" ");
22 }
23 println!("\n");
24
25 // Six lines of the first three squares
26 for row in 0..6 {
27 for square in 0..3 {
28 for column in 0..6 {
29 glow(16 + square * 36 + row * 6 + column, row >= 3);
30 print!(" ");
31 }
32
33 print!(" ");
34 }
35
36 println!();
37 }
38 println!();
39
40 // Six more lines of the other three squares
41 for row in 0..6 {
42 for square in 0..3 {
43 for column in 0..6 {
44 glow(124 + square * 36 + row * 6 + column, row >= 3);
45 print!(" ");
46 }
47
48 print!(" ");
49 }
50
51 println!();
52 }
53 println!();
54
55 // The last greyscale lines
56 for c in 232..=243 {
57 glow(c, false);
58 print!(" ");
59 }
60 println!();
61 for c in 244..=255 {
62 glow(c, true);
63 print!(" ");
64 }
65 println!();
66}
67
68fn glow(c: u8, light_bg: bool) {
69 let base = if light_bg { Color::Black } else { Color::White };
70 let style = base.on(Color::Fixed(c));
71 print!("{}", style.paint(&format!(" {:3} ", c)));
72}
73