Terminal emulator/Colors
Introduction
The [ECMA-48 (ISO/IEC 6429, ANSI X3.64) standard, "Control functions for coded character sets", in part defines methods of using Control Sequence Introducer (CSI) commands to set colors in terminals. The CSI sequence is ESC [
, typically written in strings as \e[
or \033[
.
The CSI command for specifiying colors is known as Select Graphic Rendition (SGR), the sequence CSI n m
. In this sequence, n is a single number or a sequence of numbers separated by semicolons (';'), e.g. 1;2;3
. Setting n to 0
, or omitting a value for n entirely, instructs the terminal to reset all colors / set them to 'normal'. Thus, to reset all colors, use printf to send the appropriate CSI sequence to the terminal:
user $
printf '\e[0m'
or
user $
printf '\e[m'
More generally, a missing number is treated as 0
. For example, the sequence ;2;3
in \e[;2;3
is treated as 0;2;3
.
Specifying colors
The simplest method of specifying foreground and background colors in a terminal is via SGR parameters 30–37 (foreground) and 40–47 (background). For example, to specify a white foreground (37) on a black background (40):
user $
printf '\e[37;40m'
The following table lists the available values for foreground and background, together with their VGA and xterm (decimal) RGB mappings:
FG | BG | Color name | VGA RGB | xterm RGB |
---|---|---|---|---|
30 | 40 | Black | 0, 0, 0 | ? |
31 | 41 | Red | 170, 0, 0 | 205, 0, 0 |
32 | 42 | Green | 0, 170, 0 | 0, 205, 0 |
33 | 43 | Yellow | 170, 85, 0 | 205, 205, 0 |
34 | 44 | Blue | 0, 0, 170 | 0, 0, 238 |
35 | 45 | Magenta | 170, 0, 170 | 205, 0, 205 |
36 | 46 | Cyan | 0, 170, 170 | 0, 205, 205 |
37 | 47 | White | 170, 170, 170 | 229, 229, 229 |
90 | 100 | Bright Black (Gray) | 85, 85, 85 | 127, 127, 127 |
91 | 101 | Bright Red | 255, 85, 85 | ? |
92 | 102 | Bright Green | 85, 255, 85 | 0, 255, 0 |
93 | 103 | Bright Yellow | 255, 255, 85 | ? |
94 | 104 | Bright Blue | 85, 85, 255 | 92, 92, 255 |
95 | 105 | Bright Magenta | 255, 85, 255 | ? |
96 | 106 | Bright Cyan | 85, 255, 255 | ? |
97 | 107 | Bright White | 255, 255, 255 | ? |
8-bit color
Terminals that support 8-bit color have two additional SGR sequences to set a color: \e[38;5;nm
to select a foreground colour, and \e[48;5;nm
to select a background color, where n can be:
- 0 - 7, for the 'standard' colors specified by the SGR sequences 30 to 37;
- 8 - 15, for the 'high intensity' colors specified by the SGR sequences 90 to 97;
- 16 - 231, for the colors in the 6 × 6 × 6 cube defined by 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5);
232-255: grayscale from dark to light in 24 steps
Wikipedia provides a chart displaying the colors specified by the latter two sets of values.
24-bit color / 'truecolor'
Terminals that support 24-bit color / 'truecolor' have two additional SGR sequences to set a color: \e[38;2;r;g;bm
to select an RGB foreground color, and \e[48;2;r;g;b;m
to select an RGB background color.
Terminal emulators fully supporting 24-bit color include[1]:
Emulator | Package |
---|---|
Alacritty | x11-terms/alacritty |
GNOME Terminal | x11-terms/gnome-terminal |
Kitty | x11-terms/kitty |
Konsole | kde-apps/konsole |
rxvt-unicode ('urxvt') | x11-terms/rxvt-unicode |
st | x11-terms/st |
XTerm | x11-terms/xterm |
All emulators based on libvte (dev-libs/libvterm), such as GNOME Terminal, fully support 24-bit color.
Linux console
The Linux console / Virtual Terminal ('VT') provides an OSC escape sequence (i.e. a sequence with the prefix \e]
) to change the palette: \e]Pnrrggbb
, where:
- n is a one-digit hexadecimal value specifying the color number to set, as per the table below; and
- rrggbb specifies the RGB value of the color to be associated with that color number, with each component - rr, gg, or bb - a two-digit hexadecimal value.
Value | Color name |
---|---|
0 | Black |
1 | Red |
2 | Green |
3 | Yellow |
4 | Blue |
5 | Magenta |
6 | Cyan |
7 | White |
8 | Bright Black (Gray) |
9 | Bright Red |
A | Bright Green |
B | Bright Yellow |
C | Bright Blue |
D | Bright Magenta |
E | Bright Cyan |
F | Bright White |
For example, to set color "1" in the palette ("Red") to be the hex RGB tuple "FF,20,20",
user $
printf '\e]P1ff1010'
Or to set color "A" in the palette ("Bright Green") to be the hex RGB tuple "20,FF,20":
user $
printf '\e]Pa20ff20'
The palette can be reset with the OSC sequence \e]R
.
For further information, refer to the console_codes(7) man page.
The console_codes(7) man page notes that:
Linux "private mode" sequences do not follow the rules in ECMA-48 for private mode control sequences. In particular, those ending with ] do not use a standard terminating character. The OSC (set palette) sequence is a greater problem, since xterm(1) may interpret this as a control sequence which requires a string terminator (ST). Unlike the setterm(1) sequences which will be ignored (since they are invalid control sequences), the palette sequence will make xterm(1) appear to hang (though pressing the return-key will fix that). To accommodate applications which have been hardcoded to use Linux control sequences, set the xterm(1) resource brokenLinuxOSC to true.
Configuring color
Some emulators, such as Kitty, Konsole, and libvte-based emulators, advertise truecolor support by setting the COLORTERM variable to the value truecolor
.
Additionally, some emulators, such as Konsole and rxvt-unicode ('urxvt'), report the color scheme of the terminal via the COLORFGBG variable.
Gentoo-specific configuration
By default, if the NO_COLOR variable is set, colorization will be disabled (e.g. via the /etc/bash/bashrc.d/10-gentoo-color.bash Bash configuration file).
The /etc/portage/color.map file, described in color.map(5), contains variables that define color classes used by Portage.
The emerge option --color < y | n >
can be used to enable or disable color output:
This option will override NO_COLOR and NOCOLOR (see make.conf(5)) and may also be used to force color output when stdout is not a tty (by default, color is disabled unless stdout is a tty).
eix provides two options for controlling the use of ANSI color codes: -n
/ --nocolor
to disable them, and -F
/ --force-color
to force their use.
Miscellaneous
GNU ls(1) uses the LS_COLORS variable; the dircolors(1) program can be used to generate a line setting that variable.
Software
- app-text/ansifilter - ANSI escape code stripper and converter
See also
- Shell — command-line interpreter that offers a text-based interface to users.
External resources
Man pages
- dircolors(1) - color setup for ls
- showrgb(1) - display an rgb color-name database
- color.map(5) - custom color settings for Portage
- color(7) - representation of pixels and colors
- console_codes(7) - Linux console escape and control sequences
General
- Wikipedia: "ANSI escape code"
- Terminal Colors - Information about the level of color support provided by various emulators
References
- ↑ Terminal Colors. Retrieved on 2024-09-27.