ANSI 256-Color Palette - Extended Color Reference
Complete reference for the 256-color ANSI palette including the 6x6x6 color cube (codes 16-231) and 24-step grayscale ramp (codes 232-255). Escape sequence format and usage examples.
Detailed Explanation
The 256-Color ANSI Palette
The 256-color mode extends the basic 16 colors with a systematic color palette. It is accessed using the SGR codes 38;5;N for foreground and 48;5;N for background, where N is a color index from 0 to 255.
Palette Structure
The 256 colors are divided into four regions:
- Standard colors (0-7): The original 8 ANSI colors
- Bright colors (8-15): High-intensity variants of the standard colors
- 216-color cube (16-231): A 6x6x6 RGB color cube
- Grayscale ramp (232-255): 24 shades of gray from dark to light
The 6x6x6 Color Cube
Colors 16-231 form a cube where each RGB component has 6 levels:
- Levels: 0, 95, 135, 175, 215, 255
- Formula:
code = 16 + (36 * r) + (6 * g) + bwhere r, g, b are 0-5
For example:
- Code 16: RGB(0, 0, 0) - darkest
- Code 196: RGB(255, 0, 0) - pure red
- Code 46: RGB(0, 255, 0) - pure green
- Code 21: RGB(0, 0, 255) - pure blue
- Code 231: RGB(255, 255, 255) - lightest
Usage
# Orange text (code 208)
echo -e "\033[38;5;208mOrange text\033[0m"
# Purple background (code 93)
echo -e "\033[48;5;93mPurple background\033[0m"
# Combine: Bright yellow on dark blue
echo -e "\033[38;5;226;48;5;17mYellow on Blue\033[0m"
Grayscale Ramp
Colors 232-255 provide 24 grayscale values, ranging from nearly black (232, RGB 8,8,8) to nearly white (255, RGB 238,238,238). This is useful for subtle UI elements.
Terminal Support
The 256-color palette is supported by virtually all modern terminal emulators including xterm, GNOME Terminal, iTerm2, Windows Terminal, Alacritty, and Kitty. You can check support with the TERM environment variable — values like xterm-256color indicate support.
Use Case
The 256-color palette is ideal for CLI tools that need more color variety than the basic 16 but want broad terminal compatibility. Use it for syntax highlighting in terminal-based editors, colored graph output, heat maps, progress bar gradients, and themed terminal interfaces. It strikes the best balance between color richness and portability.