ansi256() at level 1 maps the whole grayscale ramp (232-255) to two codes, so near-white renders dim instead of bright
Environment
- chalk 6.0.0 (the behaviour is also present at HEAD
8960ada) - Node 22.23.3, darwin arm64
Summary
The level-1 downsampling added in #687 (PR "Downsample ansi256() and bgAnsi256() to 16 colors at level 1", merged as ff549c5, state MERGED) does not reach the goal its own PR body stated. It maps the whole 24-entry grayscale ramp (232-255) onto only two ANSI-16 foreground codes.
Which shade should map to bright white is ultimately a palette-mapping convention, and it would be reasonable to decide that the ramp should be flattened. What is being reported is the collapse itself: a 24-shade ramp producing two distinct output codes, where the reference implementation produces three.
Reproduction
import {Chalk} from 'chalk';
const c = new Chalk({level: 1});
c.ansi256(231)('x'); // "\u001b[97mx\u001b[39m" bright white (correct)
c.ansi256(255)('x'); // "\u001b[37mx\u001b[39m"
c.rgb(248, 248, 248)('x'); // "\u001b[37mx\u001b[39m"
c.hex('#f8f8f8')('x'); // "\u001b[37mx\u001b[39m"
// Distinct codes across the ramp:
new Set([...Array(24)].map((_, i) => c.ansi256(232 + i)('x'))).size; // 2Observed, per code:
ansi256(231)->\u001b[97m(bright, correct)ansi256(232)throughansi256(243)->\u001b[30m(12 codes, all identical)ansi256(244)throughansi256(255)->\u001b[37m(12 codes, all identical)rgb(248, 248, 248)->\u001b[37mhex('#f8f8f8')->\u001b[37m
Distinct codes across 232-255: 30, 37. Across the full 0-255 range there are 16 distinct codes in total. The reference [email protected] gives three codes over the same ramp (ansi256.ansi16(243) -> 30, (244) -> 37, (251) -> 97, (255) -> 97).
Root cause
source/vendor/ansi-styles/index.js, grayscale branch of ansi256ToAnsi, lines 215-242 as read at chalk 6.0.0:
if (code >= 232) {
red = (((code - 232) * 10) + 8) / 255;
green = red;
blue = red;
}
// ...
const value = Math.max(red, green, blue) * 2;
if (value === 0) {
return 30;
}
let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
if (value === 2) {
result += 60;
}The brightest ramp shade is (230 + 8) / 255 = 238 / 255 = 0.9333..., so value tops out at 1.8666... and never reaches exactly 2. The if (value === 2) promotion is therefore unreachable for 232-255, and a result of 37 is never promoted to 97.
The cube branch above it does reach exactly 2, because its channels are exact multiples of 1/5 (Math.floor(code / 36) / 5 and so on), which is why cube codes can and do produce 97.
Why test/chalk.js:128 does not settle this
test/chalk.js:128 at commit ff549c5 asserts ansi256(255) -> \\\u{1B}[37m. That line was added by PR #687 itself:
git log -S "ansi256(255)" -- test/chalk.jsreturns onlyff549c5, and the parent commitff549c5^:test/chalk.jscontains noansi256assertions at all.
So the assertion was written alongside the code it describes, and documents the new behaviour rather than independently establishing intent.
PR #687's body states the goal was downsampling correctly, offering rgb()/hex() as the correct contrast case:
At color level 1,
ansi256()/bgAnsi256()emitted raw 256-color escape codes that a basic 16-color terminal cannot render, whilergb()/hex()downsampled correctly.
Those same rgb() and hex() entry points produce \u001b[37m today for the near-white values above.
Upstream carries the opposite intent. ansi-styles test/test.js:28 asserts:
t.is(ansiStyles.color.ansi(ansiStyles.rgbToAnsi(255, 255, 255)), '\\\u{1B}[97m');This assertion was introduced together with result += 60 by commit 2ceace18 (2021-04-21, PR #71, author Richie Bendall, co-authored by Qix), and is still present at ansi-styles HEAD 8cf247a. It passes only because rgbToAnsi256(255, 255, 255) returns cube code 231, which never enters the ramp branch.
Scope
Affected: ansi256(), bgAnsi256(), rgb(), hex() (whenever they resolve into 232-255).
Not affected: underlineAnsi256(), which routes through the 58;5; palette-index mapping. Levels 2 and 3 are also unaffected (they emit 38;5;N or truecolor).
Venue
Filed against chalk rather than ansi-styles deliberately: chalk's package.json has no dependencies; the import map at package.json:14 wires #ansi-styles to the vendored copy at source/vendor/ansi-styles/index.js; and upstream ansi-styles has no colour-level concept and no caller routes ansi256() through ansi256ToAnsi.
Impact
Level-1 (16-colour) terminals only. Visually different greys become indistinguishable — anything at or above 244 renders as dim white. This is a cosmetic/correctness issue, with no security implications.
AI assistance disclosure
This report was prepared with AI assistance (investigation, reproduction, and history analysis); all commands and outputs were executed and reviewed by the author.
Source: chalk/chalk