Determine if shell has a light or dark background
Project description
The Python module contained is part of a broader set of scripts
For the shell scripts, if you source this from a shell session it will set and export COLORFGBG to 0;15 for dark backgrounds and 15;0 for light backgrounds which is a convention used by some programs. Since I find this a little arcane, the program also sets and exports LC_DARK_BG to 1 for dark backgrounds and 0 for light. The LC_ (locale) assists in some ssh configurations which often will let environment variables with that prefix get passed along and set to a remote ssh session.
The heuristics it uses is to try to query the background color using an xterm control sequence.
Many, but not all, terminals support this query. So as a fallback we query environment variable COLORFGBG and failing this we use some defaults for some known terminals set from the TERM environment variable. OSX has its own way of querying characteristics so we use that too if you are running on that OS.
When we can get pixel intensities of red, blue, and green values of the background, we can use that determine light and dark based the combined sum: zero values indicate an absense of a particular color and compare that with the values of the foreground.
You can set any of these environment variables to influence the output decision.
Many thanks to Thomas Dickey, Egmont Koblinger, and Gilles, for explanations (and code!) via unix.stackexchange. John Green had the idea to compare the foreground and background colors instead comparing the background against the midway gray color and implemented that change here.
Of course bugs and lacuna in this code are mine.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for term_background-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db0b15cd158bc8257be87a0b4e7df5b38e8354b437b895dae5810f9b856d6da9 |
|
MD5 | f28fc875782c5b321c73816a4d7e4851 |
|
BLAKE2b-256 | 59724af08d7f5b7a4168b987541f6a81ef3531195743eb0c9056f0cfad54aa28 |
Hashes for term_background-1.0.1-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e16b1015460633bcd25ae3e7d65fd8ce7e48d82627ef58261b70d0882250aee |
|
MD5 | 0c56f308cb8fc69b15dd0f1fab5ff553 |
|
BLAKE2b-256 | b00d68ea1453b57580136571321db9c2a7f423afe012f2918f7031cf7ba54623 |