#include #if defined(WIN32) || defined(WIN64) // needed for GetStdHandle() and SetConsoleTextAttribute() #include #endif // the colors that can be used for the text or the background #define RED 0 #define GREEN 1 #define BLUE 2 #define YELLOW 3 #define PURPLE 4 #define CYAN 5 #define WHITE 6 #define BLACK 7 // the function that allows to change the text and colors void color(int text, int background){ int text_color = 0; int background_color = 0; #if defined(WIN32) || defined(WIN64) // to change the color of a Windows terminal switch(text){ case RED: text_color = 12; break; case GREEN: text_color = 10; break; case BLUE: text_color = 9; break; case YELLOW: text_color = 14; break; case PURPLE: text_color = 5; break; case CYAN: text_color = 11; break; case WHITE: text_color = 15; break; case BLACK: text_color = 0; break; } switch(background){ case RED: background_color = 12; break; case GREEN: background_color = 10; break; case BLUE: background_color = 9; break; case YELLOW: background_color = 14; break; case PURPLE: background_color = 5; break; case CYAN: background_color = 11; break; case WHITE: background_color = 15; break; case BLACK: background_color = 0; break; } HANDLE H=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(H,background_color*16+text_color); #else // to change the color of an ANSI terminal switch(text){ case RED: text_color = 31; break; case GREEN: text_color = 32; break; case BLUE: text_color = 34; break; case YELLOW: text_color = 33; break; case PURPLE: text_color = 35; break; case CYAN: text_color = 36; break; case WHITE: text_color = 37; break; case BLACK: text_color = 30; break; } switch(background){ case RED: background_color = 41; break; case GREEN: background_color = 42; break; case BLUE: background_color = 44; break; case YELLOW: background_color = 43; break; case PURPLE: background_color = 45; break; case CYAN: background_color = 46; break; case WHITE: background_color = 107; break; case BLACK: background_color = 1; break; } printf("\033[0;" "%d" ";" "%dm", text_color, background_color); #endif } int main(void){ color(RED, BLUE); printf("This "); color(BLUE, RED); printf("is "); color(CYAN, YELLOW); printf("an "); color(GREEN, BLUE); printf("higlhy "); color(YELLOW, GREEN); printf("colored "); color(PURPLE, WHITE); printf("text..."); color(WHITE, BLACK); }