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.
From Python you can call term_background.is_dark_background() which returns a True if we think the background is dark.
The heuristics used 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 Distribution
Built Distributions
File details
Details for the file term_background-1.0.2.tar.gz
.
File metadata
- Download URL: term_background-1.0.2.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96dcba8b664f272c7f1db183be70c1825e936990b65566999583f14ca326c75c |
|
MD5 | e5f44643fa6f3c37686018e8a30e9b7a |
|
BLAKE2b-256 | 24187dc8c71e6fac31a34a94225cb4f5f912439b1ef825f2332b34d7119d83ee |
File details
Details for the file term_background-1.0.2-py313-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py313-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3.13
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.13.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61b8dee21ea1496e15eebd71a1f1727459920c19e0c52a183b4ca0143fa5adb7 |
|
MD5 | 5ea060c5cec64a1402f4aedffa213899 |
|
BLAKE2b-256 | ca7b515211e4f4ea8c5380b2139c2eda5088d38dd0b8e3cab8a60c3ec7674c25 |
File details
Details for the file term_background-1.0.2-py312-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py312-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3.12
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b1f07bd95600786a671753a179e1ab38d971cac1a31758ab39968262fd4f259 |
|
MD5 | c35f26636eb1011f07feb66463c1c7e2 |
|
BLAKE2b-256 | 14b6b5b5b397fe5e302176cdaa8b446e7d387272f0962c55ab4f4404cc12b7bc |
File details
Details for the file term_background-1.0.2-py311-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py311-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3.11
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c80af48eea39bc19d134e52923264d3c3ad0b14fb33cd6b9ba9ad1eb45cea754 |
|
MD5 | f549e0177f8b281d053c6883909f7775 |
|
BLAKE2b-256 | 1a36e0ccd50bdc44a68ea04233a3bec64238ebcdd9e1e0158e31adf141c84496 |
File details
Details for the file term_background-1.0.2-py310-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py310-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.10
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e894ac4af47cdf2b5d5fab64470f912972f306af011f21687644d70d28698cc5 |
|
MD5 | 85679ea2f2e47366ab6a67f44a08b38c |
|
BLAKE2b-256 | 8237641ddef6347f9fd42638743dc745857ff1fb682dd7d1a7262de494dda259 |
File details
Details for the file term_background-1.0.2-py39-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py39-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.9
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 029291ba62573c637227801ecd43cb7388355610ffb7710d68a9406be210be80 |
|
MD5 | 13922ec079a785342f4825be29616a4c |
|
BLAKE2b-256 | 03e20b7a43b2f0c87807de51045093424297172e1644faca3ccb7ecf79fb3ac0 |
File details
Details for the file term_background-1.0.2-py38-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py38-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f4c2c4f84398d091329142d6b16e139a8c136c8c66af1c554fabcafadc7738a |
|
MD5 | adf79b605020b02942802870b545f4eb |
|
BLAKE2b-256 | b4919898424d425d8b6a51d64419a260b48cb8767ae374d90ebf8fd32b117b48 |
File details
Details for the file term_background-1.0.2-py37-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py37-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.7
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71d029eceb97313586324b16cfecf313478fa9cb8ed1379bbd3d2c81f3a8a64d |
|
MD5 | cbad674553b60609375da2518f3e2e1f |
|
BLAKE2b-256 | 2f77db99756367b5ef00418fdc14e3e5fc521266203ee89d8d3bdcc3bfc1b892 |
File details
Details for the file term_background-1.0.2-py36-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py36-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.6
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 540e48ec34fea7edf2482d23ede5ad359c1587f1c49bb7a23a35a5d47455c99d |
|
MD5 | e64a480c7da34ebca8fe024d638d639b |
|
BLAKE2b-256 | 59f873b31fb3ec42d3b8f4f0ee27e86fac140171620f9f05420654e8cc86bb42 |
File details
Details for the file term_background-1.0.2-py35-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py35-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.5
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b9836d90dd98f25f46afd24ed6e286ccd16d56f5dbb5d300d1d969fa2aceb2a |
|
MD5 | d6e47e0d8421cb73851797d887bc01e6 |
|
BLAKE2b-256 | 31ce9525eab178887653ed08568ef45d852ee8a7fc7103aa674b69ed59d63a7c |
File details
Details for the file term_background-1.0.2-py34-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py34-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3.4
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff955ca76b72d3928598149aa8b5db942204904c6b45f3cc3c050ba4996db4b7 |
|
MD5 | 23f6373dc41a646da78a90e9abaa0cf2 |
|
BLAKE2b-256 | 9fee42b93f7120f1fbf30136deec18296db119f03eb2b5d7a93bba936b10f023 |
File details
Details for the file term_background-1.0.2-py2-none-any.whl
.
File metadata
- Download URL: term_background-1.0.2-py2-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9be7556851668f393d024aa9a251cc2018a5dde1d665bfe024412d3f03fb14a3 |
|
MD5 | 231755833cfc36c459e807607238165d |
|
BLAKE2b-256 | 7ebcc90cad9f307e7b0746554fbaafae7392a5d2efac395106fc458710dca720 |