Skip to main content

Determine if a shell has a light or dark background

Project description

Pypi Installs Supported Python Versions

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.

To see if your environment variables are color consistent, run:

$ python -m term_background

The heuristics used are 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 the environment variable COLORFGBG, and failing this, we use some defaults for some known terminals set from the TERM environment variable. MacOS has its way of querying characteristics, so we use that too if you are running on that OS.

If either NO_COLOR or CLI_THEME are set, this is noted.

When we can get pixel intensities of red, blue, and green values of the background, we can use that to determine light and dark based on the combined sum: zero values indicate an absence of a particular color, and we can 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 of comparing the background against the midway gray color and implemented that change here.

Of course, the bugs and lacunae in this code are mine.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

term_background-1.0.5.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

term_background-1.0.5-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file term_background-1.0.5.tar.gz.

File metadata

  • Download URL: term_background-1.0.5.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.10

File hashes

Hashes for term_background-1.0.5.tar.gz
Algorithm Hash digest
SHA256 50e428dfbf6a33e5076cd7898a42a77fc4c5b60e804b50c59e89ead7a22299bd
MD5 4063a05d0f222bf0af571f3f6902c656
BLAKE2b-256 32823b40dece142266e7a06d33320e9d064f46b76b3730d876d26bf4ec046134

See more details on using hashes here.

File details

Details for the file term_background-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for term_background-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 acad2edbe43b216781d69cffa71f95638f6f73cecdda7c81ad2b4f391a1dbe5b
MD5 f5eb75d4a220aa3ae0c2073926d80ac9
BLAKE2b-256 72c85e295d3be222e5f693f82a4511a870d33dd2dabfa22a2175177367cc0a00

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page