Printing Colored Text to the Console

Printing colored text to the console can be achieved using ANSI escape codes. These codes are standardized and work on most terminals, including Unix-based systems and Windows 10.

What are ANSI Escape Codes?

ANSI escape codes are a way to control the cursor location, color, and other options on terminals. They are used by prefixing the code with an ESC (escape) character, which is represented as \033 or \u001B.

Basic Color Codes

The basic color codes are:

  • Black: \033[0;30m
  • Red: \033[0;31m
  • Green: \033[0;32m
  • Yellow: \033[0;33m
  • Blue: \033[0;34m
  • Magenta: \033[0;35m
  • Cyan: \033[0;36m
  • White: \033[0;37m

To reset the color back to default, use: \033[0m

Example Usage

Here’s an example of how to print colored text:

public class Main {
    public static void main(String[] args) {
        System.out.println("\033[0;31mThis text will be red\033[0m");
        System.out.println("\033[0;32mThis text will be green\033[0m");
    }
}

Enum-based Approach

You can also use an enum to represent the different colors and styles:

enum Color {
    // Color end string, color reset
    RESET("\033[0m"),

    // Regular Colors. Normal color, no bold, background color etc.
    BLACK("\033[0;30m"),    // BLACK
    RED("\033[0;31m"),      // RED
    GREEN("\033[0;32m"),    // GREEN
    YELLOW("\033[0;33m"),   // YELLOW
    BLUE("\033[0;34m"),     // BLUE
    MAGENTA("\033[0;35m"),  // MAGENTA
    CYAN("\033[0;36m"),     // CYAN
    WHITE("\033[0;37m"),    // WHITE

    private final String code;

    Color(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return code;
    }
}

class RunApp {
    public static void main(String[] args) {

        System.out.print(Color.RED);
        System.out.println("This text will be red");
        System.out.print(Color.RESET);

        System.out.print(Color.GREEN);
        System.out.println("This text will be green");
        System.out.print(Color.RESET);
    }
}

Background Colors

You can also use background colors by prefixing the code with 4 instead of 0. For example:

  • Black background: \033[40m
  • Red background: \033[41m
  • Green background: \033[42m
  • Yellow background: \033[43m
  • Blue background: \033[44m
  • Magenta background: \033[45m
  • Cyan background: \033[46m
  • White background: \033[47m

Bold and Underline Text

You can make text bold by prefixing the code with 1 instead of 0. For example:

  • Bold black: \033[1;30m
  • Bold red: \033[1;31m

You can underline text by prefixing the code with 4 instead of 0, but after the color code. For example:

  • Underlined black: \033[4;30m
  • Underlined red: \033[4;31m

Conclusion

Printing colored text to the console is a simple process that can be achieved using ANSI escape codes. You can use these codes directly or create an enum-based approach for better readability and maintainability.

Leave a Reply

Your email address will not be published. Required fields are marked *