Java Terminal Color Output colorizeText(String str, String color)

Here you can find the source of colorizeText(String str, String color)

Description

Creates a string with the given textcolor.

License

Open Source License

Parameter

Parameter Description
str String to be colorized.
color Constant defined color (see constants).

Return

String with internal markup-sequences.

Declaration

public static String colorizeText(String str, String color) 

Method Source Code

//package com.java2s;
//License// w  w  w.java  2 s  .  c o  m

public class Main {
    /**
     * Defines the internal marker string
     * for style/color markups.
     */
    public static final String INTERNAL_MARKER = "\001";
    /**
     * Defines the markup representation of the graphics rendition
     * reset.<br>
     * It will reset all set colors and styles.
     */
    public static final String RESET_ALL = "a";

    /**
     * Creates a string with the given textcolor.
     *
     * @param str   String to be colorized.
     * @param color Constant defined color (see constants).
     * @return String with internal markup-sequences.
     */
    public static String colorizeText(String str, String color) {
        return INTERNAL_MARKER + color + str + INTERNAL_MARKER + RESET_ALL;
    }

    /**
     * Creates a string with the given textcolor.
     *
     * @param str    String to be colorized.
     * @param color  Constant defined color (see constants).
     * @param autoff boolean that toggles automatical appending of "attributes off"
     *               sequence.<code>true<code> if "attributes off" should be appended, false
     *               otherwise.
     * @return String with internal markup-sequences.
     */
    public static String colorizeText(String str, String color, boolean autoff) {
        if (autoff) {
            return colorizeText(str, color);
        } else {
            return INTERNAL_MARKER + color + str;
        }
    }

    /**
     * Creates a string with the given foreground and backgroundcolor.
     *
     * @param str String to be colorized.
     * @param fgc Constant defined color (see constants). Will be textcolor.
     * @param bgc Constant defined color (see constants). Will be backgroundcolor.
     * @return String with internal markup-sequences.
     */
    public static String colorizeText(String str, String fgc, String bgc) {
        return INTERNAL_MARKER + fgc + INTERNAL_MARKER + bgc.toLowerCase() + str + INTERNAL_MARKER + RESET_ALL;
    }

    /**
     * Creates a string with the given foreground and backgroundcolor.
     *
     * @param str    String to be colorized.
     * @param fgc    Constant defined color (see constants). Will be textcolor.
     * @param bgc    Constant defined color (see constants). Will be backgroundcolor.
     * @param autoff boolean that toggles automatical appending of "attributes off"
     *               sequence.<code>true<code> if "attributes off" should be appended, false
     *               otherwise.
     * @return String with internal markup-sequences.
     */
    public static String colorizeText(String str, String fgc, String bgc, boolean autoff) {
        if (autoff) {
            return colorizeText(str, fgc, bgc);
        } else {
            return INTERNAL_MARKER + fgc + INTERNAL_MARKER + bgc.toLowerCase() + str;
        }
    }
}

Related

  1. colorize(String string)
  2. colorizeBackground(String str, String color)
  3. colorizeBackground(String str, String color)
  4. colorizeBackground(String str, String color, boolean autoff)
  5. colorizeSequence(String cigar, String seq)
  6. colorizeText(String str, String color)
  7. colorizeText(String str, String fgc, String bgc)
  8. colors(String string)
  9. colorString(String input)