Skip to content

Standard 16 Colors in ANSI Escape Codes#

There are 8 standard colors and 8 bright colors – 16 in total. The bright colors are the same as the standard 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 Black
1 Red Red
2 Green Green
3 Yellow Yellow
4 Blue Blue
5 Magenta Magenta
6 Cyan Cyan
7 White 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 or background color. 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 or background color. 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 Black 30 40 Bright black 90 100
Red Red 31 41 Bright red 91 101
Green Green 32 42 Bright green 92 102
Yellow Yellow 33 43 Bright yellow 93 103
Blue Blue 34 44 Bright blue 94 104
Magenta Magenta 35 45 Bright magenta 95 105
Cyan Cyan 36 46 Bright cyan 96 106
White White 37 47 Bright white 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
1
2
3
4
print("This is \x1b[31mRED\x1b[0m text")
print("This is \x1b[41mRED\x1b[0m background")
print("This is \x1b[91mBRIGHT RED\x1b[0m text")
print("This is \x1b[101mBRIGHT RED\x1b[0m background")

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
1
2
3
4
from colorist import Color, BgColor

print(f"This is {Color.RED}RED{Color.OFF} text")
print(f"This is {BgColor.RED}RED{Color.OFF} background")

How it appears in the terminal:

% This is RED text
% This is RED background

Cheat Sheets#

Foreground Text Colors#

Standard Colors#

Example Color Code Escape Code Output Example
Black Black 30 \x1b[30m This is BLACK
Red Red 31 \x1b[31m This is RED
Green Green 32 \x1b[32m This is GREEN
Yellow Yellow 33 \x1b[33m This is YELLOW
Blue Blue 34 \x1b[34m This is BLUE
Magenta Magenta 35 \x1b[35m This is MAGENTA
Cyan Cyan 36 \x1b[36m This is CYAN
White White 37 \x1b[37m This is WHITE

Bright Colors#

Example Color Code Escape Code Output Example
Bright black Black 90 \x1b[90m This is BRIGHT BLACK
Bright red Red 91 \x1b[91m This is BRIGHT RED
Bright green Green 92 \x1b[92m This is BRIGHT GREEN
Bright yellow Yellow 93 \x1b[93m This is BRIGHT YELLOW
Bright blue Blue 94 \x1b[94m This is BRIGHT BLUE
Bright magenta Magenta 95 \x1b[95m This is BRIGHT MAGENTA
Bright cyan Cyan 96 \x1b[96m This is BRIGHT CYAN
Bright white White 97 \x1b[97m This is BRIGHT WHITE

Background Colors#

Standard Colors#

Example Color Code Escape Code Output Example
Black Black 40 \x1b[40m This is BLACK
Red Red 41 \x1b[41m This is RED
Green Green 42 \x1b[42m This is GREEN
Yellow Yellow 43 \x1b[43m This is YELLOW
Blue Blue 44 \x1b[44m This is BLUE
Magenta Magenta 45 \x1b[45m This is MAGENTA
Cyan Cyan 46 \x1b[46m This is CYAN
White White 47 \x1b[47m This is WHITE

Bright Colors#

Example Color Code Escape Code Output Example
Bright black Black 100 \x1b[100m This is BRIGHT BLACK
Bright red Red 101 \x1b[101m This is BRIGHT RED
Bright green Green 102 \x1b[102m This is BRIGHT GREEN
Bright yellow Yellow 103 \x1b[103m This is BRIGHT YELLOW
Bright blue Blue 104 \x1b[104m This is BRIGHT BLUE
Bright magenta Magenta 105 \x1b[105m This is BRIGHT MAGENTA
Bright cyan Cyan 106 \x1b[106m This is BRIGHT CYAN
Bright white White 107 \x1b[107m This is BRIGHT WHITE