Yes. ANSI terminals support ANSI Escape Codes. Each Control Sequence Introducer
(CSI
) Code in the following format:
ESC[<<<CODE>>>
The Ascii character 27
(the ESC
character) or 1b
in hexadecimal, followed by a left square bracket, and then a letter denoting the action to be used.
Note that a list of ;
separated parameters may be supplied directly before the <<<CODE>>>
parameter.
Now, colors use a subset of CSI
codes, the Select Graphic Rendition
codes. These are in the form:
ESC[<<<SGR>>>m
The SGR
code is passed as a parameter the CSI
. Relevant to your question are the SGR
codes 30-49. Codes 30-39 set the foreground color. Codes 40-49 set the background color.
30 - Foreground Black
31 - Foreground Red
32 - Foreground Green
33 - Foreground Yellow
34 - Foreground Blue
35 - Foreground Magenta
36 - Foreground Cyan
37 - Foreground White
40 - Background Black
41 - Background Red
42 - Background Green
43 - Background Yellow
44 - Background Blue
45 - Background Magenta
46 - Background Cyan
47 - Background White
Codes 38 and 48 are special codes. These are the ones which allow you to use RGB values. The format for these is:
ESC[38;2;<r>;<g>;<b>m (Foreground)
ESC[48;2;<r>;<g>;<b>m (Background)
Note: alternative uses include \x1b[<3 or 4>8;5;<web safe color index>m
. With this usage, there is a completely different color encoding. See the link at the top of this answer for more information.
Codes 39 and 49 set the foreground and background colors to their defaults, which are defined on a terminal-by-terminal basis.
In practice, the colors 30-37 40-47 are slightly different than what their labels say they are.
0 - Black
1 - Darker Red
2 - Darker Green
3 - Dark/Yellow or Brown (varies between terminals)
4 - Dark Blue
5 - Dark Magenta
6 - Dark Cyan
7 - Light Grey
To get light colors, you use the SGI for bold, 1
. Bold is a misleading name. It does not make the font bold. It actually increases the brightness of the text. With bold applied the colors become:
0 - Dark Grey
1 - Bright Red
2 - Bright Green
3 - Bright Yellow
4 - Bright Blue
5 - Bright Magenta
6 - Bright Cyan
7 - White
Notice: the last two blocks of colors' numbers are are relevant to both fore- and background (<index> + (30 or 40)
).
To remove all styles (back to default/normal mode) use SGR
0.
CSI
codes are not all graphical. For example, ESC[2J
will clear your terminal. ESC[<y>;<x>H
sets the cursor position (1-indexed). See the wikipedia article for more information.
Note: to test these, use echo -e
or printf
.
echo -ne "\033]11;rgb:53/18/6f\007"