Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / Java
Tip/Trick

ColoredText - How to Print Colored Text using Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
24 Feb 2022MIT 14.6K   49   4   3
Simple Java helper class to print colored text in console window.
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:

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:

History

  • 14th January, 2022: Initial version

License

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


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiongood Pin
shuxin89824-Feb-22 7:03
shuxin89824-Feb-22 7:03 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA14-Jan-22 20:50
professionalȘtefan-Mihai MOGA14-Jan-22 20:50 
GeneralRe: My vote of 5 Pin
Shmuel Zang20-Jan-22 23:33
Shmuel Zang20-Jan-22 23:33 
Thanks.

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.