Standard 16 Colors in ANSI Escape Codes#
There are 8 normal colors and 8 bright colors – 16 in total. The bright colors are the same as the normal colors, yet with a higher intensity, and each color can be in the foreground (i.e. as text) or background.
Structure#
The 8 colors are simply black and white, plus the 6 colors of the rainbow. Firstly, the three primary colors red, green, and blue. Then the secondary colors yellow, magenta, and cyan:
Code | Color | Example |
---|---|---|
0 | Black | |
1 | Red | |
2 | Green | |
3 | Yellow | |
4 | Blue | |
5 | Magenta | |
6 | Cyan | |
7 | White |
Each color then needs to be prepended by a foreground or background option. When combining the two codes – simply replace the underscore _
with the missing color code – 34
for example is blue text, and 44
is blue background:
Code | Placement | Intensity |
---|---|---|
3_ | Text | Standard |
4_ | Background | Standard |
9_ | Text | Bright |
10_ | Background | Bright |
Sequence Parts#
Normal Colors#
For example, the sequences \x1b[31m
for red foreground text and \x1b[41m
for red background can be broken down into the following parts:
Part | \x1b[ | 3 4 | 1 | m |
---|---|---|---|---|
Description | Starts sequence, also called the Control Sequence Introducer (CSI). | Select foreground text (3 ) or background color (4 ). | Color code between 0 -7 . | Ends sequence and calls the graphics function Select Graphic Rendition (SGR). |
Bright Colors#
For example, the sequences \x1b[92m
for bright green foreground text and \x1b[102m
for bright green background can be broken down into the following parts:
Part | \x1b[ | 9 10 | 2 | m |
---|---|---|---|---|
Description | Starts sequence, also called the Control Sequence Introducer (CSI). | Select foreground text (9 ) or background color (10 ). | Color code between 0 -7 . | Ends sequence and calls the graphics function Select Graphic Rendition (SGR). |
Foreground Text and Background Colors#
To apply different color and styling options, simply replace the two underscores __
in \x1b[__m
with any of the following color codes:
Color | Example | Text | Background | Bright Example | Bright Text | Bright Background |
---|---|---|---|---|---|---|
Black | 30 | 40 | 90 | 100 | ||
Red | 31 | 41 | 91 | 101 | ||
Green | 32 | 42 | 92 | 102 | ||
Yellow | 33 | 43 | 93 | 103 | ||
Blue | 34 | 44 | 94 | 104 | ||
Magenta | 35 | 45 | 95 | 105 | ||
Cyan | 36 | 46 | 96 | 106 | ||
White | 37 | 47 | 97 | 107 | ||
Default | 39 | 49 | 99 | 109 | ||
Reset | 0 | 0 | 0 | 0 |
Tip
Remember to use \x1b[0m
every time you want to revert back to the default terminal text style. Otherwise, any color or styling may spill over and into other terminal messages.
When using Colorist, you can for example use Color.OFF
to reset the terminal text style.
Examples#
How to apply the escape color codes:
Python | |
---|---|
How it appears in the terminal:
% This is RED text
% This is RED background
% This is BRIGHT RED text
% This is BRIGHT RED background
How to Use Colors with Colorist
Instead of using raw ANSI escape codes, it's convenient to use Colorist to generate the them while keeping the code readable.
Simply use the Color
or BrightColor
classes for foreground text colors, or the BgColor
or BgBrightColor
classes for background colors. For example:
Python | |
---|---|
How it appears in the terminal:
% This is RED text
% This is RED background
Cheat Sheets#
Foreground Text Colors#
Normal Colors#
Example | Color | Code | Escape Code | Output Example |
---|---|---|---|---|
Black | 30 | \x1b[30m | This is BLACK | |
Red | 31 | \x1b[31m | This is RED | |
Green | 32 | \x1b[32m | This is GREEN | |
Yellow | 33 | \x1b[33m | This is YELLOW | |
Blue | 34 | \x1b[34m | This is BLUE | |
Magenta | 35 | \x1b[35m | This is MAGENTA | |
Cyan | 36 | \x1b[36m | This is CYAN | |
White | 37 | \x1b[37m | This is WHITE |
Bright Colors#
Example | Color | Code | Escape Code | Output Example |
---|---|---|---|---|
Black | 90 | \x1b[90m | This is BRIGHT BLACK | |
Red | 91 | \x1b[91m | This is BRIGHT RED | |
Green | 92 | \x1b[92m | This is BRIGHT GREEN | |
Yellow | 93 | \x1b[93m | This is BRIGHT YELLOW | |
Blue | 94 | \x1b[94m | This is BRIGHT BLUE | |
Magenta | 95 | \x1b[95m | This is BRIGHT MAGENTA | |
Cyan | 96 | \x1b[96m | This is BRIGHT CYAN | |
White | 97 | \x1b[97m | This is BRIGHT WHITE |
Background Colors#
Normal Colors#
Example | Color | Code | Escape Code | Output Example |
---|---|---|---|---|
Black | 40 | \x1b[40m | This is BLACK | |
Red | 41 | \x1b[41m | This is RED | |
Green | 42 | \x1b[42m | This is GREEN | |
Yellow | 43 | \x1b[43m | This is YELLOW | |
Blue | 44 | \x1b[44m | This is BLUE | |
Magenta | 45 | \x1b[45m | This is MAGENTA | |
Cyan | 46 | \x1b[46m | This is CYAN | |
White | 47 | \x1b[47m | This is WHITE |
Bright Colors#
Example | Color | Code | Escape Code | Output Example |
---|---|---|---|---|
Black | 100 | \x1b[100m | This is BRIGHT BLACK | |
Red | 101 | \x1b[101m | This is BRIGHT RED | |
Green | 102 | \x1b[102m | This is BRIGHT GREEN | |
Yellow | 103 | \x1b[103m | This is BRIGHT YELLOW | |
Blue | 104 | \x1b[104m | This is BRIGHT BLUE | |
Magenta | 105 | \x1b[105m | This is BRIGHT MAGENTA | |
Cyan | 106 | \x1b[106m | This is BRIGHT CYAN | |
White | 107 | \x1b[107m | This is BRIGHT WHITE |