Skip to content

Commit f04219e

Browse files
authored
Fix vt100 6x6x6 color cube range and missing grayscale shades (232, 254-255) (#2055)
1 parent c7c629c commit f04219e

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/prompt_toolkit/output/vt100.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,17 @@ def __init__(self) -> None:
210210
colors.append((0x00, 0xFF, 0xFF)) # 14
211211
colors.append((0xFF, 0xFF, 0xFF)) # 15
212212

213-
# colors 16..232: the 6x6x6 color cube
213+
# colors 16..231: the 6x6x6 color cube
214214
valuerange = (0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF)
215215

216-
for i in range(217):
216+
for i in range(216):
217217
r = valuerange[(i // 36) % 6]
218218
g = valuerange[(i // 6) % 6]
219219
b = valuerange[i % 6]
220220
colors.append((r, g, b))
221221

222-
# colors 233..253: grayscale
223-
for i in range(1, 22):
222+
# colors 232..255: grayscale
223+
for i in range(24):
224224
v = 8 + i * 10
225225
colors.append((v, v, v))
226226

tests/test_vt100_output.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from prompt_toolkit.output.vt100 import _get_closest_ansi_color
3+
from prompt_toolkit.output.vt100 import _256_colors, _get_closest_ansi_color
44

55

66
def test_get_closest_ansi_color():
@@ -18,3 +18,14 @@ def test_get_closest_ansi_color():
1818
assert _get_closest_ansi_color(0, 255, 10) == "ansibrightgreen"
1919

2020
assert _get_closest_ansi_color(220, 220, 100) == "ansiyellow"
21+
22+
23+
def test_256_colors():
24+
# 6x6x6 cube
25+
assert _256_colors[(0, 0, 0)] == 16 # First color in cube
26+
assert _256_colors[(255, 255, 255)] == 231 # Last color in cube
27+
assert _256_colors[(95, 95, 95)] == 59 # Verifies a color between the boundaries
28+
29+
# Grayscale
30+
assert _256_colors[(8, 8, 8)] == 232 # First grayscale level
31+
assert _256_colors[(238, 238, 238)] == 255 # Last grayscale level

0 commit comments

Comments
 (0)