16 Background Colors in Text Mode with MCGA, EGA, or VGA

ID: Q75274


The information in this article applies to:


SUMMARY

The attribute byte for video display in text mode is


     7  6  5  4   3   2  1  0
     -------------------------
    | |         |   |         |
     ------------------------- 
where the bits control the following:

   Bits  Control
   ----------------------------------
   0-2   Foreground color
     3   Foreground intensity
   4-6   Background color
     7   Blinking for foreground
          - or -
         Intensity for background 
A total of 16 foreground colors are possible with the combination of bits 0-3. If bit 7 is on (default), these colors can be optionally blinking, for a combination of 32 foreground colors.

Eight background colors are possible with the combination of bits 4-6. With an MCGA, EGA, or VGA, bit 7 can be toggled to be interpreted as background intensity rather than the default blinking color. Doing so makes 16 background colors available in text mode.


MORE INFORMATION

The BIOS call that toggles the interpretation of bit 7 is Interrupt 10h, function 10h, subfunction 03h. This call is available only with an MCGA, EGA, or VGA. A zero (0) in BL enables intensity, a one (1) in BL enables blinking text.

The following sample program illustrates the effect of this toggle. First, bit 7 is left as the blink bit; therefore, there are eight background colors and 32 foreground colors. Each of the 32 foreground colors is displayed once and each background color is repeated four times.

Next, bit 7 is toggled to intensity; therefore, there are 16 background colors and no blinking text. Still, you have to go through foreground colors 0-31 to view all 16 background colors. This is because bit 7 is still controlled in the setting of the foreground color; however, it is now interpreted to affect the display of the background color rather than the foreground color. Viewing the output of the sample program should clarify this concept.

Sample Code


/* Compile options needed: none
*/ 
#include <graph.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#define TRUE  1
#define FALSE 0
#define VIDEO_IO        0x10
#define BIOS_CALL       0x10
#define INTENSITY_TGL   0x03
void toggle_intensity( int );
void main( )
{
   long i;
   int row;
   int color;
   char buf[80];
   _settextrows( 43 );
   _clearscreen( _GCLEARSCREEN );
   toggle_intensity( 1 );
/*  Bit 7 = blink; there are 8 background colors and blinking text */ 
   for (i = 0, row = 1, color = 31; i <= 31; i++, row++, color--)
   {
      _setbkcolor( i );
      _settextcolor( color );
      _settextposition( row, 10 );
      sprintf( buf, "  Background %2ld  Foreground %2d\n", i,color );
      _outtext( buf );
   }
   _setbkcolor( _BLACK );
   _settextcolor( 7 );
   _outtext( "\n\nSetting 8 background colors, blinking text." );
   _outtext( "\n\nStrike any key to continue..." );
   getch( );
   _clearscreen( _GCLEARSCREEN );
   toggle_intensity( 0 );
/*  Bit 7 = intensity; there are 16 background colors */ 
   for (i = 0, row = 1, color = 31; i <= 31; i++, row++, color--)
   {
      _setbkcolor( i );
      _settextcolor( color );
      _settextposition( row, 10 );
      sprintf( buf, "  Background %2ld  Foreground %2d\n", i,color );
      _outtext (buf);
   }
   _setbkcolor( _BLACK );
   _settextcolor( 7 );
   _outtext( "\n\nSetting 16 background colors, no blinking text." );
   _outtext( "\n\nStrike any key to quit..." );
   getch( );
   _setvideomode( _DEFAULTMODE );
   _clearscreen( _GCLEARSCREEN );
}
void toggle_intensity( int onoff )
{
   union REGS regs;
   regs.h.ah = VIDEO_IO;
   regs.h.al = INTENSITY_TGL;
   if ( !onoff )
      regs.h.bl = 0x0;
   else
      regs.h.bl = 0x1;
   int86( BIOS_CALL, &regs, &regs );
} 

Additional query words: kbinf 1.00 1.50 5.10 6.00 6.00a 6.00ax 7.00


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 21, 1999