

This crate answers the question "Is the terminal dark or light?".
It provides
A use case in a TUI is to determine what set of colors would be most suitable depending on the terminal's background:
let should_use_light_skin = terminal_light::luma()
.map_or(false, |luma| luma > 0.6);
If you have very specialized skins, you may choose a more precise switch:
match terminal_light::luma() {
Ok(luma) if luma > 0.85 => {
// Use a "light mode" skin.
}
Ok(luma) if luma < 0.2 => {
// Use a "dark mode" skin.
}
_ => {
// Either we couldn't determine the mode or it's kind of medium.
// We should use an itermediate skin, or one defining the background.
}
}
Here are the various strategies automatically used by terminal-light to determine the background:
$COLORFGBG strategyThis environment variable is set by some terminals, like konsole or the rxvt family.
It can also be set by users.
Its value is like 15;0 where the second number is the ANSI code for the background color.
Upside:
Downsides:
0 is "dark" and 15 is "light" but the real RGB color is uncertain as the low ANSI codes are often modified by the userModern terminals implement this xterm extension: a query making it possible to know the background color as RGB.
Terminal-light sends the query to stdout, waits for the answer on stdin with a timeout of 20ms, then analyses this answer.
Upsides:
Downsides:
$COLORFGBG strategyTlError::Unsupported error335 followers · starred Apr 2024


This crate answers the question "Is the terminal dark or light?".
It provides
A use case in a TUI is to determine what set of colors would be most suitable depending on the terminal's background:
let should_use_light_skin = terminal_light::luma()
.map_or(false, |luma| luma > 0.6);
If you have very specialized skins, you may choose a more precise switch:
match terminal_light::luma() {
Ok(luma) if luma > 0.85 => {
// Use a "light mode" skin.
}
Ok(luma) if luma < 0.2 => {
// Use a "dark mode" skin.
}
_ => {
// Either we couldn't determine the mode or it's kind of medium.
// We should use an itermediate skin, or one defining the background.
}
}
Here are the various strategies automatically used by terminal-light to determine the background:
$COLORFGBG strategyThis environment variable is set by some terminals, like konsole or the rxvt family.
It can also be set by users.
Its value is like 15;0 where the second number is the ANSI code for the background color.
Upside:
Downsides:
0 is "dark" and 15 is "light" but the real RGB color is uncertain as the low ANSI codes are often modified by the userModern terminals implement this xterm extension: a query making it possible to know the background color as RGB.
Terminal-light sends the query to stdout, waits for the answer on stdin with a timeout of 20ms, then analyses this answer.
Upsides:
Downsides:
$COLORFGBG strategyTlError::Unsupported error335 followers · starred Apr 2024