Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ANSI Colors

ANSI colors are used by CLI programs to color anything printed on the terminal.
With them it's possible to change the font effect and (background) color of a text.

PS: If you are using C# you may not need this.


Getting Started

\\033\[[\d;]+m

To print colored text we need to print an ANSI code before text (it's not displayed).
Each ANSI code has its own effect.

The base form is \033[m.
We put the codes we want between the [ and the m separated by ;.

The \033 is an escape sequence, that represents ESC.
In most programming languages the ESC escape sequence is \033, \e or \x1b.

We can also repeat code as much as we want, but never separate sequences that need to be together.


Font Effects

Some terminals don't support some font effects.
Make sure yours does.

Code Effect Note
0 Reset presets All attributes off
1 Bold
2 Light Not widely supported
3 Italic Not widely supported; Sometimes treated as inverse
4 Underline
5 Blink Less than 150 per minute
6 Rapid blink Not widely supported; MS-DOS ANSI.SYS; 150+ per minute
7 Invert colors Swap foreground and background colors
8 Conceal Not widely supported
9 Line-through Not widely supported; Characters legible, but marked for deletion
10 Primary(default) font
11–19 Alternate font Select alternate font n-10
20 Fraktur Hardly ever supported
21 Bold off or Double Underline Bold off not widely supported; double underline hardly ever supported
22 Bold or light off Neither bold nor faint
23 Not italic, not Fraktur
24 Underline off Not singly or doubly underlined
25 Blink off
27 Inverse off
28 Reveal conceal off
29 Line-through off
51 Framed
52 Encircled
53 Overlined
54 Not framed or encircled
58 Set effet color Next arguments are 5;<n> or 2;<r>;<g>;<b>, see below
55 Overlined off
60 ideogram underline Hardly ever supported
61 ideogram double underline Hardly ever supported
62 ideogram overline Hardly ever supported
63 ideogram double overline Hardly ever supported
64 ideogram stress marking Hardly ever supported
65 ideogram attributes off Reset the effects of all of 60-64

2-bit colors

We use 30-39 for foreground color,
and 40-49 for background color.

0 Black
1 Red
2 Green
3 Brown
4 Blue
5 Purple
6 Cyan
7 White
9 Default color

You might be wondering why 8 is not here, it is reserved for higher level colors.

These colors are not static since they can be changed by the user in the terminal's settings.


4-bit colors

Bright colors

Table of 4-bit colors for consoles

Now, we may use 90-97 for a brighter foreground color,
and the 100-107 for a brighter background.


8-bit colors

More possibilities (256)

Table of 256 possibilities of colors

Now, we can choose a color and use it with the pattern \e[38;5;<color>m for foreground, and \e[48;5;<color>m for background.


RGB

All the colors

To use a RGB color we have the pattern \e[38;2;<r>;<g>;<b>m for foreground, and \e[48;2;<r>;<g>;<b>m for background.









Examples

"Failing and making stupid questions are the best way to learn."

  • \e[1;38;2;124;58;237m
    • 1 - Bold
    • 38;2; < 124;58;237 > - RGB purple text color
  • \033[1;4;31m
    • 1 - Bold
    • 4 - Underline
    • 31 - Red text color
  • \x1b[1;31mSNES Manager \x1b[23mthe best
    • 1 - Bold
    • 31 - Red text color
    • 23 - Bold off


I already had all this info but this answer in the StackOverFlow (recommended) helped me to take it out of my head and taught new stuff.
Credits also for Richard (the answer's owner).

© 2023 Panda Soli