256_colours.rs 1.6 KB

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