QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

The COLOR statement is used to change the foreground and background colors for printing text.

Syntax

COLOR [foreground&][, background&]

Description

Screen Mode Attributes

24/32-Bit colors using QB64

RGB Palette Intensities

RGB intensity values can be converted to hexadecimal values to create the LONG _PALETTECOLOR value in non-32-bit screens:


SCREEN 12
alpha$ = "FF" 'solid alpha colors only
OUT &H3C8, 0: OUT &H3C9, 0: OUT &H3C9, 0: OUT &H3C9, 20 'set black background to dark blue
PRINT "Attribute = Hex value      Red          Green         Blue "
PRINT
COLOR 7 
FOR attribute = 0 TO 15
  OUT &H3C7, attribute 'set color attribute to read
  red$ = HEX$(INP(&H3C9) * 4) 'convert port setting to 32 bit values
  grn$ = HEX$(INP(&H3C9) * 4)
  blu$ = HEX$(INP(&H3C9) * 4)
  IF LEN(red$) = 1 THEN red$ = "0" + red$ '2 hex digits required
  IF LEN(grn$) = 1 THEN grn$ = "0" + grn$ 'for low or zero hex values
  IF LEN(blu$) = 1 THEN blu$ = "0" + blu$
  hex32$ = "&H" + alpha$ + red$ + grn$ + blu$
  _PALETTECOLOR attribute, VAL(hex32$) 'VAL converts hex string to a LONG 32 bit value
  IF attribute THEN COLOR attribute 'exclude black color print
  PRINT "COLOR" + STR$(attribute) + " = " + hex32$, red$, grn$, blu$ 'returns closest attribute
NEXT


Attribute  Hex value      Red        Green       Blue 

COLOR 0 = &HFF000050       00         00         50
COLOR 1 = &HFF0000A8       00         00         A8
COLOR 2 = &HFF00A800       00         A8         00
COLOR 3 = &HFF00A8A8       00         A8         A8
COLOR 4 = &HFFA80000       A8         00         00
COLOR 5 = &HFFA800A8       A8         00         A8
COLOR 6 = &HFFA85400       A8         54         00
COLOR 7 = &HFFA8A8A8       A8         A8         A8
COLOR 8 = &HFF545454       54         54         54
COLOR 9 = &HFF5454FC       54         54         FC
COLOR 10 = &HFF54FC54      54         FC         54
COLOR 11 = &HFF5454FC      54         FC         FC
COLOR 12 = &HFFFC5454      FC         54         54
COLOR 13 = &HFFFC54FC      FC         54         FC
COLOR 14 = &HFFFCFC54      FC         FC         54
COLOR 15 = &HFFFCFCFC      FC         FC         FC

Explanation: The RGB intensity values are multiplied by 4 to get the _RGB intensity values as HEX$ values. The individual 2 digit HEX$ intensity values can be added to “&HFF” to make up the 32-bit hexadecimal string value necessary for VAL to return to _PALETTECOLOR. The statement is only included in the example to show how that can be done with any 32-bit color value. Note: Black has a blue hex value of 50 due to the OUT background color setting which makes it dark blue.

Reading and setting color port intensities using INP and OUT

OUT &H3C7, attribute ‘Set port to read RGB settings with:

color_intensity = INP(&H3C9) ‘reads present intensity setting

OUT &H3C8, attribute ‘Set port to write RGB settings with:

OUT &H3C9, color_intensity ‘writes new intensity setting

Example(s)

Reading the default RGB color settings of color attribute 15.


OUT &H3C7, 15
red% = INP(&H3C9)
green% = INP(&H3C9)
blue% = INP(&H3C9)
PRINT red%, green%, blue% 


 63       63       63

Changing the color settings of attribute 0 (the background) to dark blue in SCREENs 12 or 13.


SCREEN 12
OUT &H3C8, 0          'set color port attribute to write
OUT &H3C9, 0          'red intensity
OUT &H3C9, 0          'green intensity
OUT &H3C9, 30         'blue intensity

OUT &H3C7, 0
PRINT INP(&H3C9); INP(&H3C9); INP(&H3C9)
END


 0  0  30 

Printing in fullscreen SCREEN 0 mode with a color background under the text only.


SCREEN 0: _FULLSCREEN ' used for fullscreen instead of window
COLOR 30, 6: LOCATE 12, 4: PRINT "Hello!"

Result: Hello! is printed in flashing high intensity yellow with brown background behind text only when in QBasic _FULLSCREEN.

Using CLS after setting the background color in SCREEN 0 to make the color cover the entire screen.


SCREEN 0: _FULLSCREEN
COLOR , 7: CLS
COLOR 9: PRINT "Hello" 


Hello

Result: The blue word Hello is printed to a totally grey background in _FULLSCREEN.

Using a different foreground color for each letter:


SCREEN 0
COLOR 1: PRINT "H";
COLOR 3: PRINT "E";
COLOR 4: PRINT "L";
COLOR 5: PRINT "L";
COLOR 6: PRINT "O"
COLOR 9: PRINT "W";
COLOR 11: PRINT "O";
COLOR 12: PRINT "R";
COLOR 13: PRINT "L";
COLOR 14: PRINT "D" 


HELLO
WORLD

Doing the same as Example 5 but in only a few lines:


SCREEN 0
text$ = "HelloWorld"
FOR textpos = 1 TO LEN(text$)
  COLOR textpos
  IF textpos <> 5 THEN PRINT MID$(text$, textpos, 1);
  IF textpos = 5 THEN PRINT MID$(text$, textpos, 1)    'start print on next row
NEXT



Hello
World

Explanation:Semicolon(;) means that the next PRINT happens on the same line, we don’t want that when it comes to position 5 so when it is at position 5 the next PRINT will move to the next line (when it isn’t at position 5 we want it to continue printing the letter side-by-side on the same line though).

Since SCREEN 0 only uses background colors 0 to 7 by default, use _PALETTECOLOR to change color intensities of color 0.


_PALETTECOLOR 0, _RGB32(255, 255, 255) 'change color 0 intensity
_PALETTECOLOR 8, _RGB32(0, 0, 0) 'change color 8 intensity

COLOR 8: PRINT "Black on bright white!"


Black on bright white!


Explanation: Since QB64 does not have DAC SCREEN 0 limitations, changing color intensities for custom background colors is possible.

Changing light gray text in SCREEN 0 to a 32 bit custom color using a LONG HTML hexadecimal value:


COLOR 7
PRINT "Color 7 is gray"
K$ = INPUT$(1)
_PALETTECOLOR 7, &HFFDAA520 ' FF alpha makes the color translucent
PRINT "Color 7 is now Goldenrod in SCREEN 0!


Color 7 is gray
Color 7 is now Goldenrod in SCREEN 0!

Explanation: _RGB32 could be used to make custom 32 bit colors or HTML values could be used after &HFF for solid colors.

See Also