Wednesday, May 16, 2012

print color string with C / C++

In linux, printing color string uses only printf, without any alternative libraries such as ncurses. We can do that in unix based operating system that support ANSI codes. but it not works on Windows. Mac ? I am not sure.

By feeding some magic characters to printf, you can manipulate you fore color,
Background color and even attributes. Lets look at a simple example to printf "Hello, world" in Bright Red with background black.




int
main(void)
{
    enum attr {  OFF = 0,
                          BOLD = 1,
                          UNDERSCORE = 4,
                          BLINK = 5,
                          REVERSE = 7,
                         CONCEALED = 8,
    }         attr;

    enum fg {    BLACK = 30,
                         RED,
                         GREEN,
                         YELLOW,
                         BLUE,
                         MAGENTA,
                         CYAN,
                         WHITE,
    }         fg;

    enum bg {    bgBLACK = 40,
                          bgRED,
                          bgGREEN,
                          bgYELLOW,
                          bgBLUE,
                          bgMAGENTA,
                          bgCYAN,
                          bgWHITE,
    }        bg;
    printf("\033[%d;%d;%dmhello, world", attr=BOLD, fg=RED, bg=bgBLACK);
    printf("\033[0m\n");                   // comment this line, try it out
    // you can also merge above two line, like this:
   // printf("\033[%d;%d;%dmhello, world\033[0m\n", ...);

    return 0;
}



The fore color, background color and attributes codes are shown as bellow
 
 
 
 Text attributes
       0    All attributes off
       1    Bold on
       4    Underscore (on monochrome display adapter only)
       5    Blink on
       7    Reverse video on
       8    Concealed on

    Foreground colors
       30    Black
       31    Red
       32    Green
       33    Yellow
       34    Blue
       35    Magenta
       36    Cyan
       37    White

    Background colors
       40    Black
       41    Red
       42    Green
       43    Yellow
       44    Blue
       45    Magenta
       46    Cyan
       47    White
 
 
How’s the color magic works? 0x1B is a special code that used to do all the color settings. 0x1B is hex code equivalent to decimal 27. With character(27) and a open square blacket “[“, initiate the setting. The rest of the values are (attribute);(fore color);(background color) and it ends the setting with ” m “. So entire thing will be look like this:

printf("%c[%d;%d;%dm",27,1,33,40);
 
With that the rest of your print line will be in bright yellow with background color black.
I have transparent background, What if I want my default background instead of any color?
Simple, ignore the background color, How?

printf("%c[%d;%dmHello World%c[%dm\n",27,1,33,27,0); 

The line above will print bright yellow “Hello World” with default bg color.


Reference:
Print Color Text in Command Line
ANSI CODES

PS:
this article come from
http://cc.byexamples.com/2007/01/20/print-color-string-without-ncurses/


版权声明
本博客所有的原创文章,作者皆保留版权。转载必须包含本声明,保持本文完整,并以超链接形式注明作者Saturn和本文原始地址:
https://ndtm-idea.blogspot.com/2012/05/print-color-string-with-c-c.html

0 comments:

Post a Comment