Pick a text color, background and styles such as bold or underline, see exactly how it looks in a terminal, and copy ready-to-run code for bash, Python, JavaScript (Node.js) and C. Basic 16 colors, the 256-color palette and 24-bit RGB truecolor are all supported.
Last updated:
Preview
Bash / shell
Python
JavaScript / Node.js
C / C++
Advertisement
Advertisement
How to use the ANSI Color Code Generator
Type the text you want to print in the terminal.
Choose a text color: one of the 16 basic colors, a number from the 256-color palette, or any RGB color.
Optionally pick a background and styles such as bold, italic or underline.
Check the preview and the escape sequence, switching to a light terminal if that is what you use.
Copy the code for your language and run it; keep the reset at the end so later output is not affected.
ANSI color code reference
Color
Text
Background
Bright text
Bright background
Black
30
40
90 (gray)
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
256-color
38;5;n
48;5;n
n = 0-255
RGB truecolor
38;2;r;g;b
48;2;r;g;b
each 0-255
Default
39
49
Style codes
Code
Effect
Turn off with
0
Reset everything
1
Bold (some terminals also brighten the color)
22
2
Dim / faint
22
3
Italic
23
4
Underline
24
5
Blink (often disabled)
25
7
Reverse foreground and background
27
8
Hidden / conceal
28
9
Strikethrough
29
How the 256-color palette is built
Numbers 0-15 are the basic and bright colors from your terminal theme. Numbers 16-231 form a 6×6×6 color cube: the index is 16 + 36×r + 6×g + b, where r, g and b are 0-5 and map to the channel levels 0, 95, 135, 175, 215 and 255. Numbers 232-255 are 24 shades of gray from rgb(8, 8, 8) to rgb(238, 238, 238) in steps of 10.
Tips
Only color interactive output. Check whether output goes to a terminal ([ -t 1 ] in shell, sys.stdout.isatty() in Python, process.stdout.isTTY in Node.js) and respect the NO_COLOR environment variable.
Do not rely on color alone. Add a word or symbol such as "error:" or ✔ so the message is clear for color-blind users and in logs.
Always reset. Forgetting \033[0m leaves the user's prompt colored after your program exits.
Frequently asked questions
What are ANSI color codes?
ANSI escape codes are special character sequences that terminals interpret as formatting instead of text. A sequence starts with the escape character (ESC, written \033 or \x1b), followed by [ , a list of numbers separated by semicolons and the letter m, for example \033[1;31m for bold red. These SGR (Select Graphic Rendition) codes change color and style until a reset \033[0m.
What is the difference between 16, 256 and truecolor?
The 16 basic colors use codes 30-37 and 90-97 for text and 40-47 and 100-107 for backgrounds, and their exact shades come from the terminal theme. The 256-color mode uses 38;5;n and 48;5;n with a fixed 6x6x6 color cube and 24 grays. Truecolor uses 38;2;r;g;b and 48;2;r;g;b for any of 16.7 million colors.
Why does my output show codes like ^[[31m or \033[31m literally?
The escape character was not interpreted. In bash, use printf or echo -e (plain echo prints backslashes literally), or $'...' strings. In Python and C use \033 inside a normal string literal. If the output goes to a file or a pager such as less, use less -R to display colors.
Do ANSI colors work on Windows?
Yes in Windows Terminal, PowerShell 7 and the VS Code terminal. The legacy Windows console only processes them when virtual terminal processing is enabled; Python programs can call os.system("") once or use the colorama package to switch it on.
How do I reset colors back to normal?
Print \033[0m (SGR 0), which turns off all colors and styles. Individual attributes can also be switched off: 22 ends bold and dim, 23 italic, 24 underline, 27 reverse, 39 restores the default text color and 49 the default background.
Should I use \033, \x1b or \e?
They all produce the same escape character (decimal 27). \033 (octal) works in bash printf, Python and C; \x1b (hex) works in Python, C and JavaScript; \e works in bash and zsh but is not standard C and is not supported by Python. JavaScript string literals need \x1b or \u001b.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.