Skip to content

Instantly share code, notes, and snippets.

@YellowAfterlife
Created May 19, 2013 18:43
Show Gist options
  • Save YellowAfterlife/5608552 to your computer and use it in GitHub Desktop.
Save YellowAfterlife/5608552 to your computer and use it in GitHub Desktop.
Outputs a "pallete" of colors achievable via WinAPI SetConsoleTextAttribute
/// Outputs a "pallete" of colors achievable via WinAPI SetConsoleTextAttribute
/// Full article:
/// http://ru.yal.cc/cpp-colored-text-via-winapi/ (Russian)
/// http://yal.cc/cpp-colored-text-via-winapi/ (English)
#include <stdio.h>
#include <Windows.h>
int main(int argc, char* argv[]) {
// foreground and background flags:
int fb[4] = { FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE, FOREGROUND_INTENSITY };
int bb[4] = { BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE, BACKGROUND_INTENSITY };
int attr;
// top-left corner:
printf("B\\F");
// top row:
for (int x = 0; x < 16; x++) {
attr = 0; for (int b = 0; b < 4; b++) if ((x & (1 << b)) != 0) attr |= bb[b];
SetConsoleTextAttribute(hStdOut, attr);
printf(" ");
}
printf("\n");
// rows:
for (int y = 0; y < 16; y++) {
// left column:
attr = 0; for (int b = 0; b < 4; b++) if ((y & (1 << b)) != 0) attr |= bb[b];
SetConsoleTextAttribute(hStdOut, attr);
printf(" ");
// row:
for (int x = 0; x < 16; x++) {
attr = 0;
for (int b = 0; b < 4; b++) if ((x & (1 << b)) != 0) attr |= fb[b];
for (int b = 0; b < 4; b++) if ((y & (1 << b)) != 0) attr |= bb[b];
SetConsoleTextAttribute(hStdOut, attr);
printf("\xB0\xB1\xB2"); // 176, 177, 178
}
printf("\n");
}
// reset colors:
SetConsoleTextAttribute(hStdOut,
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
char s[1]; gets(s);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment