Q(uick)BASIC Statement: PALETTE

Quick View

PALETTE, PALETTE USING

Graphics statements that change one or more of the colors in the palette

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • PALETTE [attribute%,color&]
  • PALETTE USING arrayname#[(index%)]
Description/Parameter(s)
attribute% The color attribute to change.
color& A color value to assign to an attribute.
arrayname# An array of color values to assign to the current screen mode's set of attributes. The array must be large enough to assign colors to all the attributes.
index% The index of the first array element to assign to an attribute.
The available color attributes and values depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
Example
'This example requires a color graphics adapter. PALETTE 0, 1 SCREEN 1 FOR i% = 0 TO 3: a%(i%) = i%: NEXT i% LINE (138, 35)-(288, 165), 3, BF LINE (20, 10)-(160, 100), 2, BF DO FOR i% = 0 TO 3 a%(i%) = (a%(i%) + 1) MOD 16 NEXT i% PALETTE USING a%(0) LOOP WHILE INKEY$ = ""
Syntax
  • PALETTE [attribute,color]
  • PALETTE USING array-name [(array-index)]
Description/Parameter(s)
Argument Description
attribute The palette attribute to be changed.
color The display color number to be assigned to the attribute. The color must be a long integer expression for the IBM Video Graphics Array adapter (VGA) and IBM Multicolor Graphics Array adapter (MCGA) in screen modes 11 to 13. Integer or long-integer expressions may be used with the IBM Enhanced Graphics Adapter (EGA).
array-name An array containing the color numbers to be assigned to the attributes available in the current screen mode. The VGA and MCGA adapters require a long integer array in screen modes 11 to 13. With the EGA this can be either an integer or long-integer array.
array-index The index of the first array element to use in setting the palette.

The PALETTE statement works only on systems equipped with the EGA, VGA, or MCGA adapters.

The statement provides a way of mapping display colors (the actual binary values used by the adapter) to color attributes (a smaller set of values). All BASIC graphics statements such as CIRCLE, COLOR, DRAW, or LINE use color attributes rather than display-color values.

When a program enters a screen mode, the attributes are set to a series of default color values. (See the SCREEN statement for a list of the default colors.) In the EGA, VGA, and MCGA adapters these default values have been selected so the display shows the same colors, even though the EGA uses different color values.

With the palette statement you can assign different colors to the attributes, giving you greater control over the colors on the display. A PALETTE statement with no arguments sets the palette back to the default color values.

When you execute a PALETTE statement with arguments, the adapter subsequently uses the display color (indicated by color) whenever the value attribute appears in a statement like DRAW or LINE that specifies a color. Changing the display color assigned to an attribute changes the color on the screen immediately.

For example, assume that the current palette contains colors 0, 1, 2, and 3 in the four attributes numbered 0, 1, 2, and 3. The DRAW statement

  • DRAW "C3L100"

selects attribute 3, and draws a line of 100 pixels using the display color associated with attribute 3, in this case also 3. If the statement

  • PALETTE 3,2

is executed, then the color associated with attribute 3 is changed to color 2. All text or graphics currently on the screen displayed using attribute 3 are instantaneously changed to color 2. Text or graphics subsequently displayed with attribute 3 are also displayed in color 2. The new palette of colors contains 0, 1, 2, and 2.

With the USINGN option, all entries in the palette can be modified in one PALETTE statement. The array-name argument is the name of an integer or long-integer array and the array-index specifies the index of the first array element in the array-name to use in setting the palette. Each attribute in the palette is assigned a corresponding color from this array. The array must be dimensioned large enough to set all the palette entries after array-index. For example, if you are assigning colors to all 16 attributes, and the index of the first array element that is given in your PALETTE USING statement is 5, then the array must be dimensioned to hold at least 20 elements (since the number of elements from 5-20, inclusive, is 16):

  • DIM PAL%(20)
  • PALETTE USING PAL%(5)

A color argument of -1 in the array leaves the attribute unchanged. All other negative numbers are invalid values for color.

You can use the COLOR statement to set the default foreground color and the background display color. The foreground color argument specifies the way text characters appear on the display screen. Under a common initial palette setting, points colored with the attribute 0 appear black on the display screen. Using the PALETTE statement, you could, for example, change the mapping of attribute 0 from black to white.

The table below lists attribute and color ranges for various adapter types and screen modes.

The VGA uses a different way of calculating color values from the EGA. To calculate a color value, select the intensities of red, green, and blue. The intensity of a color is a number from 0 (low intensity) to 63 (high intensity). Then use the following formula to calculate the actual color number:

  • color number = 65536 * blue + 256 * green + red

Because there are gaps in the range of color numbers, you should use the formula rather than just select a number.

When used with the IBM Analog Monochrome Monitor, the VGA color values are converted to a gray-scale value by taking a weighted sum of the red, blue, and green intensities:

  • gray value = 11% blue + 59% green + 30% red

For example if the blue, green, and red intensities are 45, 20, and 20, the gray value would be .11*45+.59*20+.30*20 or 22 (the fraction in the result is dropped).

See the SCREEN statement for the list of colors available for various screen-mode, monitor, and graphics-adapter combinations.

Screen Color and Attribute Ranges

SCREEN Mode Monitor Attached Adapter Range Attribute Range Color
0 Monochrome MDPA 0-15 N/A
Monochrome EGA 0-15 0-2
Color CGA 0-15 N/A
Color/Enhanced b EGA 0-15 0-63
N/A VGA 0-15 0-63
N/A MCGA 0-15 N/A
1 Color CGA 0-3 N/A
Color/Enhanced b EGA 0-3 0-15
N/A VGA 0-3 0-15
N/A MCGA 0-3 N/A
2 Color CGA 0-1 N/A
Color/Enhanced b EGA 0-1 0-15
N/A VGA 0-1 0-15
N/A MCGA 0-1 N/A
7 Color/Enhanced b EGA 0-15 0-15
N/A VGA 0-15 0-15
8 Color/Enhanced b EGA 0-15 0-15
N/A VGA 0-15 0-15
9 Enhanced b EGA c 0-3 0-63
Enhanced b EGA d 0-15 0-63
N/A VGA 0-16 0-63
10 Monochrome EGA 0-3 0-8
N/A VGA 0-3 0-8
11 N/A VGA 0-1 0-262,143 e
N/A MCGA 0-1 0-262,143 e
12 N/A VGA 0-15 0-262,143 e
13 N/A VGA 0-255 0-262,143 e
N/A MCGA 0-255 0-262,143 e
  • N/A = Not Applicable
  • With 64K of EGA memory
  • With more than 64K of EGA memory
  • IBM Enhanced Color Display
  • Display colors are not numbered from 0 to 262,143. See above.
Note: Because of their wide range of colors, the VGA and MCGA adapters require a long-integer array in the PALETTE USING statement in screen modes 11 to 13.
Example

This example shows the different forms of the PALETTE and PALETTE USING statements.
Note: This is not a complete program. Do not run this example in its current form.

PALETTE 0,2 'Changes all points colored with attribute 0 'to color 2. PALETTE USING A%(0) 'Changes each palette entry. Since the 'array is initialized to zero when it 'is first declared, all attributes are 'now mapped to display color zero. The 'screen will now appear as one single 'color. However, it will still be 'possible to execute BASIC statements. PALETTE 'Sets each palette entry to its appropriate 'initial display color. Actual initial colors 'depend on your screen-hardware configuration.
Syntax
  • PALETTE [attribute%,color&]
  • PALETTE USING array-name [(array-index)]
Description/Parameter(s)
attribute% The palette attribute to be changed.
color& The display color assigned to the attribute.
array-name and array-index Used to change more than one palette assignment with a single PALETTE USING statement.

The PALETTE statement changes one or more colors in the palette. The PALETTE statement works only on systems equipped with the EGA, VGA, or MCGA adapters. The PALETTE statement is not supported in screen mode 3 or 4.

PALETTE [attribute%,color&]

  • The argument color& must be a long integer expression for the IBM Video Graphics Array adapter (VGA) and IBM Multicolor Graphics Array adapter (MCGA) in screen modes 11 to 13. Integer or long-integer expressions can be used with the IBM Enhanced Graphics Adapter (EGA).
  • The table below lists attribute and color ranges for various adapter types and screen modes. See the SCREEN statement for a list of colors available for various screen modes, monitor, and graphics adapter combinations.
SCREEN mode Monitor attached Adapter Attribute range Color range PALETTE supported
0 Monochrome MDPA 0-15 No
Monochrome EGA 0-15 0-2 Yes
Color CGA 0-15 No
Color/Enhanced 1 EGA 0-15 0-15/0-63 Yes
Analog VGA 0-15 0-63 Yes
Color/Analog MCGA 0-15 0-63 No
1 Color CGA 0-3 0-15 2 No
Color/Enhanced 1 EGA 0-3 0-15 Yes
Analog VGA 0-3 0-15 Yes
Color/Analog MCGA 0-3 0-15 No
2 Color CGA 0-1 No
Color/Enhanced 1 EGA 0-1 0-15 Yes
Analog VGA 0-1 0-15 Yes
Color/Analog MCGA 0-1 0-15 No
3 Monochrome HGC 0-1 No
4 Color/Enhanced OCGA/ 7 0-1 0-15 3 No
OEGA/ 7
OVGA  7
7 Color/Enhanced 1 EGA 0-15 0-15 Yes
VGA 0-15 0-15 Yes
8 Color/Enhanced 1 EGA 0-15 0-15 Yes
VGA 0-15 0-15 Yes
9 Enhanced 1 EGA 4 0-3 0-63 Yes
Enhanced 1 EGA 5 0-15 0-63 Yes
Analog VGA 0-16 0-63 Yes
10 Monochrome EGA 0-3 0-8 Yes
Analog VGA 0-3 0-8 Yes
11 Analog VGA 0-1 0-262,143 6 Yes
Analog MCGA 0-1 0-262,143 6 Yes
12 Analog VGA 0-15 0-262,143 6 Yes
13 Analog VGA 0-255 0-262,143 6 Yes
Analog MCGA 0-255 0-262,143 6 Yes
  • IBM Enhanced Color Display.
  • Color range available for attribute 0 only.
  • Color range available for attribute 1 only.
  • With 64K of EGA memory.
  • With more than 64K of EGA memory.
  • Range of display colors is actually from 0 to 4,144,959, but only 262,144 of these can be displayed.
  • Olivetti CGA, EGA and VGA

PALETTE USING array-name [(array-index)]

  • The argument array-name is an array that contains more than one display color. It must be a long integer array for VGA and MCGA adapters in screen modes 11 to 13. Otherwise, the array can be either an integer or long-integer array.
  • The argument array-index is the index of the first array element to use in setting the palette.
  • With USING PALETTE, all entries in the palette can be modified in one statement. Each attribute in the palette is assigned a corresponding color from the array.
    • A color argument of -1 in the array leaves the attribut unchanged in the palette. (All other negative numbers are invalid values for color.)
    • The dimensions for the array must be large enough to set all the palette entries after array-index. For example, if you are assigning colors to a palette with 16 attributes and the argument array-index is 5 (the first array element to use in resetting the palette), then the dimensions for the array must be large enough to hold at least 20 elements (because the number of elements from 5 to 20, inclusive, is 16):

  • DIM PAL%(20)
  • ' Other statements can go here.
  • PALETTE USING PAL%(5)

Usage Notes

  • The PALETTE statement provides a way of mapping display colors (the actual binary values used by the adapter) to color attributes (a smaller set of values).
  • When a program enters a screen mode, the attributes are set to a series of default color values. See the SCREEN Statement Details for a list of the default colors.
    Note:In the EGA, VGA, and MCGA adapters the default values have been selected so the display shows the same colors, even though the EGA uses different color values.
    Use the PALETTE statement to assign different colors to the attributes.
  • Use a PALETTE statement with no arguments to set the palette back to the default color values.
  • After a PALETTE statement is executed with arguments, the graphics adapter subsequently uses the display color associated with the attribute whenever an attribute argument appears in a statement such as DRAW or LINE. Changing the display color assigned to an attribute changes those colors currently displayed on the screen associated with that attribute. For example, assume that the current palette contains colors 0, 1, 2, and 3 in the four attributes numbered 0, 1, 2, and 3. The following DRAW statement selects attribute 3, and draws a line of 100 pixels using the display color associated with attribute 3, in this case also 3:

  • DRAW "C3L100"

  • If the following statement is executed, the color associated with attribute 3 is changed to color 2:

  • PALETTE 3, 2

  • All text or graphics currently on the screen displayed using attribute 3, including the line that is 100 pixels long, are instantaneously changed to color 2. Text or graphics subsequently displayed with attribute 3 are also displayed in color 2. The new palette of colors contains 0, 1, 2, and 2.

  • Attribute 0 is always the screen background color. Under a common initial palette setting, points colored with the attribute 0 appear black on the display screen. Using the PALETTE statement, you could, for example, change the mapping of attribute 0 from black to white.
  • You also can use the COLOR statement to change the screen background color in modes 1 and 7 through 10.
  • The 64 EGA colors are derived from four levels each of red, green, and blue. For example, black is composed of red, green, and blue levels of (0,0,0), bright white is (3,3,3), dark gray is (1,1,1), etc. The best way to see the BASIC color code (0 through 63) associated with each combination of red, green, and blue levels is to run the following program:

  • ' Display the EGA color codes 1 through 63
  • ' using color code 0 (black) as background.
  • DEFINT A-Z
  • SCREEN 9 ' Establish EGA screen mode.
  • ' Display a set of nine color bars.
  • FOR CC% = 1 TO 9
  • LINE (((CC% * 64) - 24), 40)-STEP(60, 140), CC%, BF
  • NEXT CC%
  • ' Display seven sets of nine color bars.
  • ' A new set is displayed each time user presses a key.
  • FOR Set% = 0 TO 6
  • FOR ColorBar% = 1 TO 9
  • DisplayCode% = (Set% * 9) + ColorBar%
  • LOCATE 15, (ColorBar% * 8)
  • PRINT DisplayCode%
  • PALETTE ColorBar%, DisplayCode%
  • NEXT ColorBar%
  • SLEEP
  • NEXT Set
  • END

  • To calculate a VGA color value, select the intensities of red, green, and blue. The intensity of a color is a number from 0 (low intensity) to 63 (high intensity). Then use the following formula to calculate the actual color number:

  • COLOR number = 65536 * blue + 256 * green + red

  • This formula yields integer values from 0 to 4,144,959, but because there are gaps in the range of color numbers, you should use the formula rather than just select a number.

  • When used with the IBM Analog Monochrome Monitor, the VGA color values are converted to a gray-scale value by taking a weighted sum of the red, blue, and green intensities:

  • gray value = 11% blue + 59% green + 30% red

  • For example, if the blue, green, and red intensities are 45, 20, and 20, the gray intensity value calculation would be

  • gray value = (.11 * 45) + (.59 * 20) + (.3 * 20) = 22

  • The fractional part of the result is dropped.
Example

This example illustrates the use of the PALETTE and PALETTE USING statements.

DEFINT A-Z DIM SHARED PaletteArray(15) CONST ASPECT = 1 / 3 '640 x 200, 16 color resolution. SCREEN 8 'Initialize PaletteArray. FOR I = 0 TO 15 PaletteArray(I) = I NEXT I 'Draw and paint concentric ellipses. FOR ColorVal = 15 TO 1 STEP -1 Radius = 20 * ColorVal CIRCLE (320, 100), Radius, ColorVal, , , ASPECT PAINT (320, 100), ColorVal NEXT 'Shift the palette until a key is pressed. DO FOR I = 1 TO 15 PaletteArray(I) = (PaletteArray(I) MOD 15) + 1 NEXT I PALETTE USING PaletteArray(0) 'Map the black background to some random color. PALETTE 0, PaletteArray(INT(RND * 15)) LOOP WHILE INKEY$ = "" END