Home > Enterprise >  How do I decompose the value of 192 in the code `SetConsoleTextAttribute(hStdout, 192)` into foregro
How do I decompose the value of 192 in the code `SetConsoleTextAttribute(hStdout, 192)` into foregro

Time:11-15

I'm trying to understand how enter image description here

I make up the value of 192 but I don't know how it works. How do I decompose the value into foreground and background color?

Could someone give me a clue?

CodePudding user response:

The wAttributes parameter to SetConsoleTextAttribute is of type WORD, i.e. a 16-bit unsigned integer. The lower 8 bits of the character attributes encode the color information.

The following diagram illustrates what the individual bits mean:

 === === === === === === === === 
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |   bit
 === === === === === === === === 
| I | R | G | B | I | R | G | B |   meaning
 --- --- --- --- --- --- --- --- 
|  background   |  foreground   |   scope
 --- --- --- --- --- --- --- --- 

RGB are the individual red-green-blue color channels. If the respective bit is set, the color channel is on, otherwise it is off. I designates the intensity. When set it selects the "bright" color, otherwise it refers to the "dark" variant.

The value 192 is 0b1100'0000 in binary. The I and R bits of the background color are set, meaning "bright red". None of the foreground color bits are set, so the foreground color is "black".

  • Related