Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What are the QuickBASIC text-mode colours?

What are the QuickBASIC text-mode colours?

by  logiclrd  Posted    (Edited  )
Before continuing, it is important to remember that computers operate inherently with numbers made up with bits -- that is, a number's digits inside the computer are 0's and 1's. This means that if you add 1 to 1, you get 10, and 1 to 11, you get 100. Instead of carrying when you go over 9, you carry when you go over 1.

Now, back in the (good?) old days of CGA graphics, there weren't a great many different screen modes to pick from. The basic video card had a whole 16 kilobytes of memory on it -- video cards with more allowed for this beautiful animation technique that involved telling the video card to start its next retrace from a different place in video memory. The video card allowed basically for just two pixel modes: 320x200 and 640x200. In text mode, you could pick from 40x25 and 80x25, and each character was made up of 8x8 dots (40x25 * 8x8 = 320x200, and similarly 80x25 * 8x8 = 640x200). In video modes, every pixel would be represented by a certain (small!) number of bits -- in 320x200, 2 bits per pixel, and in 640x200, only 1 bit per pixel (i.e., monochrome).

The video card, internally, had access to one brightness of each of the 3 additive primaries -- i.e., pure red, pure green, and pure blue. When representing a colour, you could for each colour either have it on or off. Additionally, you could halve the brightness with a 4th bit, which they thoughtfully inverted so that the first half, and not the second half, of colours would be halved.

[tt] 1 0 1 1
| | | `- blue channel
| | `---- green channel
| `------- red channel
`---------- brightness channel[/tt]

The colour represented in the diagram above is bright cyan. In text mode, which, though extended by the EGA palette, is still essentially the way it was with the CGA card, every character has a whole byte of colour information -- that's 8 bits. Since a colour only needs 4 bits to represent, that allows us to give every character a foreground and a background colour. Later video cards changed the brightness channel of the background colour to be a blink channel for that character, which, if enabled, caused that character to turn on and off roughly every 1/4 second, but originally, it was simply brightness. All was bliss -- like at Christmas. But wait, though -- the graphics modes. How do you represent a 4-bit colour using only 2 -- or even 1 -- bit? Well, the short answer is that you can't, and you don't get as many colours. In 320x200 -- SCREEN 1 -- which takes 2 bits per pixel, the video card allows you to specify a value for the blue channel, and this, along with a brightness channel that is 'always on', is mixed in with the red & green channels that are specified for each pixel. This allows us to pick between two sets of colours: either every pixel can be one of black, green, red or yellow (you've seen games like this), or every pixel can be one of blue, cyan, magenta or white (you've seen games like this too). The video card is perhaps a bit more allowing with SCREEN 2, in that you can specify two distinct colours for the background and for the foreground, and then each pixel picks either one or the other by being either on or off.

How does all this tie in with QB? Well, first of all, the text mode colours can all be derived using this table:

[tt] Red Green Blue Value (binary),(decimal) Colour
0 0 0 000 0 Black
0 0 1 001 1 Blue
0 1 0 010 2 Green
0 1 1 011 3 Cyan
1 0 0 100 4 Red
1 0 1 101 5 Magenta
1 1 0 110 6 Yellow/Brown
1 1 1 111 7 White/Grey[/tt]

As you can see, you get cyan by mixing blue and green, magenta by mixing blue and red, and yellow by mixing red and green. This set of colours is duplicated for the second half of the palette, except that the brightness bit is turned on. QuickBASIC's colour command allows you to select all 16 possible foreground colours, but only allows you to specify a number between 0 and 7 for the background colour. This is because with later video cards, that setting that makes the background's brightness bit into a blink bit is on by default. It is possible to turn it off using direct hardware access with OUT, but QuickBASIC doesn't care either way, and only allows you to specify the 3 colour bits of the background colour. However, it adds a 5th bit to the foreground colour, and if you add 16 to your colour (which turns this 5th bit on), then the COLOR statement will turn on the brightness bit of the background colour. Under normal operation, this means simply that the text will blink. It is possible to find code snippets that allow for bright backgrounds, too. By the way, text viewed in a windowed (not full-screen) DOS box in Windows 95 always treats the background's brightness bit as a brightness bit, and not as a blink bit.

As for the two graphics modes, you can select the colours for them using the COLOR command. How it all works is documented in the help file, but I'll just quickly link it in with what I've said. For CGA graphics modes, the COLOR command's two parameters have different meanings than they do for text mode. While in text mode, you specify a foreground colour and a background colour, in SCREEN 1, you specify a background colour and a "palette." The background colour is what QuickBASIC will treat graphics drawn in colour 0 as, and the palette is the blue channel's value. With it set to 0, you get the red/green/yellow palette, and with 1, you get the magenta/white/cyan palette. Unfortunately, QuickBASIC does not provide any access to the "colourization" of SCREEN 1 for CGA adaptors, but under EGA and VGA adaptors, you can change the colours using the PALETTE statement. In fact, the EGA adaptor uses an array of 64 colours that the 16 colours in text mode, 4 in SCREEN 1 and 2 in SCREEN 2 can each be separately mapped to. This is a topic for a later discussion, though.

Enjoy the CGA palette given to you in SCREEN 0, and for the reference, here is a colour table for QB colours:

[color #000000]Colour 00: Black[/color]
[color #0000AA]Colour 01: Blue[/color]
[color #00AA00]Colour 02: Green[/color]
[color #00AAAA]Colour 03: Cyan (Blue + Green)[/color]
[color #AA0000]Colour 04: Red[/color]
[color #AA00AA]Colour 05: Magenta (Blue + Red)[/color]
[color #AA5500]Colour 06: Dark Yellow (Brown)[/color]
[color #AAAAAA]Colour 07: Gray (Dark White)[/color]
[color #555555]Colour 08: Dark Gray (Bright Black)[/color]
[color #5555FF]Colour 09: Bright Blue[/color]
[color #55FF55]Colour 10: Bright Green[/color]
[color #55FFFF]Colour 11: Bright Cyan[/color]
[color #FF5555]Colour 12: Bright Red[/color]
[color #FF55FF]Colour 13: Bright Magenta (Pink)[/color]
[color #FFFF55]Colour 14: Yellow (Green + Red)[/color]
[color #FFFFFF]Colour 15: White (Blue + Green + Red)[/color]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top