ANSI Color Codes Chart: Complete Guide
Ansi color codes have been a fundamental aspect of text-based interfaces since their inception. These codes, defined by the American National Standards Institute (ANSI), provide a standardized system for controlling text colors in terminal and console applications. Understanding and implementing ANSI color codes can enhance the user experience and make information more accessible. Let's dive into the world of ANSI color codes with this comprehensive guide.
The ANSI color codes are a system of 16 foreground and 8 background colors, accessible directly from the terminal. Each color is represented by a unique numerical code. The complete ANSI color codes chart includes: Black (30,40), Red (31,41), Green (32,42), Yellow (33,43), Blue (34,44), Magenta (35,45), Cyan (36,46), White (37,47), and the Reset code (0).
The 16 Base Colors
The first 16 ANSI color codes represent the basic color palette. They can be applied to the foreground color of the text:
| Color | Foreground Code | Background Code |
|---|---|---|
| Black | 30 | 40 |
| Red | 31 | 41 |
| Green | 32 | 42 |
| Yellow | 33 | 43 |
| Blue | 34 | 44 |
| Magenta | 35 | 45 |
| Cyan | 36 | 46 |
| White | 37 | 47 |
Background Colors
In addition to the 16 base colors, there are 8 background color codes. These can be applied to the background of the text, providing contrast and visual distinction:
Background colors use the codes 40-47, corresponding to the foreground color codes 30-37. To change the background color, use the 40-47 codes. To change both the foreground and background colors, use the colorama library in Python.
Setting and Resetting Colors
The 'reset' code (0) is used to restore the terminal's default text color and background. To set a color, use \033[ followed by the appropriate color code and a 'm'. For example, to set the foreground color to red and the background to black, use:
- \033[31m for red foreground (or 41m for red background)
- \033[40m for black background (or 30m for black foreground)
Extensions and Bright Colors
In addition to the 16 base colors, there are extended 16 colors, often referred to as 'bright' colors. These use codes 80-87 for the foreground and 90-97 for the background:
Color
Foreground Code
Background Code
Bright Black
90
100
Bright Red
91
101
Bright Green
92
102
Yellow
93
103
Bright Blue
94
104
Bright Magenta
95
105
Bright Cyan
96
106
White
97
107
With these extended colors, the total number of possible combinations reaches 256. However, not all systems support these extended colors.
Whether you're writing scripts, creating user interfaces, or enhancing log files, understanding and effectively using ANSI color codes can improve clarity, tic-tac-toe the user experience, and make complex information more easily digestible.