Skip to content

Commit 180daf7

Browse files
alecthomasclaude
andcommitted
fix: remove shadowed entries from 256-colour TTY tables
Go map literals silently keep the last duplicate key, so the nine system-colour entries (indices 0 and 8-15) whose RGB values also appear in the 6x6x6 cube/greyscale ramp were dead code. The cube codes were already the effective mappings, so behaviour is unchanged; the table now says what it does. A test asserts the entry counts so dropped or shadowed entries fail fast. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent a6b9dc7 commit 180daf7

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

formatters/tty_indexed.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ var ttyTables = map[int]*ttyTable{
4444
c("#0000ff"): "\033[104m", c("#ff00ff"): "\033[105m", c("#00ffff"): "\033[106m", c("#ffffff"): "\033[107m",
4545
},
4646
},
47+
// System colours 0 and 8-15 are omitted from the 256-colour tables: their
48+
// RGB values duplicate colours in the 6x6x6 cube/greyscale ramp (indices
49+
// 16, 244, 196, 46, 226, 21, 201, 51 and 231), and duplicate map keys
50+
// silently overwrite each other. The cube/greyscale codes are preferred
51+
// as terminal themes commonly remap the system colours.
4752
256: {
4853
foreground: map[chroma.Colour]string{
49-
c("#000000"): "\033[38;5;0m", c("#800000"): "\033[38;5;1m", c("#008000"): "\033[38;5;2m", c("#808000"): "\033[38;5;3m",
54+
c("#800000"): "\033[38;5;1m", c("#008000"): "\033[38;5;2m", c("#808000"): "\033[38;5;3m",
5055
c("#000080"): "\033[38;5;4m", c("#800080"): "\033[38;5;5m", c("#008080"): "\033[38;5;6m", c("#c0c0c0"): "\033[38;5;7m",
51-
c("#808080"): "\033[38;5;8m", c("#ff0000"): "\033[38;5;9m", c("#00ff00"): "\033[38;5;10m", c("#ffff00"): "\033[38;5;11m",
52-
c("#0000ff"): "\033[38;5;12m", c("#ff00ff"): "\033[38;5;13m", c("#00ffff"): "\033[38;5;14m", c("#ffffff"): "\033[38;5;15m",
5356
c("#000000"): "\033[38;5;16m", c("#00005f"): "\033[38;5;17m", c("#000087"): "\033[38;5;18m", c("#0000af"): "\033[38;5;19m",
5457
c("#0000d7"): "\033[38;5;20m", c("#0000ff"): "\033[38;5;21m", c("#005f00"): "\033[38;5;22m", c("#005f5f"): "\033[38;5;23m",
5558
c("#005f87"): "\033[38;5;24m", c("#005faf"): "\033[38;5;25m", c("#005fd7"): "\033[38;5;26m", c("#005fff"): "\033[38;5;27m",
@@ -112,10 +115,8 @@ var ttyTables = map[int]*ttyTable{
112115
c("#d0d0d0"): "\033[38;5;252m", c("#dadada"): "\033[38;5;253m", c("#e4e4e4"): "\033[38;5;254m", c("#eeeeee"): "\033[38;5;255m",
113116
},
114117
background: map[chroma.Colour]string{
115-
c("#000000"): "\033[48;5;0m", c("#800000"): "\033[48;5;1m", c("#008000"): "\033[48;5;2m", c("#808000"): "\033[48;5;3m",
118+
c("#800000"): "\033[48;5;1m", c("#008000"): "\033[48;5;2m", c("#808000"): "\033[48;5;3m",
116119
c("#000080"): "\033[48;5;4m", c("#800080"): "\033[48;5;5m", c("#008080"): "\033[48;5;6m", c("#c0c0c0"): "\033[48;5;7m",
117-
c("#808080"): "\033[48;5;8m", c("#ff0000"): "\033[48;5;9m", c("#00ff00"): "\033[48;5;10m", c("#ffff00"): "\033[48;5;11m",
118-
c("#0000ff"): "\033[48;5;12m", c("#ff00ff"): "\033[48;5;13m", c("#00ffff"): "\033[48;5;14m", c("#ffffff"): "\033[48;5;15m",
119120
c("#000000"): "\033[48;5;16m", c("#00005f"): "\033[48;5;17m", c("#000087"): "\033[48;5;18m", c("#0000af"): "\033[48;5;19m",
120121
c("#0000d7"): "\033[48;5;20m", c("#0000ff"): "\033[48;5;21m", c("#005f00"): "\033[48;5;22m", c("#005f5f"): "\033[48;5;23m",
121122
c("#005f87"): "\033[48;5;24m", c("#005faf"): "\033[48;5;25m", c("#005fd7"): "\033[48;5;26m", c("#005fff"): "\033[48;5;27m",

formatters/tty_indexed_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ import (
88
"github.com/alecthomas/chroma/v3"
99
)
1010

11+
func TestTTYTableEntriesAreDistinct(t *testing.T) {
12+
// The 256-colour palette has only 247 distinct RGB values; see the
13+
// comment on ttyTables. A count below these values means an entry has
14+
// been dropped or shadowed by a duplicate key.
15+
tests := []struct {
16+
colours int
17+
expected int
18+
}{
19+
{8, 16},
20+
{16, 16},
21+
{256, 247},
22+
}
23+
for _, test := range tests {
24+
table := ttyTables[test.colours]
25+
assert.Equal(t, test.expected, len(table.foreground))
26+
assert.Equal(t, test.expected, len(table.background))
27+
}
28+
}
29+
1130
func TestClosestColour(t *testing.T) {
1231
actual := findClosest(ttyTables[256], chroma.MustParseColour("#e06c75"))
1332
assert.Equal(t, chroma.MustParseColour("#d75f87"), actual)

0 commit comments

Comments
 (0)