1

Currently, I'm doing this to change my console colors:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN);

But what if I wanted to set the background color to, for example, #64e0fc? I know the console can render different colors (You can set them in the properties), but do I have any control over the colors displayed?

AlgoRythm
  • 1,196
  • 10
  • 31
  • 3
    The console API uses a 16-color palette, defined in each screen buffer. You can modify the palette in the console registry settings (ColorTable00 -- ColorTable15) for the default and per-title settings; or in the shortcut settings for apps launched from a shortcut; or in code via `SetConsoleScreenBufferInfoEx`. The API `FOREGROUND_*` constants are Intensity (8), Red (4), Green (2), and Blue (1) -- the 4 bits of the 16-color palette. The `BACKGROUND_*` constants are left-shifted by 4 bits, and use the same palette. The upper-byte `COMMON_LVB_*` constants are for DBCS, reverse, and underscore. – Eryk Sun Jan 03 '18 at 04:57
  • @eryksun I wish I could mark that as the answer; very informative! Thank you! – AlgoRythm Jan 03 '18 at 04:59
  • Do you have to support older versions of Windows, or can you use new features in the Windows 10 console? – Eryk Sun Jan 03 '18 at 05:00
  • @eryksun I wish to support older versions, but am interested in what you have in mind as well – AlgoRythm Jan 03 '18 at 05:01

1 Answers1

2

The Windows console uses a 4-bit color palette, so you can have a maximum of 16 colors for foreground and background. The exact values are defined in the registry and is modifiable.

HKEY_CURRENT_USER\Console\ColorTable##
HKEY_CURRENT_USER\Console\(program)\ColorTable##

Where ## is two digits from 00 to 15.

A program can set the color palette by calling SetConsoleScreenBufferInfoEx(), but will not be able to handle more than 16 colors at a time.

Take note that the format for DWORD COLORREF:

When specifying an explicit RGB color, the COLORREF value has the following hexadecimal form:

0x00BBGGRR
iBug
  • 35,554
  • 7
  • 89
  • 134
  • 1
    "HKCU\Console" has the default console settings. These get overridden by window-title settings. The window title is set in the application `STARTUPINFO` (e.g. CMD's `start` command lets you set the title). It defaults to the application path. The console loads per-title settings from "HKCU\Console\\[window title]" when an application allocates a new console instead of inheriting its console. However, window-title settings aren't used if an application is run via a .LNK shortcut. Instead the settings in the shortcut are used to override the defaults. – Eryk Sun Jan 03 '18 at 05:29
  • @eryksun Yeah, I'm aware of that. – iBug Jan 03 '18 at 05:37
  • 1
    In Windows 10, you can use Virtual Terminal sequences with 24-bit RGB color codes -- i.e. `"\x1b[38;2;;;m"` (foreground) and `"\x1b[48;2;;;m"` (background). But they've divorced this from the 16-color palette data that can be set and read by console API functions such as `ReadConsoleOutputAttribute`. In this case reading the character attributes lies about what the actual colors are, and I don't think there's any way to get the real values using console functions. – Eryk Sun Jan 03 '18 at 09:25