Q(uick)BASIC Statement: COLOR

Quick View

COLOR

A graphics statement that selects display colors

Worth knowing

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

Syntax
  • COLOR [foreground%] [,[background%] [,border%]]Screen mode 0 (text only)
    COLOR [background%] [,palette%]Screen mode 1
    COLOR [foreground%]Screen modes 4, 12, 13
    COLOR [foreground%] [,background&]Screen modes 7-10
Description/Parameter(s)
foreground%
foreground&
A number that sets the foreground screen color. In screen mode 0, foreground% is a color attribute that sets the text color. In other screen modes, foreground% a color attribute or 4-bit color value (screen mode 4 only) that sets the text and line-drawing color.
background%
background&
A number that sets the background screen color. In screen mode 0, background% is a color attribute. In screen mode 1, background% is a 4-bit color value. In screen modes 7-10, background& is a color value.
border%
palette%
A color attribute that sets the screen border color. A number (0 or 1) that specifies which of two sets of color attributes to use:
palette% Attribute 1 Attribute 2 Attribute 3
0 Green Red Brown
1 Cyan Magenta Bright white

  • The available color attributes and values depend on your graphics adapter and the screen mode set by the most recent SCREEN statement.
  • If your system is equipped with an EGA, VGA, or MCGA adapter, use the PALETTE statement to change the color assignments of color attributes.
Example

This example requires a color graphics adapter.

SCREEN 7 FOR i% = 0 TO 15 COLOR i% PRINT i% NEXT i%
Description/Parameter(s)

SCREEN Mode 0 Syntax:

  • COLOR [foreground][,[background][,border]]
  • foreground is the text color (range = 0-31, with 16-31 blinking)
  • background is the screen color (range = 0-7)
  • border is the color surrounding the screen (range = 0-15):
  • 0 = black4 = red8 = grey12 = light red
    1 = blue5 = magenta9 = light blue13 = light magenta
    2 = green6 = brown10 = light green14 = yellow
    3 = cyan7 = white11 = light cyan15 = bright white

SCREEN Mode 1 Syntax:

  • COLOR [background][,palette]
  • background is the screen color (range = 0-15)
  • palette is a three-color palette (range = 0-1):
    0 = green, red, and brown1 = cyan, magenta, and bright white

SCREEN Modes 7-10 Syntax:

  • COLOR [foreground][,background]
  • foreground is the line-drawing color (range = 0-15, except in Mode 10)
  • background is the screen color (ranges vary)

SCREEN Modes 11-13 Syntax:

  • COLOR [foreground]
  • foreground is the line-drawing color (ranges vary)

With the COLOR statement, you can set the foreground and background colors for the display. In screen mode 0 a border color can also be selected. In screen mode 1 no foreground color can be selected, but one of two four-color palettes can be selected for use with graphics statements. In screen modes 12-13 only the foreground color can be set.

The values of foreground in screen modes 7-10, 12, and 13 are attribute numbers (not color numbers) and display the color assigned to that attribute. See the PALETTE statement for a description of attributes.

The COLOR statement does not determine the range of available colors. The combination of adapter, display, and the mode set by the SCREEN statement determine the color range. See the SCREEN statement for more information.

The different syntaxes and their effects in different screen modes are described below:

Mode Description
SCREEN 0

Modifies the current default text foreground and background colors and the screen border. The foreground color must be an integer expression in the range 0-31. It determines the "foreground" color in text mode-the default color of text. Sixteen colors can be selected with the integers 0-15. You can select a blinking version of a color by adding 16 to the color number. For example a blinking color 7 is equal to 7 + 16, or 23.

The background color is an integer expression in the range 0-7 and is the color of the background for each text character. Blinking background colors are not supported.

The border color-the color used to draw the screen border-is an integer expression in the range 0-15. The IBM Enhanced Graphics Adapter (EGA), the IBM Video Graphics Array adapter (VGA), and the IBM Multicolor Graphics Array adapter (MCGA) do not support the border argument.

SCREEN 1

In mode 1, the COLOR statement has a unique syntax that includes a palette argument that is an odd or even integer expression in the range 0 to 255. This argument determines which of two sets of colors to use when displaying particular color numbers.

The default colors for the palette parameter are equivalent to the following PALETTE statements on a system equipped with an EGA:

COLOR ,0'Same as the next three
'PALETTE statements.
PALETTE 1,2'Attribute 1 = color 2 (green)
PALETTE 2,4'Attribute 2 = color 4 (red)
PALETTE 3,6'Attribute 3 = color 6 (yellow)
COLOR ,1'Same as the next three
'PALETTE statements.
PALETTE 1,3'Attribute 1 = color 3 (cyan)
PALETTE 2,5'Attribute 2 = color 5 (magenta)
PALETTE 3,7'Attribute 3 = color 7 (white)

Note that in screen mode 1, a COLOR statement overrides previous PALETTE statements.

SCREEN 2 An "Illegal function call" message results if COLOR is used in this mode.
SCREEN 7-10 In these modes, no border color can be specified. the graphics background is given by the background color number, which must be in the range of valid color numbers for the screen mode. The foreground color argument is the default line-drawing color. In screen modes 7 to 10 foreground is an attribute number, while background is a color number. See the SCREEN statement for more details.
SCREEN 11 Use the PALETTE statement to set color in screen mode 11. An "Illegal function call" message results if COLOR is used in this mode.
SCREEN 12,13 No background color can be specified in these modes. The foreground argument is the attribute used for the foreground graphics color. The attribute must be in the correct range for the screen mode. See the SCREEN statement for more information.

Use the PALETTE statement to set color in screen mode 11. An "Illegal function call" message results if COLOR is used in this mode.

No background color can be specified in these modes. The foreground argument is the attribute used for the foreground graphics color. The attribute must be in the correct range for the screen mode. See the SCREEN statement for more information.

Arguments outside the valid ranges produce error messages that read "Illegal function call."

The foreground can be the same color as the background, making displayed characters invisible. The default background is black, or color number 0 for all display hardware configurations and all screen modes.

In screen modes 12 and 13 you can set the background color by assigning a color to attribute 0 with a PALETTE statement. For example, to make the background color 8224 (a light violet), you would use the following PALETTE statement:

  • PALETTE 0,8224

In screen mode 11 you can set both the foreground and background color by assigning a color to attribute 0 with a PALETTE statement.

With an EGA, VGA, or MCGA installed, the PALETTE statement gives you flexibility in assigning different display colors to the actual color-number ranges for the foreground, background, and border colors discussed above. See the PALETTE statement for more details.

Example

The following series of examples show COLOR statements and their effects in the various screen modes:

'*** Programming example for COLOR statement*** SCREEN 0 'text mode only 'foreground = 1 (blue), background = 2 (green), border = 3 (cyan) 'EGA and VGA boards do not support the border argument COLOR 1, 2, 3 CLS LOCATE 12,25: PRINT "Press any key to continue..." DO LOOP WHILE INKEY$ = "" SCREEN 1 'set screen to 320 x 200 graphics 'background = 1 (blue), foreground = even palette number (red, green, yellow) COLOR 1, 0 LINE (20, 20) - (300, 180), 3, B 'draw a box LOCATE 12,7: PRINT "Press any key to continue..." DO LOOP WHILE INKEY$ = "" 'background = 2 (green), foreground = odd palette (white, magenta, cyan) COLOR 2, 1 LINE (20, 20) - (300, 180), 3, B 'draw a box LOCATE 12,7: PRINT "Press any key to continue..." DO LOOP WHILE INKEY$ = "" SCREEN 0 'set screen to text mode 'foreground = 7 (white), background = 0 (black), border = 0 (black) COLOR 7, 0, 0 CLS END
Syntax
  • COLOR [foreground&] [,[background&] [,border&]]Screen mode 0 (text only)
    COLOR [background&] [,palette%]Screen mode 1
    COLOR [foreground&]Screen mode 4
    COLOR [foreground&] [,background&]Screen modes 7-10
    COLOR [foreground&]Screen modes 12, 13
Description/Parameter(s)
foreground& Text color attribute in screen mode 0; text or line-drawing display color in screen mode 4; text and line-drawing color attribute in screen modes 7-10, 12, and 13.
background& Color code or color attribute for screen background.
border& Color code for the area surrounding the screen.
palette% In screen mode 1, one of two three-color palettes:
0 is green, red, and brown;
1 is cyan, magenta, and bright white.
  • Screen mode 0 (text only) syntax:
    • COLOR [foreground&] [,background&] [,border&]]
    • The foreground color value must be an integer expression between 0 and 31, inclusive. Sixteen colors can be selected with the integers 0-15. A blinking version of a color is specified by adding 16 to the color number. For example, a blinking color 7 is equal to 7 + 16, or 23.
    • The background color value is an integer expression between 0 and 7, inclusive, and is the color of the background for each text character. Blinking background colors are not supported.
    • The border color, which fills the screen area outside the text area, is an integer expression between 0 and 15, inclusive. Note that the border& argument has no effect with the following adapters:
      • the IBM Enhanced Graphics Adapter (EGA),
      • the IBM Video Graphics Array adapter (VGA), and
      • the IBM Multicolor Graphics Array adapter (MCGA).
  • Screen mode 1 syntax:
    • COLOR [background&] [,palette%]
    • The background color value must be an integer expression between 0 and 3, inclusive.
    • The palette% argument is an integer expression with an odd or even value, between 0 and 255, inclusive. An even value makes available one set of three default foreground colors, and an odd value makes available a different default set. The following BASIC code defines the two default sets of colors in terms of EGA PALETTE statements:
    • Definition of the even palette-number set.
      COLOR , 0' In screen mode 1, an even palette number is equivalent to next three EGA PALETTE statements:
      PALETTE 1, 2' EGA Attribute 1 = color 2 (green)
      PALETTE 2, 4' EGA Attribute 2 = color 4 (red)
      PALETTE3, 6' EGA Attribute 3 = color 6 (yellow)
    • Definition of the odd palette number set
      COLOR , 1' In screen mode 1, an odd palette-number is equivalent to:
      PALETTE 1, 3' EGA Attribute 1 = color 3 (cyan)
      PALETTE 2, 5' EGA Attribute 2 = color 5 (magenta)
      PALETTE 3, 7' EGA Attribute 3 = color 7 (white)
      Important: In screen mode 1, a COLOR statement overrides PALETTE statements that were executed previously.
  • Screen mode 4 syntax:
    • COLOR (foreground&)
    • The argument foreground&, the screen foreground color value, is an integer expression between 0 and 15, inclusive. This expression sets the display color associated with color attribute 1 only. The screen background color is fixed as black.
  • Screen mode 7-10 syntax:
    • COLOR [foreground&] [,background&]
    • The foreground, the line-drawing color, is a color attribute.
    • The background, the screen color, is a display color.
  • Screen mode 12-13 syntax:
    • COLOR (foreground&)
    • The foreground, the line drawing color, is a color attribute.
  • The COLOR statement does not determine the range of available colors. The combination of adapter, display, and the screen mode determine the color range. See the SCREEN statement for more details. If you use COLOR statement arguments outside these valid ranges, BASIC generates the error message, "Illegal function call."

Usage Notes

  • BASIC generates the error message, "Illegal function call" if the COLOR statement is used in screen modes 2, 3, or 11. Use the PALETTE statement to set the color in screen mode 11.
  • The foreground can be the same color as the background, making characters and graphics invisible.
  • The default background color is black for all display hardware configurations and all screen modes. This background is produced by the default display color of 0 associated with the background color attribute of 0.
  • With an EGA, VGA, or MCGA installed, the PALETTE statement gives you flexibility in assigning different display colors to the attribute color ranges for the foreground, background, and border colors.
  • In screen modes 12 and 13 you can set the background color by assigning a color to attribute 0 with a PALETTE statement. For example, to make the background color 8224 (olive green), you would use the following PALETTE statement:
    • PALETTE 0, 8224
    To make the background color bright red-violet, use:
    • PALETTE 0, 4128831
  • In screen mode 11 you can set both the foreground and background color by assigning a color to attribute 0 with a PALETTE statement.
Example

These examples use COLOR statements to show their effects in various screen modes.

SCREEN 0 'Text mode only. 'Foreground = 1 (blue), background = 2 (green), border = 3 (cyan). 'EGA and VGA boards do not support the border argument. COLOR 1, 2, 3 CLS LOCATE 12, 25: PRINT "Press any key to continue..." DO LOOP WHILE INKEY$ = "" SCREEN 1 'Set screen to 320 x 200 graphics. 'Background = 1 (blue), foreground = even palette number (red, green, 'yellow). COLOR 1, 0 LINE (20, 20)-(300, 180), 3, B 'Draw a brown box. LOCATE 12, 7: PRINT "Press any key to continue..." DO LOOP WHILE INKEY$ = "" 'Background = 2 (green), foreground = odd palette (white, magenta, cyan). COLOR 2, 1 LINE (20, 20)-(300, 180), 3, B 'Draw a bright white box. LOCATE 12, 7: PRINT "Press any key to continue..." DO LOOP WHILE INKEY$ = "" SCREEN 0 'Set screen to text mode. 'Foreground = 7 (white), background = 0 (black), border = 0 (black). COLOR 7, 0, 0 CLS END