ANSI escape codes set the attributes and positioning of text in a terminal. A common use of these codes is to set the text color.
The code "\033[3xm" sets the foreground color x. These values are the decimal representation of a three-bit BGR color:
| blue | green | red | binary | decimal (x) | color |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 000 | 0 | black |
| 0 | 0 | 1 | 001 | 1 | red |
| 0 | 1 | 0 | 010 | 2 | green |
| 0 | 1 | 1 | 011 | 3 | yellow (green+red) |
| 1 | 0 | 0 | 100 | 4 | blue |
| 1 | 0 | 1 | 101 | 5 | magenta (blue+red) |
| 1 | 1 | 0 | 110 | 6 | cyan (blue+green) |
| 1 | 1 | 1 | 111 | 7 | white (blue+green+red) |
For example, to set the foreground color red, use "\033[31m".
To reset all text attributes, use "\033[0m".