A Rust library for terminal background color detection
Rust
108
97 commits
updated Nov 24, 2025
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.
"Windows Terminal" may be supported in a future release: https://github.com/microsoft/terminal/issues/3718.
[dependencies]
termbg = "0.6.2"
fn main() {
let timeout = std::time::Duration::from_millis(100);
println!("Check terminal background color");
let term = termbg::terminal();
let rgb = termbg::rgb(timeout);
let theme = termbg::theme(timeout);
println!(" Term : {:?}", term);
match rgb {
Ok(rgb) => {
println!(" Color: R={:x}, G={:x}, B={:x}", rgb.r, rgb.g, rgb.b);
}
Err(e) => {
println!(" Color: detection failed {:?}", e);
}
}
match theme {
Ok(theme) => {
println!(" Theme: {:?}", theme);
}
Err(e) => {
println!(" Theme: detection failed {:?}", e);
}
}
}
This crate provides a simple program to check.
$ cargo run --example termbg
Check terminal background color
Term : Tmux
Color: R=0, G=0, B=0
Theme: Dark
If the terminal is win32 console, WIN32API is used for detection.
If the terminal is xterm compatible, "Xterm Control Sequences" is used.
When these method was failed, COLORFGBG environment variable is used.
The detected RGB is converted to YCbCr. If Y > 0.5, the theme is detected as "light", otherwise "dark".
Overall
Maintained
Code review
crates.io
Latest release
Releases this year
1,061 followers · starred Aug 2022
8,313 followers · starred Jul 2021
2,722 followers · starred Apr 2023
249 followers · starred Nov 2021
A Rust library for terminal background color detection
Rust
108
97 commits
updated Nov 24, 2025
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.
"Windows Terminal" may be supported in a future release: https://github.com/microsoft/terminal/issues/3718.
[dependencies]
termbg = "0.6.2"
fn main() {
let timeout = std::time::Duration::from_millis(100);
println!("Check terminal background color");
let term = termbg::terminal();
let rgb = termbg::rgb(timeout);
let theme = termbg::theme(timeout);
println!(" Term : {:?}", term);
match rgb {
Ok(rgb) => {
println!(" Color: R={:x}, G={:x}, B={:x}", rgb.r, rgb.g, rgb.b);
}
Err(e) => {
println!(" Color: detection failed {:?}", e);
}
}
match theme {
Ok(theme) => {
println!(" Theme: {:?}", theme);
}
Err(e) => {
println!(" Theme: detection failed {:?}", e);
}
}
}
This crate provides a simple program to check.
$ cargo run --example termbg
Check terminal background color
Term : Tmux
Color: R=0, G=0, B=0
Theme: Dark
If the terminal is win32 console, WIN32API is used for detection.
If the terminal is xterm compatible, "Xterm Control Sequences" is used.
When these method was failed, COLORFGBG environment variable is used.
The detected RGB is converted to YCbCr. If Y > 0.5, the theme is detected as "light", otherwise "dark".
Overall
Maintained
Code review
crates.io
Latest release
Releases this year
1,061 followers · starred Aug 2022
8,313 followers · starred Jul 2021
2,722 followers · starred Apr 2023
249 followers · starred Nov 2021