How to Convert VGA Colors to Their Equivalent Gray Scale (50225)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0

This article was previously published under Q50225

SUMMARY

In Microsoft QuickBasic, VGA colors can be converted to their equivalent gray scale values using the CALL INTERRUPT statement. This can be useful when printing an image or having the image scanned by devices that do not support color.

This procedure can be used to convert a color image to a monochrome equivalent before printing out the image on a dot-matrix printer.

Once the color registers are converted to their gray scale equivalents, the original red, green, and blue values are lost. If this information needs to be restored, the VGA color registers should be saved before doing the gray scale summing, and then restored afterward.

This information applies to Microsoft QuickBasic Versions 4.00, 4.00b, and 4.50 for MS-DOS and to Microsoft Basic Compiler Versions 6.00, and 6.00b for MS-DOS, and Microsoft Basic PDS 7.00 for MS-DOS.

MORE INFORMATION

The following Basic program is GRAY.BAS, which displays a multicolored image in VGA SCREEN 13, then converts all of the colors to their equivalent gray scale values:
REM $INCLUDE: 'qb.bi'  ' defines for CALL INTERRUPT
' For BC.EXE and QBX.EXE in Basic PDS 7.00 the include file is 'QBX.BI'

DIM inregs AS RegType
DIM outregs AS RegType
SCREEN 13

FOR i% = 2 TO 255      ' display colorful pattern

        LINE (i%, 10)-(i%, 199), i%

NEXT

LOCATE 1, 1
COLOR 7
PRINT "press any key to convert to gray scale"
WHILE INKEY$ = "": WEND

inregs.ax = &H101B     ' BIOS call to set gray scale values
inregs.bx = 0          ' start at color register 0
inregs.cx = 256        ' convert all 256 color registers
CALL INTERRUPT(&H10, inregs, outregs)

LOCATE 1, 1
PRINT "press any key to end                   "
WHILE INKEY$ = "": WEND
END
				
To demonstrate this program from an .EXE program, compile and link as follows:
   BC GRAY.BAS;
   LINK GRAY,,,QB.LIB;
				
For Basic compiler 7.00, compile and link as follows:
   BC GRAY.BAS;
   LINK GRAY,,,QBX.LIB;
				
If you are running the program from the QuickBasic QB.EXE editor, the Quick library QB.QLB must be loaded in as follows:
   QB GRAY /L QB.QLB
				
For QBX.EXE in Basic compiler 7.00, the Quick library QBX.QLB must be loaded as follows:
   QBX GRAY /L QBX.QLB
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB50225