How the Colour Attribute Byte is derived
[Back]
It wasn’t until I was developing a DOS console user interface,
and I needed to be able to store the text screen attributes,
then restore them, that I developed an appreciation for the use
of bitwise operations. The attribute byte for a text screen stores
the 16 possible text and 16 possible background colours (plus the
ability to make the background 'blink') in the 8 bits. This is
accomplished because all colours are made from the 3 primary colours;
Red, Green, and Blue. (we are speaking in terms of light, not pigment-
If you add Red, Green, and Blue paint together, you get Black.
If you add Red, Green, and Blue light together, you get White).
Examine the diagram below illustrating the structure of Attribute Byte:
Most Significant Bit | Least Significant Bit | ||||||||||||||||
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | Bit Position | |||||||||
0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | Bit Value | |||||||||
X | R | G | B | X | R | G | B | ||||||||||
Background | Foreground |
In our example above, the binary number 0010 1010 = 42 = 0x2A
It is LIGHTGREEN text (GREEN + INTENSITY BIT) on a CYAN background (BLUE+GREEN).
The most significant bit in the text attribute is the Blink bit..if its 0, the character doesn’t blink, if its 1, it does. If we wanted our example to blink, its binary value would be 1011 1010=0xBA=186 decimal.
Below is a chart of the Text(Foreground) and Background colours:
COLOUR | DEC | HEX | BIN | USE |
Black | 0 | 0 | 0000 |
Text/Bkgrnd |
Blue |
1 |
1 |
0001 |
Text/Bkgrnd |
Green |
2 |
2 |
0010 |
Text/Bkgrnd |
Cyan (Blue+Green) |
3 |
3 |
0011 |
Text/Bkgrnd |
Red |
4 |
4 |
0100 |
Text/Bkgrnd |
Magenta (Red+Blue) |
5 |
5 |
0101 |
Text/Bkgrnd |
DarkYellow or Brown (Green+Red) |
6 |
6 |
0110 |
Text/Bkgrnd |
Lightgray(Red+Green+Blue) |
7 |
7 |
0111 |
Text/Bkgrnd |
Darkgray(Black+Intensity) |
8 |
8 |
1000 |
Text |
LightBlue(Blue+Intensity) |
9 |
9 |
1001 |
Text |
LightGreen(Green+Intensity) |
10 |
A |
1010 |
Text |
LightCyan(Cyan+Intensity) |
11 |
B |
1011 |
Text |
LightRed (Red+Intensity) |
12 |
C |
1100 |
Text |
LightMagenta (Magenta+Intensity) |
13 |
D |
1101 |
Text |
Yellow (DarkYellow+Intensity) |
14 |
E |
1110 |
Text |
White(Lightgray+Intensity) |
15 |
F |
1111 |
Text |