Skip to content

Instantly share code, notes, and snippets.

@agoose77
Created April 10, 2026 08:29
Show Gist options
  • Select an option

  • Save agoose77/5a3da28ac3b6244201fd79c4e43d527d to your computer and use it in GitHub Desktop.

Select an option

Save agoose77/5a3da28ac3b6244201fd79c4e43d527d to your computer and use it in GitHub Desktop.
xterm_colors = [
# colors 0..15: 16 basic colors
(0x00, 0x00, 0x00), # 0
(0xCD, 0x00, 0x00), # 1
(0x00, 0xCD, 0x00), # 2
(0xCD, 0xCD, 0x00), # 3
(0x00, 0x00, 0xEE), # 4
(0xCD, 0x00, 0xCD), # 5
(0x00, 0xCD, 0xCD), # 6
(0xE5, 0xE5, 0xE5), # 7
]
names = [
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
]
def closest_color(r, g, b):
distance = 257 * 257 * 3 # "infinity" (>distance from #000000 to #ffffff)
match = 0
for i, values in enumerate(xterm_colors):
rd = r - values[0]
gd = g - values[1]
bd = b - values[2]
d = rd * rd + gd * gd + bd * bd
if d < distance:
match = i
distance = d
return match
for name, (r, g, b) in zip(names, xterm_colors):
print(f"--ansi-{name}: rgb({r}, {g}, {b});")
print(f"--ansi-bright-{name}: var(--ansi-{name});")
i = 16
for red in range(0, 6):
for green in range(0, 6):
for blue in range(0, 6):
code = 16 + (red * 36) + (green * 6) + blue
r = red * 40 + 55 if red != 0 else 0
g = green * 40 + 55 if green != 0 else 0
b = blue * 40 + 55 if blue != 0 else 0
j = closest_color(r, g, b)
closest_name = names[j]
print(f"--ansi-palette-{i}: var(--ansi-{closest_name});")
i += 1
for gray in range(0, 24):
level = gray * 10 + 8
code = 232 + gray
i = code
j = closest_color(level, level, level)
closest_name = names[j]
print(f"--ansi-palette-{i}: var(--ansi-{closest_name});")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment