Debian Codegraph @

examples/256_colours.rs

raw symbols SCIP
1 extern crate yansi_term;
2 use yansi_term::Colour;
3
4 // This example prints out the 256 colours.
5 // They're arranged like this:
6 //
7 // - 0 to 8 are the eight standard colours.
8 // - 9 to 15 are the eight bold colours.
9 // - 16 to 231 are six blocks of six-by-six colour squares.
10 // - 232 to 255 are shades of grey.
11
12 fn 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
68 fn glow(c: u8, light_bg: bool) {
69 let base = if light_bg {
70 Colour::Black
71 } else {
72 Colour::White
73 };
74 let style = base.on(Colour::Fixed(c));
75 print!("{}", style.paint_fn(|f| write!(f, " {:3} ", c)));
76 }