Skip to content

Instantly share code, notes, and snippets.

@YellowAfterlife
Created May 19, 2013 18:43
Show Gist options
  • Select an option

  • Save YellowAfterlife/5608552 to your computer and use it in GitHub Desktop.

Select an option

Save YellowAfterlife/5608552 to your computer and use it in GitHub Desktop.

Revisions

  1. YellowAfterlife created this gist May 19, 2013.
    42 changes: 42 additions & 0 deletions pallete.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    /// 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;
    }