Click here to Skip to main content
15,179,951 members
Articles / Programming Languages / Java
Tip/Trick
Posted 14 Jan 2022

Tagged as

Stats

1.2K views
13 downloads

ColoredText - How to print colored text using Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Jan 2022MIT
This tip provides a simple Java helper class for printing colored text into the console window.

It can be very usuful 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:

Java
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:

Java
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:

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Shmuel Zang
Software Developer
Israel Israel
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA14-Jan-22 21:50
professionalȘtefan-Mihai MOGA14-Jan-22 21:50 
GeneralRe: My vote of 5 Pin
Shmuel Zang21-Jan-22 0:33
MemberShmuel Zang21-Jan-22 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.