ANSI True Color (24-bit RGB) - 16 Million Colors in Terminal
Use 24-bit true color in terminals with ANSI escape codes 38;2;R;G;B for foreground and 48;2;R;G;B for background. Support guide and examples for all major terminals.
Detailed Explanation
24-bit True Color in Terminals
True color (also called 24-bit color or direct color) allows you to use any of the 16.7 million RGB colors in your terminal output. This is the most flexible ANSI color mode, matching the full color range of web and desktop applications.
Escape Sequence Format
- Foreground:
\033[38;2;R;G;Bmwhere R, G, B are 0-255 - Background:
\033[48;2;R;G;Bmwhere R, G, B are 0-255
Examples
# Custom orange foreground
echo -e "\033[38;2;255;165;0mOrange text\033[0m"
# Specific brand color (#6366F1 - Indigo)
echo -e "\033[38;2;99;102;241mBrand color\033[0m"
# Gradient effect
for i in $(seq 0 5 255); do
echo -en "\033[38;2;${i};0;$((255-i))m#\033[0m"
done
echo
Converting Hex to ANSI True Color
To convert a hex color like #FF6B35:
hex_color = "FF6B35"
r = int(hex_color[0:2], 16) # 255
g = int(hex_color[2:4], 16) # 107
b = int(hex_color[4:6], 16) # 53
print(f"\033[38;2;{r};{g};{b}mColored text\033[0m")
Terminal Support
True color is supported in:
- Full support: iTerm2, Windows Terminal, Alacritty, Kitty, WezTerm, GNOME Terminal (3.18+), Konsole (18.07+), foot
- Partial/No support: macOS Terminal.app (no support), older PuTTY versions, tmux (requires
TcorRGBterminfo capability)
Detecting True Color Support
# Check COLORTERM environment variable
if [ "$COLORTERM" = "truecolor" ] || [ "$COLORTERM" = "24bit" ]; then
echo "True color supported"
fi
Fallback Strategy
For maximum compatibility, implement a fallback chain: true color > 256-color > 16-color > no color. Check the COLORTERM and TERM environment variables to determine the best color mode.
Use Case
True color is ideal for terminal applications that need pixel-perfect color accuracy, such as image viewers in terminal (chafa, img2txt), terminal-based design tools, color picker utilities, and applications that need to match specific brand colors. Modern CLI tools like bat, delta, and gum use true color for rich syntax highlighting and UI themes.