ANSI Color Codes

All ANSI terminal color codes with foreground/background and 256-color palette.

Reference for 3/4-bit, 8-bit and 24-bit ANSI terminal color codes with a rendered preview. Copy SGR foreground and background sequences for the 16-color, 256-color and truecolor palettes. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the difference between 16, 256 and truecolor ANSI?

16-color (3/4-bit) uses codes 30-37, 90-97 for foreground; 256-color (8-bit) uses ESC[38;5;n m with n from 0-255; and truecolor (24-bit) uses ESC[38;2;r;g;b m for any sRGB color. Support depends on the terminal.

Color your terminal output

ANSI SGR (Select Graphic Rendition) sequences set text color in the terminal. There are three tiers: the classic 16-color palette, the 256-color (8-bit) cube, and 24-bit truecolor for any RGB value. This tool previews each color and gives you the exact escape sequence to copy, with the ESC byte already inserted.

How it works

For the 16-color set, foreground codes are 30-37 (standard) and 90-97 (bright); background codes are 40-47 and 100-107. The 256-color palette is built from a formula: indices 0-15 are the base colors, 16-231 form a 6x6x6 RGB cube where each channel maps through the levels 0, 95, 135, 175, 215, 255, and 232-255 are a grayscale ramp. The preview reproduces that math so the swatch matches a typical terminal. Truecolor uses ESC[38;2;r;g;bm, embedding the exact red, green and blue bytes you enter.

Quick reference: common sequences

EffectSequence
Red foregroundESC[31m
Bright green foregroundESC[92m
Blue backgroundESC[44m
256-color foreground (index 202, orange)ESC[38;5;202m
Truecolor foreground (cornflower blue)ESC[38;2;100;149;237m
BoldESC[1m
Reset allESC[0m

In shell scripts, \e or \033 (octal) represent the ESC byte. In Python, \x1b or \033 works in a raw string; in Go and Rust you write \x1b directly.

Combining attributes

SGR parameters can be stacked in a single sequence by separating codes with semicolons:

ESC[1;31m   → bold red foreground
ESC[1;33;44m → bold yellow on blue background

Reset with ESC[0m at the end of any colored block, or specify just the attribute to reset: ESC[39m resets the foreground to default, ESC[49m resets the background.

Tips and notes

  • Foreground uses the 38 selector, background uses 48, both followed by ;5;n for 256-color or ;2;r;g;b for truecolor.
  • Always end colored runs with ESC[0m so the color does not bleed into later output.
  • Bright colors (90-97) are a convenient shortcut for bold-looking text without needing the 1m bold attribute — though they render differently across terminals.
  • If your terminal only supports 256 colors, pick the nearest cube index rather than truecolor; the 6×6×6 cube gives a reasonable approximation of most colors.
  • Test in the target environment: SSH sessions, CI runners, and Windows terminal emulators all have different color-support levels. Guard with a COLORTERM or TERM environment check when scripting for unknown environments.