Skip to content

Extended VGA Palette with 256 Colors in ANSI Escape Codes#

Structure#

The VGA color palette was introduced with the IBM VGA display adapter in 1987. The 8-bit color space has 256 colors in total, covering:

  • 216 colors in a 6x6x6 color cube of red, green, and blue
  • 24 shades of gray

It works both with foreground text and background colors. Simply replace the three underscores ___ with any number from 0 to 255:

Code Placement
\x1b[38;5;___m Text
\x1b[48;5;___m Background

Sequence Parts#

For example, the sequences \x1b[38;5;123m for turquoise foreground text and \x1b[48;5;123m for turquoise background can be broken down into the following parts:

Part \x1b[ 38;5;
48;5;
123 m
Description Starts sequence, also called the Control Sequence Introducer (CSI). Select foreground text (38;5;) or background color (48;5;). Color code between 0-255. Ends sequence and calls the graphics function Select Graphic Rendition (SGR).

Examples#

For example, \x1b[38;5;166m is an orange foreground text color, and \x1b[48;5;243m is a light gray background color. When wrapped with \x1b[0m to reset the color, you can write this:

Python
print("This is \x1b[38;5;166mORANGE\x1b[0m text")
print("This is \x1b[48;5;243mLIGHT GRAY\x1b[0m background")

How it appears in the terminal:

% This is ORANGE text
% This is LIGHT GRAY background

How to Use VGA Colors with Colorist

Instead of using raw ANSI escape codes, it's convenient to use Colorist to generate them while keeping the code readable.

Simply use the ColorVGA class for foreground text colors or the BgColorVGA class for background colors. For example:

Python
1
2
3
4
5
6
7
from colorist import ColorVGA, BgColorVGA

orange = ColorVGA(166)
light_gray = BgColorVGA(243)

print(f"This is {orange}ORANGE{orange.OFF} text")
print(f"This is {light_gray}LIGHT GRAY{light_gray.OFF} background")

How it appears in the terminal:

% This is ORANGE text
% This is LIGHT GRAY background

Cheat Sheets#

Standard Palette#

 0   1   2   3   4   5   6   7 
 8   9  10 11 12 13 14 15

Examples#

Let's use color 11 for yellow text and color 14 for cyan background:

Python
print("This is \x1b[38;5;11mYELLOW\x1b[0m text")
print("This is \x1b[48;5;14CYAN\x1b[0m background")

How it appears in the terminal:

% This is YELLOW text
% This is CYAN background

Gray Scale#

232 233 234 235 236 237 238 239
240 241 242 243 244 245 246 247
248 249 250 251 252 253 254 255

Examples#

Let's use color 244 for light gray text and color 234 for dark gray background:

Python
print("This is \x1b[38;5;244mLIGHT GRAY\x1b[0m text")
print("This is \x1b[48;5;234mDARK GRAY\x1b[0m background")

How it appears in the terminal:

% This is LIGHT GRAY text
% This is DARK GRAY background

Extended Palette#

16 17 18 19 20 21
22 23 24 25 26 27
28 29 30 31 32 33
34 35 36 37 38 39
40 41 42 43 44 45
46 47 48 49 50 51
52 53 54 55 56 57
58 59 60 61 62 63
64 65 66 67 68 69
70 71 72 73 74 75
76 77 78 79 80 81
82 83 84 85 86 87
88 89 90 91 92 93
94 95 96 97 98 99
100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 129
130 131 132 133 134 135
136 137 138 139 140 141
142 143 144 145 146 147
148 149 150 151 152 153
154 155 156 157 158 159
160 161 162 163 164 165
166 167 168 169 170 171
172 173 174 175 176 177
178 179 180 181 182 183
184 185 186 187 188 189
190 191 192 193 194 195
196 197 198 199 200 201
202 203 204 205 206 207
208 209 210 211 212 213
214 215 216 217 218 219
220 221 222 223 224 225
226 227 228 229 230 231

Examples#

Let's use color 201 for pink text and color 21 for royal blue background:

Python
print("This is \x1b[38;5;201mPINK\x1b[0m text")
print("This is \x1b[48;5;21mROYAL BLUE\x1b[0m background")

How it appears in the terminal:

% This is PINK text
% This is ROYAL BLUE background