Skip to main content

src/printing.rs

1use palette::Srgb;
3// Print the colours to the terminal, using ANSI escape codes to
4// apply formatting if desired.
5//
6// See https://alexwlchan.net/2021/04/coloured-squares/
7// See: https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797?permalink_comment_id=3857871
8pub fn print_color(c: Srgb<u8>, background: &Option<Srgb<u8>>, include_bg_colo: bool) {
9 let display_value = format!("#{:02x}{:02x}{:02x}", c.red, c.green, c.blue);
11 if include_bg_colo {
12 // If a background colour is specified, print it behind the
13 // hex strings.
14 match background {
15 Some(bg) => print!("\x1B[48;2;{};{};{}m", bg.red, bg.green, bg.blue),
16 _ => (),
17 };
19 println!(
20 "\x1B[38;2;{};{};{}m▇ {}\x1B[0m",
21 c.red, c.green, c.blue, display_value
22 );
23 } else {
24 println!("{}", display_value);
25 }