xterm.js icon indicating copy to clipboard operation
xterm.js copied to clipboard

_extractColor misparses colon-separated RGB without color space: 38:2:R:G:B

Open ccstr opened this issue 8 months ago • 3 comments

Bug

When a program sends colon-separated true-color SGR without the optional color space identifier — e.g. \e[48:2:20:22:27m — xterm.js shifts the RGB values by one position and zeroes the blue channel.

Input Expected Actual
\e[48:2:20:22:27m #14161b (R=20 G=22 B=27) #161b00 (R=22 G=27 B=0)
\e[48:2::20:22:27m (empty colorspace) #14161b #14161b ✓
\e[48;2;20;22;27m (semicolons) #14161b #14161b ✓

Only the 4-sub-param form 38:2:R:G:B is affected. The 5-sub-param form 38:2::R:G:B and the semicolon form 38;2;R;G;B work correctly.

Reproduction

```js const { Terminal } = require('@xterm/headless'); const t = new Terminal({ cols: 80, rows: 4, allowProposedApi: true });

t.write('\x1b[48:2:20:22:27m X \x1b[0m', () => { const cell = t.buffer.active.getLine(0).getCell(1); const bg = cell.getBgColor(); console.log('R:', (bg >> 16) & 0xff, 'G:', (bg >> 8) & 0xff, 'B:', bg & 0xff); // Expected: R: 20 G: 22 B: 27 // Actual: R: 22 G: 27 B: 0 t.dispose(); }); ```

Tested on @xterm/headless 5.5.0, 6.0.0, and 6.1.0-beta.191.

Impact

neovim (0.10+) emits 48:2:R:G:B (without color space) when TERM_PROGRAM=iTerm.app. This causes a visible yellow tint across the entire UI — the zero blue channel shifts all colors toward yellow/olive.

Cause

In _extractColor (src/common/InputHandler.ts), the accu array has layout [target, CM, colorspace, R, G, B]. When colon sub-params arrive as [2, R, G, B] (4 values, no color space), they fill accu[1..4]:

accu = [48, 2, R, G, B, 0]

The code then reads accu[3] as R, accu[4] as G, accu[5] as B — but those positions now contain G, B, and 0.

The cSpace offset that compensates for the missing color space field only activates for semicolon mode (the if (accu[1]) { cSpace = 1; } branch), not when sub-params are present (the hasSubParams branch breaks before reaching it).

Spec

Per ITU T.416, the color space identifier is optional in the colon-separated form. Both 38:2:R:G:B and 38:2:<cs>:R:G:B are valid. Other terminals (kitty, foot, WezTerm, ghostty) accept both forms.

ccstr avatar Apr 02 '26 06:04 ccstr

The current implementation in xterm.js is the correct behaviour. Nowhere in the T.416 spec does it say that the colour space identifier is optional. These are the relevant bits of the spec:

If the first parameter element has the value 2, 3, or 4, the second parameter element specifies a colour space identifier referring to a colour space definition in the document profile.

If the first parameter element has the value 2, the parameter elements 3, 4, and 5, are three integers for red, green, and blue colour components. Parameter 6 has no meaning.

If the first parameter element has the value 2, 3, or 4, the parameter element 7 may be used to specify a tolerance value (an integer) and the parameter element 8 may be used to specify a colour space associated with the tolerance

The only parameters that are possibly "optional" in that definition are 6, 7, and 8. And that just means you can leave them empty, and potentially trim the empty parameters off the end of the list.

The DEC STD 070 standard explains this in more detail:

If more than one parameter value is supplied, the ordering of parameters is significant and must be preserved by the use of additional separators to replace omitted parameter values. A zero or omitted numeric parameter represents a default that can apply to that one parameter or to the entire parameter string, depending on the control function.

Note the part I've emphasised. If you're omitting a parameter in the middle of the list (like the colour space) you must include an additional separator to preserve the ordering of later parameters. The meaning of the parameter positions doesn't change randomly based on the parameter count.

So in this case, if the sequence has fewer than 5 subparameters, it's parameter 5 (the blue component) that is an omitted value, and which should be assigned a default value (typically 0).

That said, when some terminals first implemented these sequences, they got the format completely wrong - using semicolons instead of colons for the subparameter separators, and missing out the colour space identifier. There was also a brief period where they got it half right (colons but still no colour space). So over time there have been three different forms used:

  1. 2 ; <r> ; <g> ; <b>
  2. 2 : <r> : <g> : <b>
  3. 2 : <color_space> : <r> : <g> : <b>

Only option 3 is valid. Option 1 is often supported for backwards compatibility, because it's still used a lot. However, there's not a compelling reason to support option 2, because nobody should be using that.

j4james avatar Apr 02 '26 10:04 j4james

@weichensw What @j4james wrote (thanks for the detailed explanation).

in short: your example \e[48:2:20:22:27m reads R=22, G=27, B=0, as the position stays meaningful. the 20 is swallowed, as the colorspace notion is not handled by xterm.js. B=0 gets appended from the default=0 assumption for missing params in default CSI sequence parsing.

jerch avatar Apr 02 '26 14:04 jerch

Thanks @j4james and @jerch for the detailed explanation — you're right, the positions are fixed per T.416 and the color space field must be preserved with an empty separator if omitted.

Traced this to neovim's TUI code. In src/nvim/tui/tui.c, when TERM_PROGRAM=iTerm.app is detected, nvim emits:

\x1b[38:2:%p1%d:%p2%d:%p3%dm

producing 38:2:R:G:B (4 sub-params, no color space). This was introduced in neovim/neovim#6816 (2017) — the commit message cites T.416 but the format string omits the empty color space separator. It should be 38:2::%p1%d:%p2%d:%p3%dm. Went unnoticed for 9 years because iTerm2 and xterm accept both forms.

Closing this — thanks again.

ccstr avatar Apr 03 '26 07:04 ccstr