Printing color strings uses only printf, without any alternative libraries such as ncurses? Yes, we can do that in any unix based operating system that support ANSI codes, I am not sure whether it works on either windows or mac, have no chance to try at the moment.
By feeding some magic characters to printf, you can manipulate your fore color, background color and even attributes. Lets look at a simple example to print “Hello World” in Bright Red with background black.
#define BRIGHT 1
#define RED 31
#define BG_BLACK 40
printf("%c[%d;%d;%dmHello World", 0x1B, BRIGHT,RED,BG_BLACK);
Okay, the code above print the string with bright red but the color setting will remain, to reset back the color to default, refers the code as bellow:
printf("%c[%dm", 0x1B, 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
Pingback: c/c++ programming by examples
Pingback: writting a fun cli apps with ncurses
……….excellent
Tyvm
Works great in PHP (cli) as well for those without the ncurses lib compiled in, or the PECL library.
nice
but can u write the full program
I mean with #includ
int main()
…………..
please …. I need it alot
thanks *_*
#include “stdio.h”
#define BRIGHT 1
#define RED 31
#define BG_BLACK 40
int main()
{
printf(“%c[%d;%d;%dmHello World”, 0x1B, BRIGHT,RED,BG_BLACK);
}
Can u help me with this code.A program to read a string as well as color and to print it in that font color.Once system receives these two arguments in the function , compiler will return the required result as follows:-
Input:
Enter the string: Can’t believe it’s possible in C.
Enter the colour: Red
Output: Can’t believe it’s possible in C.//(this has to be in “RED”colour)
Doesn’t work in Dev-C++ (as .C).
What is wrong?
Copied the code above and then corrected some, such as the “-signs and the x between 0 and 1B, but only writes
a “leftarrow” and then: [1;31;40mHello World
and still in only white on black background.
thank you very much, mister
You can alsow define your definition of function for example:
bufig_printf (const char *tekst) {
printf (“%c[%d;%d;%d;%dm%s”, 0x1B, 1, 4, 5, GREEN, tekst);
printf (“%c[%dm”, 0x1B, 0); // Restoring default settings
}
thanks very much
thanks, helped me a lot in college
Its not work in g++ compiler.
How can we used color text in g++ compiler.
we want to make colored home page. is it possible???
can you show how this came be done in windows ?
you can make it more usable and more pretty :
// define buffers
char cYellow[40];
char cEnd[40];
// define colors
sprintf( (char*) &cYellow, "%c[1;33m", 27 );
sprintf( (char*) &cReset, "%c[0m", 27 );
// use colors
printf( " %s My Color-Text %s \n", cYellow, cReset );
small mistake: change cEnd[40] to cReset[40] above.
possibly
instead:
sprintf( (char*) &cYellow, “%c[1;33m”, 27 );
use:
sprintf( (char*) &cYellow, “\x1B[1;33m” );
Pingback: want to know error in the program ... plz help ...
This worked on my Mac running OS X 10.7.3 using NetBeans C/C++ plugin. Thanks for the tip!