COLOR - QB64 Wiki

COLOR

From QB64 Wiki

Revision as of 22:56, 15 September 2012 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The COLOR statement is used to change the color of text and background in some SCREEN modes.

Contents


Syntax:

COLOR [foreground%][, background%]


  • Background colors are available in SCREEN modes 0, 1, 7, 8 and 9 only.
  • SCREEN mode 10 has only 3 white foreground attributes including flashing.
  • SCREEN modes 12 and 13 can use the foreground parameter only! Background color 0 can be changed using OUT.
  • SCREEN modes 2 and 11 cannot use the COLOR keyword as they are monochrome with white foreground!
  • An illegal function error will occur if a background color is used in other screen modes!
  • To change the background color only, use a comma and the color. EX: COLOR ,background%


Screen Mode Attributes

  • SCREEN 0 background colors 0 to 7 can be changed each text character without affecting other text. Use CLS after a background color statement to create a fullscreen background color. 64 DAC hues with 16 high intensity blinking foreground (16 to 31) color attributes. QBasic windows will not flash in a window in NT, XP, VISTA or 7 (will flash in QB64). See example 7 below for more SCREEN 0 background colors.
  • SCREEN 1 has 4 background color attributes: 0 = black, 1 = blue, 2 = green, 3 = grey. White foreground color only.
  • SCREEN 2 is monochrome with white forecolor and black background. Cannot use the COLOR statement!
  • SCREEN 7 can use 16 (DAC) colors with background colors. RGB settings can be changed in colors 0 to 7 using OUT.
  • SCREEN 8 has 16 color attributes with 16 background colors.
  • SCREEN 9 can use up to 64 DAC color hues in 16 color attributes with background colors assigned to attribute 0 with a PALETTE swap. RGB settings can be changed in colors 0 to 5 and 7 using OUT.
  • SCREEN 10 has only 4 color attributes with black background. COLOR 0 = black, 1 = grey, 2 = flash white and 3 = bright white.
  • SCREEN 11 is monochrome with white forecolor and a black background, Cannot use the COLOR statement!
  • SCREEN 12 can use 16 color attributes with a black background. 256K possible RGB color hues.
  • SCREEN 13 can use 256 color attributes with a black background. 256K possible RGB hues.
  • DAC screens 0, 7 and 9 color changes are limited in Qbasic ONLY!
  • PALETTE swaps can be made in SCREEN 7 and 9 only. Those screens were DAC screen modes in Qbasic.
  • _DEST can be used to set the destination page or image to color using QB64.
  • _DEFAULTCOLOR returns the current color being used on an image or screen page handle.


24/32 Bit Colors using QB64
  • Pixel color intensities for Red, Green, Blue and Alpha range from 0 to 255 when used with _RGB, _RGBA, _RGB32 and RGBA32.
  • Combined RGB function values returned are LONG values! Blue intensity values may be cut off using SINGLE values!
  • _ALPHA transparency values can range from 0 as transparent up to 255 which is fully opaque.
  • _CLEARCOLOR can also be used to set a color as transparent.
  • Colors can be mixed by using _BLEND(default) in 32 bit screen modes ONLY. _DONTBLEND disables blending.
  • NOTE: Default 32 bit backgrounds are clear black or _RGBA(0, 0, 0, 0)! Use CLS to make the black opaque!

(Return to Table of Contents)

RGB Palette Intensities

Basic's 16 Default Color Attributes (non-DAC)

 Attribute    Description     Red   Green   Blue 32 HEX HTML Name  0 Black 0 0 0 000000 Black  1 Dark Blue 0 0 42 00008B DarkBlue  2 Dark Green 0 42 0 006400 DarkGreen  3 Dark Cyan 0 42 42 008B8B DarkCyan  4 Dark Red 42 0 0 8B0000 DarkRed  5 Dark Magenta 42 0 42 8B008B DarkMagenta  6 Dark Yellow 42 21 0 DAA520 GoldenRod  7 Light Grey 42 42 42 D3D3D3 LightGrey 8 Dark Grey 21 21 21 696969 DimGray  9 Blue 21 21 63 0000FF Blue 10 Green 21 63 21 15FF15 Lime 11 Cyan 21 63 63 15FFFF Cyan 12 Red 63 21 21 FF1515 Red 13 Magenta 63 21 63 FF15FF Magenta 14 Yellow 63 63 21 FFFF00 Yellow 15 White 63 63 63 FFFFFF White

HTML Color Table Values and Names or Other RGB colors
Note: QB64 32 bit color intensity values from 0 to 255 can be found by multiplying above values by 4.

Summary: The red, green, and blue intensity values can be changed using OUT or PALETTE statements. Some Qbasic RGB color attribute values can be changed in DAC SCREEN modes and the DAC RGB intensity settings may be different.

Color Port access using INP and OUT
OUT &H3C7, attribute : Set port to read RGB settings
color_intensity = INP(&H3C9) 'reads present setting
OUT &H3C8, attribute : Set port to write RGB settings
OUT &H3C9, color_intensity : Writes new settings
  • After every 3 reads or writes, changes to next higher color attribute. Loops can be used to set more than one attribute's intensities.
  • Color port setting of Red, Green and Blue intensities can be done in ascending order.
  • Color port attribute intensity values range from 0 to 63(1/4 of the 32 bit values) only in Qbasic 4 and 8 bit screen modes.

(Return to Table of Contents)

Examples:

Example 1: 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


Example 2: 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


Example 3: 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.


Example 4: 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.


Example 5: 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


Example 6: 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).


Example 7: Since SCREEN 0 only uses background colors 0 to 7 by default, use OUT to change color intensities of color 0 in QB64 only.

OUT &H3C8, 0 'change color 0 intensities OUT &H3C9, 63 OUT &H3C9, 63 OUT &H3C9, 63 OUT &H3C8, 8 'change color 8 intensities OUT &H3C9, 0 OUT &H3C9, 0 OUT &H3C9, 0 COLOR 8: PRINT "Black on bright white!"

Black on bright white!

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

(Return to Table of Contents)

References:

See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page