12

I'm trying to set a win32 console application's font color to a specific RGB value like 50, 75, 90 respectively. I've already tried SetConsoleTextAttribute(), but unfortunately it seems to be limited to 0 or 255 for R, G, or B.

This has to be possible, because in the command prompt properties window you can set the specific color, like so

http://www.yourgamercard.net/screen/i/4a8c57.png

I've searched quite a bit but it seems that the only answer is SetConsoleTextAttribute().

4
  • Red, Green and Blue colors are actually within a 0 to 255 range. Any color is a combination of 3 eight bits. See also - service.real.com/help/library/guides/realone/ProductionGuide/…
    – user405725
    Mar 1, 2012 at 1:28
  • 5
    Did you even read my question? Mar 1, 2012 at 1:30
  • Of course. It is absolutely not clear what exactly is your problem. You got bitmask, you got a function - call it and make your console full of colors.
    – user405725
    Mar 1, 2012 at 1:37
  • 5
    @Vlad; it's not that simple: the Win32 console uses a color table with a 4-bit index, you can't just set any character to any RBG value. This is really a question about a Win32 feature/quirk/limitation, not about how RGB values work, which is perhaps why the OP didn't find your reply to be useful.
    – BrendanMcK
    Mar 1, 2012 at 2:47

3 Answers 3

17

You need to use SetConsoleScreenBufferInfoEx to set this, see the ColorTable entry of the CONSOLE_SCREEN_BUFFER_INFOEX struct.

Console colors are a two-level process: there's the console attribute, which has four bits each for foreground and background (red, green, blue and intensity), which appears to limit the colors to the basic primary and secondary colors. But these values are used as indices to the color table, to determine the actual display value. So think of the character attribute 'color' bits as "logical red" etc rather than physical red. (The value that Character Attribute 'red' maps to is actually RGB red by default, but doesn't have to be.) So you're always limited to 16 indexed colors; but you can set those to whatever 16 full-RGB colors you want via the ColorTable.

The strip of colored squares you see in the dialog above is essentially that color table, and lists the colors in their Character Attribute order, the first suqare being 'logical black', and so on.

0
5

Sorry for being a little late to answer but here is the code you desire:

CONSOLE_SCREEN_BUFFER_INFOEX info;
info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfoEx(hConsole, &info);

info.ColorTable[0] = RGB(0,0,0);
...
info.ColorTable[3] = RGB(135, 206, 235);
...
info.ColorTable[15] = RGB (25,25,25);

SetConsoleScreenBufferInfoEx(hConsole, &info);

With this code you can change color values of all 16 index colors to any RGB color you desire.

Than you can print line with your desired color like this:

SetConsoleTextAttribute(hConsole, 3);
std::cout << "Hello World!" << std::endl;

And here is my output: My output windows

2
  • Thanks, this is great. Do you know how to use this with the background color? This appears to only work with fore color? Feb 9, 2020 at 2:47
  • 1
    @DavidLedger info.ColorTable[0] is the color value of background
    – 552bb224
    Feb 9, 2020 at 15:43
-2

There is a way to make it so the text is completely RGB, but it requires 'SetPixel' if you make a replica of the text you can then change the RGB values, here is something I made for A, it is hard to do, but I'm making a .h file so everyone can use it, here you go:

void PrintA(int R, int G, int B)
{
    HWND myconsole = GetConsoleWindow();
    HDC mydc = GetDC(myconsole);
    SetPixel(mydc, i + 0, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 9, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 9, RGB(R, G, B));
    SetPixel(mydc, i + 2, i2 + 1, RGB(R, G, B));
    SetPixel(mydc, i + 2, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 2, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 3, i2 + 1, RGB(R, G, B));
    SetPixel(mydc, i + 3, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 3, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 9, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 9, RGB(R, G, B));
    i += 8;

    if (i / 80 == 8)
    {
        i = 0;
        i2 += 12;
    }
}
1
  • You cannot render onto a window you do not own. Using the slowest possible way to do it doesn't help either. Neither does leaking a device context. Jun 5, 2020 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.