This tip provides a simple Java helper class for printing colored text into the console window.
Introduction
It can be very useful to emphasize some of our text prints with some colors. We can do that using a standard print command, by adding some escape sequences. Since it can be annoying to manually write the appropriate escape sequence every time we need it, I created a helper class for simplifying this task:
package sz;
public class ColoredText {
private static final String ESC_STR = "\u001B";
private static final String RESET_ESC_SEQUENCE_STR = "\u001B[0m";
private String coloredStr;
private ColoredText(String text, int color) {
coloredStr = toColoredStr(text, color);
}
private ColoredText(String initial, String text, int color) {
coloredStr = initial + toColoredStr(text, color);
}
public static ColoredText with(String text, int color) {
return new ColoredText(text, color);
}
public ColoredText plus(String text, int color) {
return new ColoredText(coloredStr, text, color);
}
public void print() {
System.out.print(coloredStr);
}
public void println() {
System.out.println(coloredStr);
}
@Override
public String toString() {
return coloredStr;
}
private String toEscSequenceStr(int color) {
int foreground = 30;
foreground += color & 0x07;
color >>= 3;
foreground += 60 * (color & 0x01);
color >>= 1;
int background = 40;
background += color & 0x07;
color >>= 3;
background += 60 * (color & 0x01);
return String.format("%s[%d;%dm", ESC_STR, foreground, background);
}
private String toColoredStr(String text, int color) {
return String.format("%s%s%s", toEscSequenceStr(color), text, RESET_ESC_SEQUENCE_STR);
}
}
This class takes a text, and a 8 bits number for the color. Then, it uses the appropriate escape sequences for defining the needed colors for the text. The given 8 bits color is constructed with a foreground color (the lower 4 bits), and a background color (the higher 4 bits).
For demonstrating the usage of this class, I created a simple program that prints a colors
table, and a colored smiley:
import sz.ColoredText;
public class Main {
public static void main(String[] args) {
ColoredText.with("Colors table (", 0x8c).
plus("hex values", 0x8a).plus("):", 0x8c).println();
printColorsTable();
System.out.println();
printSmiley();
}
private static void printColorsTable() {
for (int backgroundInx = 0; backgroundInx < 16; backgroundInx++) {
for (int foregroundInx = 0; foregroundInx < 16; foregroundInx++) {
ColoredText.with(" " + Integer.toHexString(backgroundInx) +
Integer.toHexString(foregroundInx),
(backgroundInx << 4) + foregroundInx).print();
}
System.out.println();
}
}
private static void printSmiley() {
int[][] colorsMatrix = {
{0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00},
{0x30, 0x30, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0x30, 0x30},
{0x30, 0x30, 0xf0, 0xc0, 0xf0, 0x30, 0x30, 0xf0, 0xc0, 0xf0, 0x30, 0x30},
{0x30, 0x30, 0xf0, 0xf0, 0xf0, 0x30, 0x30, 0xf0, 0xf0, 0xf0, 0x30, 0x30},
{0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30},
{0x30, 0x30, 0x30, 0x30, 0x30, 0xb0, 0xb0, 0x30, 0x30, 0x30, 0x30, 0x30},
{0x30, 0x30, 0x30, 0x30, 0x30, 0xb0, 0xb0, 0x30, 0x30, 0x30, 0x30, 0x30},
{0x30, 0x30, 0x90, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x90, 0x30, 0x30},
{0x30, 0x30, 0x30, 0x90, 0x30, 0x30, 0x30, 0x30, 0x90, 0x30, 0x30, 0x30},
{0x30, 0x30, 0x30, 0x30, 0x90, 0x30, 0x30, 0x90, 0x30, 0x30, 0x30, 0x30},
{0x30, 0x30, 0x30, 0x30, 0x30, 0x90, 0x90, 0x30, 0x30, 0x30, 0x30, 0x30},
{0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00}
};
for (int j = 0; j < 12; j++) {
for (int i = 0; i < 12; i++) {
ColoredText.with(" ", colorsMatrix[j][i]).print();
}
System.out.println();;
}
}
}
The result is:

History
- 14th January, 2022: Initial version