13

I know a bit how to do colors in Win32 C++ console. But it's not really efficient. For example:

 SYSTEM("color 01")

Slows down a lot on your process. Also:

 HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
 WORD wOldColorAttrs;
 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

 /*
  * First save the current color information
  */

 GetConsoleScreenBufferInfo(h, &csbiInfo);
 wOldColorAttrs = csbiInfo.wAttributes;

 /*
  * Set the new color information
  */

 SetConsoleTextAttribute ( h, FOREGROUND_RED );

Works great, but it doesn't have much colors. Also, FOREGROUND_RED is dark-red.

So what I want to ask, isn't there a way like CLR property Console::ForegroundColor set, so you can use any color from the ConsoleColor enum?

J. Calleja
  • 4,855
  • 2
  • 33
  • 54
  • 2
    Have you tried setting the `FOREGROUND_INTENSITY` flag to make it bright-red? For example: `SetConsoleTextAttribute( h, FOREGROUND_RED | FOREGROUND_INTENSITY )` – Thomas Russell Jun 15 '13 at 16:00
  • That works, but still I wonder if there's any way to get more colors, maybe with palette? – x_metal_pride_diamond_x Jun 15 '13 at 16:01
  • Console only supports 16 colors, which are created by combining FOREGROUND_RED, FOREGROUND_BLUE, FOREGROUND_GREEN and FOREGROUND_INTENSITY. – riv Jun 15 '13 at 16:03
  • There are 16 colors. Actual color values are retrieved from a color table. Editable by the user in the system menu. And you can update it in code, SetConsoleScreenBufferEx() function. – Hans Passant Jun 15 '13 at 16:38
  • More colors are coming: https://blogs.msdn.microsoft.com/commandline/2016/09/22/24-bit-color-in-the-windows-console/ – Lassi Nov 20 '18 at 10:30

3 Answers3

31

The console only supports 16 colors, which are created by combining the four values as follows (I might have got the gray/darkgray confused, but you get the idea):

namespace ConsoleForeground
{
  enum {
    BLACK             = 0,
    DARKBLUE          = FOREGROUND_BLUE,
    DARKGREEN         = FOREGROUND_GREEN,
    DARKCYAN          = FOREGROUND_GREEN | FOREGROUND_BLUE,
    DARKRED           = FOREGROUND_RED,
    DARKMAGENTA       = FOREGROUND_RED | FOREGROUND_BLUE,
    DARKYELLOW        = FOREGROUND_RED | FOREGROUND_GREEN,
    DARKGRAY          = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
    GRAY              = FOREGROUND_INTENSITY,
    BLUE              = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
    GREEN             = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
    CYAN              = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
    RED               = FOREGROUND_INTENSITY | FOREGROUND_RED,
    MAGENTA           = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
    YELLOW            = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
    WHITE             = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
  };
}
riv
  • 6,846
  • 2
  • 34
  • 63
  • You can just remove all of the assignments and it will still work because the color codes are in increasing order. – Krii Sep 06 '17 at 20:45
  • It is worth noting that by using [`SetConsoleScreenBufferInfoEx`](https://learn.microsoft.com/en-us/windows/console/setconsolescreenbufferinfoex) one can change the colours. They are not set into stone, changing the palette is perfectly possible. – z0rberg's Jul 08 '19 at 15:35
2

The console only has a limited set of colors. The .NET implementation uses ConsoleColor to set colors which is just an enumeration of 16 colors.

The underlying Win32 console has 8 base colors that's doubled through the use of the intensity flag (as mentioned in Shaktal's comment under your question).

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
1

Here is full list of Background and ForeGround colors. Complete list of WinAPI colors

Haseeb Mir
  • 928
  • 1
  • 13
  • 22