# Change Color Palette (OSC 4)

> Set and query the colors that SGR's color numbers stand for.

- **Sequence:** `OSC 4 ; Pc ; Pt ST`
- **ECMA-48:** [§8.3.89 OSC – Operating System Command, p. 51](https://archive.org/details/ecma-48-5th-edition-june-1991/page/n64/mode/1up)
- **xterm:** [Operating System Commands](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html)

The terminal's palette is the 256 colors that [SGR](https://control-codes.page/csi/sgr/index.html.md)'s color
numbers stand for: `CSI 31 m` uses color 1, `CSI 38 ; 5 ; 196 m` color 196.
`OSC 4 ; Pc ; Pt ST` changes color `Pc` to `Pt`, and
[`OSC 104 ; Pc ST`](https://control-codes.page/osc/reset-palette/index.html.md) puts it back as it started. These are xterm's.

- `Pt` is a color in any form X11's `XParseColor` accepts: a name such as
  `red` or `light sky blue`, `#rrggbb`, or `rgb:rr/gg/bb`, where each part
  may have one to four hex digits.
- `Pt` of `?` asks what the color is. The answer is
  `OSC 4 ; Pc ; rgb:rrrr/gggg/bbbb`, sixteen bits a part, ended with
  whichever of ST or BEL ended the question.
- One sequence can carry several pairs, `OSC 4 ; 1 ; red ; 2 ; ? ST`, mixing
  changes and questions. xterm stops at the first color it cannot parse or
  number it does not have, and ignores the rest
  ([`ChangeAnsiColorRequest`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/misc.c#L2980-L3029),
  [`ReportAnsiColorRequest`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/misc.c#L2636-L2659)).
- DECSTR and RIS both reset the palette
  ([`ReallyReset`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/charproc.c#L14402-L14407)).

Where the palette starts is the host's choice, made in xterm with resources
such as `color1`. The cases are run in a terminal given a default xterm's:
black, `red3`, `green3`, `yellow3`, `blue2`, `magenta3`, `cyan3` and `gray90`,
then their bright versions, for colors 0 to 15
([`charproc.c`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/charproc.c#L650-L665)),
and the 6×6×6 cube and the gray ramp for 16 to 255
([`256colres.h`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/256colres.h)).
The answers assume a 24-bit display, where xterm reports each part of a
color as its eight bits repeated, `cd` as `cdcd`.

xterm also has [`OSC 5`](https://control-codes.page/osc/special-colors/index.html.md) and [`OSC 105`](https://control-codes.page/osc/reset-special-colors/index.html.md), which work
the same way on its *special* colors, the ones it can use for bold,
underlined, blinking, reversed and italic text. libghostty-vt does keep the palette through DECSTR, but its
DECSTR is not implemented at all (see [DECSTR](https://control-codes.page/csi/decstr/index.html.md)).

## Support

| Terminal | Version | Support | Notes |
| --- | --- | --- | --- |
| VT220 | [manual](https://vt100.net/docs/vt220-rm/), 2nd ed., 1984 | No | OSC is not among the controls it recognizes ([§4.2](https://vt100.net/docs/vt220-rm/chapter4.html#S4.2)) |
| VT510 | [manual](https://vt100.net/docs/vt510-rm/), 1st ed., 1993 | No | ignores an OSC string ([chapter 4](https://vt100.net/docs/vt510-rm/chapter4.html)) |
| xterm | patch 412, [xterm-411a](https://github.com/ThomasDickey/xterm-snapshots/tree/xterm-411a) | Yes |  |
| libghostty-vt | [0f171f6](https://github.com/ghostty-org/ghostty/tree/0f171f650a1cbbd6df4a2573b6b0c7de57cc6d90) | Yes | DECSTR does not reset it, having no DECSTR (OSC4-8) |
| Konsole | 26.11.70, [a24c3d71](https://invent.kde.org/utilities/konsole/-/tree/a24c3d71be3684f24d030aa2dbe9bd723d985233) | Partly | answers only the first `?` in a list, and sets every color in a list to the first one's |
| kitty | 0.49.1, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/kitty.json) | Partly | fails OSC4-4 of the 3 cases that show it working; ends its replies with ST where xterm uses BEL |
| foot | 1.28.0, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/foot.json) | Partly | fails OSC4-5 of the 2 cases that show it working |
| pyte | 0.8.2, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/pyte.json) | No | fails the 3 cases that show it working |
| xterm.js headless | 6.0.0, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/xtermjs.json) | No | fails the 3 cases that show it working |
| alacritty_terminal | 0.26.0, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/alacritty.json) | Partly | fails OSC4-5 of the 3 cases that show it working |
| ConEmu | build 230724, its [documentation](https://conemu.github.io/en/AnsiEscapeCodes.html) | No | not in its [ANSI escape codes](https://conemu.github.io/en/AnsiEscapeCodes.html) |
| rxvt-unicode | 9.31, [run in a VM](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/urxvt.json) | Partly | fails OSC4-4, OSC4-7 of the 3 cases that show it working; writes colors as `ff00` where xterm writes `ffff` |
| VTE | 0.84.1, [run as a library](https://git.jcollie.dev/jeff/control-codes/src/branch/main/emulators/results/vte.json) | Partly | fails OSC4-5 of the 3 cases that show it working |

## Validation

### OSC4-1: Ask for a color

Input, one step per line:

```text
\e]4;1;?\a
```

Expected screen:

```text
|____|
reply \e]4;1;rgb:cdcd/0000/0000\a
```

### OSC4-2: The answer ends as the question did

Input, one step per line:

```text
\e]4;12;?\e\\
```

Expected screen:

```text
|____|
reply \e]4;12;rgb:5c5c/5c5c/ffff\e\\
```

### OSC4-3: From the cube and the gray ramp

Input, one step per line:

```text
\e]4;196;?;244;?\a
```

Expected screen:

```text
|____|
reply \e]4;196;rgb:ffff/0000/0000\a\e]4;244;rgb:8080/8080/8080\a
```

### OSC4-4: Change a color, and text drawn in it

Input, one step per line:

```text
\e[31mA
\e]4;1;#123456\a
\e]4;1;?\a
```

Expected screen:

```text
|A___|
attr 1,1 fg=#123456
reply \e]4;1;rgb:1212/3434/5656\a
```

### OSC4-5: By name, and by rgb:

Input, one step per line:

```text
\e]4;1;light sky blue;2;rgb:f/80/abc\a
\e]4;1;?;2;?\a
```

Expected screen:

```text
|____|
reply \e]4;1;rgb:8787/cece/fafa\a\e]4;2;rgb:ffff/8080/abab\a
```

### OSC4-6: A color it cannot parse stops the rest

Input, one step per line:

```text
\e]4;1;nosuch;2;red\a
\e]4;1;?;2;?\a
```

Expected screen:

```text
|____|
reply \e]4;1;rgb:cdcd/0000/0000\a\e]4;2;rgb:0000/cdcd/0000\a
```

### OSC4-7: RIS resets the palette

Input, one step per line:

```text
\e]4;1;red\a
\ec
\e]4;1;?\a
```

Expected screen:

```text
|____|
reply \e]4;1;rgb:cdcd/0000/0000\a
```

### OSC4-8: So does DECSTR

*A known difference: libghostty-vt does not pass this case.*

Input, one step per line:

```text
\e]4;1;red\a
\e[!p
\e]4;1;?\a
```

Expected screen:

```text
|____|
reply \e]4;1;rgb:cdcd/0000/0000\a
```

---

This is the Markdown version of <https://control-codes.page/osc/palette/>. On that page every validation case runs live in libghostty-vt, the terminal emulation core of Ghostty, compiled to WebAssembly.

The validation cases are written in the notation described in <https://control-codes.page/notation/index.html.md>.
