Terminal Colors - Chris Yeh
Terminal Colors - Chris Yeh
Chris Yeh
Terminal Colors
Posted: Mar 28, 2020.
Tags: unix
In this post, I explore how terminals display color, a two-stage process involving ANSI escape
codes and user-defined color schemes.
Contents
Overview
ASCII Escape Character
ANSI Escape Codes for Terminal Graphics
Color Schemes
Windows Colors
My Preferred Color Scheme
Overview
Terminals traditionally take an input of bytes and display them as white text on a black
background. If the input contains specific “control characters,” then the terminal may alter
certain display properties of the text, such as the color or font. Old terminals could only display
a maximum of 8 colors. However, modern computer screens are capable of displaying 24-bit
RGB color, so modern terminals have to implement a mapping of the 8 original colors to RGB
values. This mapping can usually be changed according to user-defined color schemes.
ASCII code 27 is indeed the character corresponding to the Escape key on a keyboard. However,
most shells recognize the Escape key as a control character (usually for a keyboard shortcut)
and therefore do not translate the Escape key into any text representation. Thus, each
programming language has its own method of representing the escape character within a
string:
Bash C Python 3
https://chrisyeh96.github.io/2020/03/28/terminal-colors.html 1/6
12/9/2021 Terminal Colors | Chris Yeh
Bash C Python 3
\u1b ,
Unicode – \u001b , \U0000001b
\U1b
Additional notes
For all 3 languages, any place where 1b appears, the capitalized hexadecimal 1B also
works.
Bash supports Unicode characters of the form \uXXXX ( \u + 4 hexadecimal digits) and
\UXXXXXXXX ( \U + 8 hexadecimal digits), but unlike Python, it allows for leading zeros to
be omitted.
Most major C compilers, including gcc and clang, support the non-standard \e syntax for
the escape character.
While C generally supports unicode characters of the form \uXXXX and \UXXXXXXXX , it
oddly does not support Unicode control characters. From the C18 specification on universal
character names:
A universal character name shall not specify a character whose short identifier is less
than 00A0 other than 0024 ($), 0040 (@), or 0060 (‘)
Parameter Effect
https://chrisyeh96.github.io/2020/03/28/terminal-colors.html 2/6
12/9/2021 Terminal Colors | Chris Yeh
Parameter Effect
4 singly underlined
5 slow blink
The 8 actual colors within the ranges (30-37, 40-47, 90-97, 100-107) are defined by the ANSI
standard as follows:
0 black
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white
We put these pieces together to create a SGR command. Thus, ESC[1m specifies bold (or
bright) text, and ESC[31m specifies red foreground text. We can chain together parameters; for
example, ESC[32;47m specifies green foreground text on a white background.
https://chrisyeh96.github.io/2020/03/28/terminal-colors.html 3/6
12/9/2021 Terminal Colors | Chris Yeh
The following diagram shows a complete example for rendering the word “text” in red with a
single underline.
\x1b[31;4mtext
ESC character Parameters
in Hex ASCII
Notes
For terminals that support bright foreground colors, ESC[1;3Xm is usually equivalent to
ESC[9Xm (where X is a digit in 0-7). However, the reverse does not seem to hold, at least
anecdotally: ESC[2;9Xm usually does not render the same as ESC[3Xm .
Not all terminals support every effect.
Documentation is available for Microsoft Windows Console and Linux.
Microsoft and the Linux manual pages both refer to SGR as “Set Graphics Rendition,”
instead of “Select Graphic Rendition.”
Examples
Additional Sources
Colors In Terminal: describes control sequences at a high level and covers 256-color support
ANSI Escape sequences: easy-to-read chart of control sequences
Color Schemes
The role of terminal color schemes is to map the 8 colors to RGB values. Most terminals support
an additional 8 colors corresponding to the bold or bright variants of the original colors. The
GitHub repo mbadolato/iTerm2-Color-Schemes provides a sampling of common color schemes.
Windows Colors
While the 8 standard color names are widely used within ANSI and ISO standards documents as
well as the Linux community, Microsoft uses slightly different names and ordering of colors.
The command color sets the default console foreground and background colors, and it can
also be used to list out the supported colors with color /? . The colors are named as follows:
https://chrisyeh96.github.io/2020/03/28/terminal-colors.html 4/6
12/9/2021 Terminal Colors | Chris Yeh
0 = Black 8 = Gray
Notably, Microsoft renames “cyan” to “aqua” and “magenta” to “purple,” and it names the
bold/bright variant of black as “gray.” This ordering is also the ordering of colors that appear in
the Windows Console settings.
Windows Terminal now uses the ANSI color names except that it still uses “purple” instead of
“magenta.” However, there is an open issue (as of March 28, 2020) where Microsoft seems to be
considering “magenta.”
■ black* 0, 0, 0 000000
https://chrisyeh96.github.io/2020/03/28/terminal-colors.html 5/6
12/9/2021 Terminal Colors | Chris Yeh
https://chrisyeh96.github.io/2020/03/28/terminal-colors.html 6/6