# Change Color Palette (OSC 4, OSC 104)

> Set, query and reset 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` 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)).
- `OSC 104 ST` with no number resets the whole palette, and with numbers,
  `OSC 104 ; 1 ; 2 ST`, just those
  ([`ResetAnsiColorRequest`](https://github.com/ThomasDickey/xterm-snapshots/blob/xterm-411a/misc.c#L3051-L3086)).
- 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` and `OSC 105`](https://control-codes.page/osc/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)).

## 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: Reset one color

Input, one step per line:

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

Expected screen:

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

### OSC4-8: Reset them all

Input, one step per line:

```text
\e]4;1;red;2;blue\a
\e]104\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-9: 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-10: 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>.
