5

I'm trying to output colored output for my java game, which is only using terminal and I would like to output colorful things but using precise colors using RGB values or hex colors how can I do I already saw the coloring with things like

"\u001B[0m"

but there's not enough diversity, how can I do ?

lolozen
  • 386
  • 1
  • 4
  • 19

1 Answers1

3

The ANSI escape color codes have several different variants. The oldest and most widely supported is 3-bit & 4-bit colors discussed in other answers. However, there are 2 more newer revisions of the standard which provide higher numbers of colors:

  • 8-bit (256 colors)
  • 24-bit "True Color"

Support for each color range is dependent upon which terminal implementation you're running on. 24-bit "true color" foreground & background color is supported on at least the following terminals: Xterm, KDE's Konsole, iTerm, and all libvte based terminals (including GNOME Terminal).

According to this article, as of roughly September 22nd, 2016 the Windows 10 console has been updated to support "24-bit RGB true color" (as of Windows 10 Insiders Build #14931). The article does not explicitly state that this 24-bit color is ANSI compatible, but it does reference using the "Windows Subsystem for Linux (WSL)" to run "some Linux scripts and tools to demonstrate the Console’s new 24-bit color support". So, I would assume that it is ANSI Compatible.

The complete Wiki article for the ANSI color standard can be found here.


Usage:

A quick recap for those who have never used ANSI escape codes. To use an ANSI escape sequence, treat it as a string, and print it to the console. This will change the style of the console for all text that gets printed after it.

You can reset the styling, but printing the escape sequence:

\033[0m

Which will reset all styling to defaults.

Bash Script 4-bit Example:

echo -e "\033[31mHello, World!\033[0m"

Java Example 4-bit Example:

String escapeCode = "\033[31m";
String resetCode = "\033[0m";

System.out.println(escapeCode + "Hello, World!");
System.out.println("Some more stuff I want to say.");
System.out.println(resetCode);
System.out.println("Now styling is default again.");

Preface:

The ANSI escape sequence begins with an escape character, but that character can be written a few different ways, depending upon how you want to format your numbers. All of the following are equivalent:

\033
\u001B

//The character with the numeric value of 27.
//Example in Java:
char escapeChar = (char) 27; 

8-bit (256 Colors)

Text Color (Foreground)

\033[38;5;__m

Background Color

\033[48;5;__m

'm' terminates the escape sequence. Replace the '__' with a number from 0-255. See the wiki for the conversion table.

Ex (Teal - FG): \033[38;5;79m
Ex (Pink - FG): \033[38;5;207m

Ex (Teal - BG): \033[48;5;79m
Ex (Pink - BG): \033[48;5;207m

24-bit "True Color"

Text Color (Foreground)

\033[38;2;<R>;<G>;<B>m

Background Color

\033[48;2;<R>;<G>;<B>m

'm' terminates the escape sequence. Replace the '<R/G/B>' with a number from 0-255. See this article for some charts showing all the possible colors.

Ex (Teal - FG): \033[38;2;27;161;147m
Ex (Pink - FG): \033[38;2;255;143;184m

Ex (Teal - BG): \033[48;2;27;161;147m
Ex (Pink - BG): \033[48;2;255;143;184m

You can also combine them into one giant escape sequence.

Ex (Pink BG & Teal FG): \033[48;2;255;143;184;38;2;27;161;147m

There are other text decorations that you can use too, such as underline, strikethrough, bold, faint, invert FG & BG, etc. See this table.

Note: SGR codes 2 & 5 are overridden when manipulating the 8-bit & 24-bit color codes. Ex: "38;5;" means 8-bit foreground text color, even though on it's own, "5" means slow blink.

Yurelle
  • 348
  • 2
  • 13