RGB colors

Hi, I would like to know if someone can explain me how to program the console to use RGB colors in visual studio 2015
This prints colors in the terminal in Linux. Does it work on windows?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>
using std::cout;

int main() {
    for (int i = 0; i < 16; i++) {
        cout << "\x1b[38;5;" << i << 'm' << std::setw(4) << i;
        if (i % 8 == 7) cout << "\x1b[0;0m\n";
    }
    cout << '\n';

    for (int row = 0; row < 2; row++) {
        for (int g = 0; g < 6; g++) {
            for (int col = 0; col < 3; col++) {
                for (int b = 0; b < 6; b++) {
                    int r = row * 3 + col;
                    int i = 16 + 36 * r + 6 * g + b;
                    cout << "\x1b[38;5;" << i << 'm' << std::setw(4) << i;
                }
                cout << "\x1b[0;0m";
                if (col < 2) cout << "   ";
            }
            cout << '\n';
        }
        cout << '\n';
    }

    for (int i = 232; i < 256; i++) {
        cout << "\x1b[38;5;" << i << 'm' << std::setw(4) << i;
        if (i == 243) cout << "\x1b[0;0m\n";
    }
    cout << "\x1b[0;0m\n\n";
}

Well actually I meant that I want to use some colors like brown, but it doesn't appear in the options that I can use, so I want to know the function RGB so I could create my own colors
So did the program work or not? Did it print a bunch of different colors?
no :/ it didn't work
That's too bad. I don't use Windows, but the console functions are described here: https://docs.microsoft.com/en-us/windows/console/console-reference
It looks pretty complicated.
I think it would look more or less like this. I don't have a windows compiler atm either, so this is possibly pure gibberish that I cobbled together from a couple of examples. Hopefully it steers you in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
#include <windows.h>   // this is for visual studio...
int main()
{
  HANDLE  hConsole; //a pox on whoever invented the handle concept. 
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hConsole, 100); //100 is the color, you can mess with the value or look up how the function works..
     //write to console in the above color here

  return 0;
}
no :/, it didn't work either
jonnin said you might have to mess with the color value.
Peeking at the docs, this might work.
 
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_RED);

But you need to read that documentation yourself.
It's not possible to display RGB colors on the console. The console uses a (IIRC) 4-bit palette. No, ORing foreground constants won't do anything, but you can display different colors as foreground and background.
helios wrote:
No, ORing foreground constants won't do anything

I'm pretty sure you can OR those constants. The microsoft website gives this example:
 
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE


https://docs.microsoft.com/en-us/windows/console/console-screen-buffers
Last edited on
Since it's a palette, that's a risky assumption, since the display console might have remapped cyan to something entirely different.
I see. That makes it even more complicated then.
A man of few words ... but we need a little more.

What exactly didnt work? Didnt compile because Jonnin can't type, or compiled and didn't change the colors, or blew up because handles are terrible, or something else?

This could be how it was done in 1997 too for all I know. It may not be the right way in 2015.


Last edited on
Well, actually it printed different letters xd and numbers and that was all
Sorry, I didn't expect many replies and my teacher told me that is possible to change the default colors of the console,well i think i have to say that i'm programming frogger in visual, so I want to use more colors, because it doesn't look well with just default colors.
Last edited on
closed account (E0p9LyTq)
Trying to do even simple graphics and game updating in a Windows console is going to be a major PITA. Easier (relatively speaking) would be to use Windows GDI.
Topic archived. No new replies allowed.