Skip to content

Instantly share code, notes, and snippets.

@simonLeary42
Last active August 24, 2025 22:56
Show Gist options
  • Select an option

  • Save simonLeary42/c5f820129547cfe180b6ea93e7720b0f to your computer and use it in GitHub Desktop.

Select an option

Save simonLeary42/c5f820129547cfe180b6ea93e7720b0f to your computer and use it in GitHub Desktop.
convert colors from dark mode to light mode
import re
import sys
import colorsys
def clamp(x, _min, _max):
return max(_min, min(x, _max))
def hsl_clamp_l(hex: str, _min: float, _max: float):
assert hex.startswith("#") and (len(hex) == 7)
r, g, b = int(hex[1:3], 16), int(hex[3:5], 16), int(hex[5:7], 16)
h, l, s = colorsys.rgb_to_hls(r / 255, g / 255, b / 255)
l = clamp(l, _min, _max)
r_new, g_new, b_new = [int(x * 255) for x in colorsys.hls_to_rgb(h, l, s)]
return f"#{r_new:2x}{g_new:2x}{b_new:2x}"
COLOR_RE = re.compile(r"([a-z]*-?color:#[0-9a-fA-F]{6})")
for chunk in re.split(COLOR_RE, sys.stdin.read()):
if re.match(COLOR_RE, chunk) and chunk.startswith("color:"):
hex = chunk[len("color:") :]
new_hex = hsl_clamp_l(hex, 0, 0.4) # foreground color should be dark
print(f"color:{new_hex}", end="")
elif re.match(COLOR_RE, chunk) and chunk.startswith("background-color:"):
hex = chunk[len("background-color:") :]
new_hex = hsl_clamp_l(hex, 0.6, 1) # background color should be light
print(f"background-color:{new_hex}", end="")
else:
print(chunk, end="")

I convert all colors to HLS, and then ensure that all foreground colors are between 0% and 40% light, and all background colors are between 60% and 100% light.

This works specifically with the output of ANSI HTML adapter (aha), which I assume to be similar to ansi2html. Looping over re.split is just a nice way to make a traditional unixy pipeline tool.

Here are some examples of syntax highlighted git diffs with two different syntax themes:

Visual Studio Dark+Solarized
original
white background
white background with clamped L

ANSI generated with delta:

git diff | delta --color-only --syntax-theme='Solarized (dark)' | aha --black
git diff | delta --color-only --syntax-theme='Solarized (dark)' | aha
git diff | delta --color-only --syntax-theme='Solarized (dark)' | aha | python ./dark2light.py
git diff | delta --color-only --syntax-theme='Visual Studio Dark+' | aha --black
git diff | delta --color-only --syntax-theme='Visual Studio Dark+' | aha
git diff | delta --color-only --syntax-theme='Visual Studio Dark+' | aha | python ./dark2light.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment