Skip to content

Fix xterm-256 color table: wrong cube count and missing grayscale entries#3043

Merged
birkenfeld merged 1 commit intopygments:masterfrom
bysiber:fix/terminal256-color-table-counts
Feb 20, 2026
Merged

Fix xterm-256 color table: wrong cube count and missing grayscale entries#3043
birkenfeld merged 1 commit intopygments:masterfrom
bysiber:fix/terminal256-color-table-counts

Conversation

@bysiber
Copy link
Copy Markdown
Contributor

@bysiber bysiber commented Feb 20, 2026

Problem

The xterm-256 color table in Terminal256Formatter has two off-by-one errors:

  1. Color cube: range(217) generates 217 entries instead of 216 (6x6x6 = 216). The extra entry at index 232 contains (0,0,0) (black) instead of the correct grayscale start.

  2. Grayscale ramp: range(1, 22) generates only 21 entries instead of the correct 24 (colors 232-255). Three grayscale values are missing entirely.

Total entries: 16 + 217 + 21 = 254 instead of the correct 256. The extra cube entry shifts all grayscale indices by one, and the missing entries cause wrong color matching for grays.

Additionally, _closest_color iterates range(0, 254) which doesn't cover all entries.

Fix

  • Color cube: range(216)
  • Grayscale: range(24)
  • Closest color search: range(256)

…ries

The color cube loop used range(217) instead of range(216), generating
one extra entry (6x6x6 = 216, not 217).  The grayscale loop used
range(1, 22) generating only 21 entries instead of the correct 24
(colors 232-255).

Total was 16 + 217 + 21 = 254 instead of 16 + 216 + 24 = 256.  The
extra cube entry shifted all grayscale indices by one, and the three
missing grayscale values caused wrong color matching for lighter grays.

Also update _closest_color to iterate range(256) instead of range(254).
@birkenfeld
Copy link
Copy Markdown
Member

LGTM, thanks!

@birkenfeld birkenfeld merged commit f0c819a into pygments:master Feb 20, 2026
15 checks passed
@Anteru Anteru added this to the 2.20.0 milestone Mar 26, 2026
@Anteru Anteru added the A-theming area: changes to themes label Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-theming area: changes to themes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants