Skip to content

Colorful text in ASCII

Colorful text can be a great asset when using standard shell or console based applications. In a modern terminal, using colors can help with showing errors, statues, asserts and warnings to the user using a serial connetion

ANSI defines eight color codes representing basic text colors: black, red, green, yellow, blue, magenta, cyan, and white. These can be foreground or background colors.

 

0 Black 30 \x1b[30m 40 \x1b[40m
1 Red 31 \x1b[31m 41 \x1b[41m
2 Green 32 \x1b[32m 42 \x1b[42m
3 Yellow 33 \x1b[33m 43 \x1b[43m
4 Blue 34 \x1b[34m 44 \x1b[44m
5 Magenta 35 \x1b[35m 45 \x1b[45m
6 Cyan 36 \x1b[36m 46 \x1b[46m
7 White 37 \x1b[37m 47 \x1b[47m

 

The color codes can be combined in a single sequence if separated by a semicolon. It is vital to reset the colorization using the 0m escape character.


#include <stdio.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_WHITE_BLUE_BACKGROUND "\x1b[37m\x1b[44m"
#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_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
    printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
    printf(ANSI_COLOR_WHITE_BLUE_BACKGROUND "This text is WHITE ON BLUE BG!" ANSI_COLOR_RESET "\n");
    return 0; 
}

 

Playing around with the little code bit shows the optical impressiveness of this little ASCII tweak.

 

 

Happy CCoding!

💖👨‍💻

 

 

Category
Popular Post

Leave a Comment