A Rust library for terminal background color detection. The detected color is provided by RGB or theme ( dark or light ).
If you check other terminals, please report through issue.
Cargo.toml
[dependencies]
termbg = "0.1.0"
```rust fn main() { println!("Check terminal background color"); let rgb = termbg::rgb(); let theme = termbg::theme();
if let Some(rgb) = rgb {
println!(" Color: R={:x}, G={:x}, B={:x}", rgb.r, rgb.g, rgb.b);
} else {
println!(" Color: detection failed");
}
if let Some(theme) = theme {
println!(" Theme: {:?}", theme);
} else {
println!(" Theme: detection failed");
}
} ```
This crate provides a simple program to check.
console
$ cargo run
Check terminal background color
Term : Tmux
Color: R=0, G=0, B=0
Theme: Dark
If the terminal is rxvt compatible ( COLORFGBG
environment variable is defined ),
background color is detected from COLORFGBG
.
On the other hand, if the terminal is xterm compatible, "Xterm Control Sequences" is used for detection.
The detected RGB is converted to YCbCr. If Y > 0.5, the theme is detected as "light", otherwise "dark".