3

I want to set the text color of the console to a RGB color. I created a function to get the ColorTable of the console and change the colors in it, but it doesn't work. I don't know how to set the text color to a value from the color table so I just change the whole color table, but it doesn't do anything.

void setColor(int r, int g, int b)
{
    COLORREF cr;
    cr = RGB(r, g, b);
    PCONSOLE_SCREEN_BUFFER_INFOEX ci;
    CONSOLE_SCREEN_BUFFER_INFOEX cir;
    ci = ○
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfoEx(hConsole , ci);
    cout <<hex<< ci->ColorTable[2];
    for(int i=0;i<16;i++){
            ci->ColorTable[i] = cr;
    }
    SetConsoleScreenBufferInfoEx(hConsole, ci);

    GetConsoleScreenBufferInfoEx(hConsole , ci);

    cout <<endl <<  ci->ColorTable[2];
}

In main() I invoke the function multiple times, but the output is the same every call and the color doesn't change. SetConsoleScreenBufferInfoEx() and GetConsoleScreenBufferInfoEx() don't seem to do anything, ci remains unchanged when they are called.

What do I do wrong?

Also, if it worked I assume the background color would also get changed because I change the whole pallette, so how do I set the text color to a specific value from the color table, e.g. i put ci->ColorTable[2] = cr; in the changeColor() function instead of the for loop, how can I set the text color to the color that is now stored in ColorTable[2]?

user1950929
  • 874
  • 1
  • 9
  • 17
  • I'm not sure if this is useful, but you could take a look at related questions http://stackoverflow.com/questions/8285825/how-to-change-text-or-background-color-in-a-windows-console-application and http://stackoverflow.com/questions/4053837/c-colors-in-console-different-colors-in-different-text. – Daniel Daranas Jun 27 '13 at 16:14

2 Answers2

5

You need to use SetConsoleTextAttribute to set the current text color and background color, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047(v=vs.85).aspx for details.

Mgetz
  • 5,108
  • 2
  • 33
  • 51
  • thanks, but with this function I don't have RGB colors. I just found this question http://stackoverflow.com/questions/9509278/rgb-specific-console-text-color-c, the accepted answer tells to modify the color table like I tried to do and then select the color from the color table via `SetConsoleTextAttribue` (if I understood it correctly). However I don't get the color table modified. – user1950929 Jun 27 '13 at 18:04
  • have you checked the return value of `SetConsoleScreenBufferInfoEx`? to see if it's succeeding? And called `GetLastError` if it's failing? – Mgetz Jun 27 '13 at 18:10
  • just tested, `getlastError()` returns `0x57` which is `ERROR_INVALID_PARAMETER` for both `SetConsoleScreenBufferInfoEx` and `GetConsoleScreenBufferInfoEx`. Any Idea why the parameter could be Invalid? Maybe I'm using the wrong handle? – user1950929 Jun 27 '13 at 18:15
  • no you forgot to set the cbSize member of the `CONSOLE_SCREEN_BUFFER_INFOEX` structure you're passing in – Mgetz Jun 27 '13 at 18:21
  • thanks, works now. I assume I can only have 16 different colors at a time. is that right? – user1950929 Jun 27 '13 at 18:34
  • According to the structure definition yes it appears that is the case – Mgetz Jun 27 '13 at 18:35
4

A Windows console color table looks like this:

Color            Background Foreground
---------------------------------------------
Black            0           0
Blue             1           1
Green            2           2
Cyan             3           3
Red                  4           4
Magenta          5           5
Brown            6           6
White            7           7
Gray             -           8
Intense Blue     -           9
Intense Green    -           10
Intense Cyan     -           11
Intense Red          -           12
Intense Magenta  -           13
Yellow           -           14
Intense White    -               15

To set background colors you have to combine the foreground color code with the background color code using this equation:

finalcolor = (16*backgroundcolor) + foregroundcolor

if you want to set a text color that has a blue background and white text you simply look up the color code in the table. Blue is 1 and white is 15;

Hence int backgroundcolor=1; and int foregroundcolor=15;

#include <windows.h>
#include <iostream> 
using namespace std;

void setcolor(int color)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
    return;
}

int main()
{

    int foregroundcolor=15;
    int backgroundcolor=1;
    int finalcolor;

    finalcolor=(16*backgroundcolor)+foregroundcolor;

    setcolor(finalcolor);
    cout<<"finalcolor=(16*backgroundcolor)+foregroundcolor\n";
    setcolor(7);

    return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28