Skip to main content

Determine if 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 NO_COLOR is 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.4.tar.gz (12.3 kB view details)

Uploaded Source

Built Distributions

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

term_background-1.0.4-py311-none-any.whl (12.4 kB view details)

Uploaded Python 3.11

term_background-1.0.4-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: term_background-1.0.4.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for term_background-1.0.4.tar.gz
Algorithm Hash digest
SHA256 9c7a68d922ebeda37dc3cf866498985be7cd42b6b6fa7295c9e196ad98d11861
MD5 ea31d9868b5e87a0862048caba371b94
BLAKE2b-256 97fe3cadc1e9137b3fa0f0937943c19b3c3e0dbe2e8a4a36d79e30bb0ff4c778

See more details on using hashes here.

File details

Details for the file term_background-1.0.4-py311-none-any.whl.

File metadata

File hashes

Hashes for term_background-1.0.4-py311-none-any.whl
Algorithm Hash digest
SHA256 8c945e28e85f0a089bb3f45a6452338365c2d6842e24dd6a462dfc20b01691f0
MD5 42678f177ca82747e254b736d2ded9e3
BLAKE2b-256 4fba16a79934ced97bd12d26fcff2b4a308d0ccb7fa20dfa7e34993bba1f3b02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for term_background-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8db718c9a8dea55592e66e0cb00d35fab8795cc52269a0cb2a856e6dcf76b7ee
MD5 4f28f60c55b75aa622a8025d506695a6
BLAKE2b-256 879994e5e7e35a0ac28c8c48f7cc73a0f44d50719ac57e400ecfc75c0d603461

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