Total number of colors available are 16. Number of available colors depends on current graphics mode and driver. For example, setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default drawing color is WHITE.
If you are going to write your program for Windows and you want to change color of text and/or background, use this: SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), attr); Where attr is a combination of values with | (bitwise OR operator), to choose whther you want to change foreground or background color.
All modern terminal emulators use ANSI escape codes to show colours and other things.
Don't bother with libraries, the code is really simple.
More info is here.
Example in C:
#include <stdio.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
int main (int argc, char const *argv[]) {
printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n");
return 0;
}
Dealing with colour sequences can get messy and different systems might use different Colour Sequence Indicators.
I would suggest you try using ncurses. Other than colour, ncurses can do many other neat things with console UI.
You can output special color control codes to get colored terminal output, here's a good resource on how to print colors.
For example:
printf("\033[22;34mHello, world!\033[0m"); // shows a blue hello world
EDIT: My original one used prompt color codes, which doesn't work :( This one does (I tested it).
You can assign one color to every functionality to make it more useful.
#define Color_Red "\33[0:31m\\]" // Color Start
#define Color_end "\33[0m\\]" // To flush out prev settings
#define LOG_RED(X) printf("%s %s %s",Color_Red,X,Color_end)
foo()
{
LOG_RED("This is in Red Color");
}
Like wise you can select different color codes and make this more generic.
#include <stdio.h>
#define BLUE(string) "\x1b[34m" string "\x1b[0m"
#define RED(string) "\x1b[31m" string "\x1b[0m"
int main(void)
{
printf("this is " RED("red") "!\n");
// a somewhat more complex ...
printf("this is " BLUE("%s") "!\n","blue");
return 0;
}
reading Wikipedia:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With