Skip to content

Instantly share code, notes, and snippets.

@blakepell
Created October 20, 2021 14:43
Show Gist options
  • Save blakepell/ddfb7adf590e4dfccb12d7734124990b to your computer and use it in GitHub Desktop.
Save blakepell/ddfb7adf590e4dfccb12d7734124990b to your computer and use it in GitHub Desktop.
C program to echo ANSI colors (8, 16 and 256)
#include <stdio.h>
#define CLEAR "\x1b[0m"
#define HEADER_COLOR "\x1b[31;1;4m"
/*
* Displays the ANSI colors in the 8 set, the 16 set and the 256 set.
*/
int main()
{
int item = 0;
int code = 0;
printf("\r\n\%s8 ANSI Color Codes%s\r\n\r\n", HEADER_COLOR, CLEAR);
for (int i = 30; i <=37; i++)
{
printf("\033[0;%dm%2d ", i, i);
}
printf("\r\n\r\n\%s16 ANSI Color Codes%s\r\n\r\n", HEADER_COLOR, CLEAR);
for (int i = 30; i <=37; i++)
{
printf("\033[0;%dm%2d \033[1;%dm%2d ", i, i, i, i);
}
printf("\r\n\r\n\%s256 ANSI Color Codes%s\r\n\r\n", HEADER_COLOR, CLEAR);
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
item++;
int code = i * 16 + j;
printf("\033[38;5;%dm%3d ", code, code);
if (item % 20 == 0)
{
printf("\r\n");
}
}
}
// Reset terminal color
printf("\r\n\r\n%s", CLEAR);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment