LANGUAGE/Java & Groovy 2017. 11. 11. 20:35

!markdown


# Java


`System.out.print`를 사용하면 배경과 대비되는 색깔의 글자가 출력된다.


`색을 바꿀 수 있을까`란 의문을 가졌다면, 방법은 있을까? 쓸모있을까?


터미널 기반의 프로그래밍을 한다고 하면? 필요한 순간이 있을지도 모른다.




## 형형색색 로그 만들기


### Test코드


```java

public class ColorTester {

    public static final String ANSI_RESET = "\u001B[0m";

    public static final String ANSI_BLACK = "\u001B[30m";

    public static final String ANSI_RED = "\u001B[31m";

    public static final String ANSI_GREEN = "\u001B[32m";

    public static final String ANSI_YELLOW = "\u001B[33m";

    public static final String ANSI_BLUE = "\u001B[34m";

    public static final String ANSI_PURPLE = "\u001B[35m";

    public static final String ANSI_CYAN = "\u001B[36m";

    public static final String ANSI_WHITE = "\u001B[37m";

    public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";

    public static final String ANSI_RED_BACKGROUND = "\u001B[41m";

    public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";

    public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";

    public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";

    public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";

    public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";

    public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";


    public static void main(String[] args){

        System.out.println( "\u001b[31m test" );

        System.out.println(  ANSI_GREEN_BACKGROUND + "This text has a green background but default text!" + ANSI_RESET );

        System.out.println(  ANSI_YELLOW + "This text has yellow text but a default background!" + ANSI_RESET );

        System.out.println( ANSI_BLUE + "This text has blue text but a default background!" + ANSI_RESET );

        System.out.println( ANSI_CYAN + "This text has cyan text but a default background!" + ANSI_RESET );

        System.out.println( ANSI_BLACK + "This text has black text but a default background!" + ANSI_RESET );

        System.out.println( ANSI_RED + "This text has red text but a default background!" + ANSI_RESET );

        System.out.println( ANSI_GREEN_BACKGROUND + ANSI_RED + "This text has a green background and red text!" + ANSI_RESET );

        System.out.println( yellow("This text has yellow text but a default background!") );

    }


    public static String yellow(String content){      

        return ANSI_YELLOW + content + ANSI_RESET;

    }

}

```



### 참조


https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println