Skip to main content

bg

Macro bg 

Source
macro_rules! bg {
    ($r:expr, $g:expr, $b:expr $(,)?) => { ... };
}
Expand description

Creates a true rgb background color. R, G and B must be values in range 0..256.

Equivalent to CSI 4 8 ; 2 ; Pr ; Pg ; Pb m.

If the argument is literal, this expands to [&'static str]. Otherwise this expands to String.

Background color can be reset with RESET_BG or RESET. Note that RESET will also reset all text modes.

Printing newline with background set might fill the whole line to the end with the background color. This is why I recommend to always reset the background color before printing newline.

§Example

use termal_core::{codes, raw::term_size, Error};

let mut buf = codes::CLEAR.to_string();
let size = term_size()?;
let w = size.char_width;
let h = size.char_height - 1;
let l = (w * h).isqrt();

for y in 0..h {
    for x in 0..w {
        let r = y * 256 / h;
        let g = x * 256 / w;
        let b = 255 - (x * y).isqrt() * 256 / l;

        buf += &codes::bg!(r, g, b);
        buf.push('H');
    }
    buf += codes::RESET_BG;
    buf.push('\n');
}

print!("{buf}");

Ok::<(), Error>(())

§Result in terminal