COLOR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The COLOR statement is used to change the foreground and background colors for printing text. {{PageSyntax}} : COLOR [{{Parameter|foreground&}}][, {{Parameter|background&}}] {{PageDescription}} * {{Parameter|background&}} colors are available in all QB64 color SCREEN modes. * SCREEN mode 10 has only 3 white foreground attributes including flashing. * To change the {{Parameter|background&}} color only, use a comma and the desired color. Ex: COLOR , {{...")
 
No edit summary
 
(21 intermediate revisions by 2 users not shown)
Line 1: Line 1:
The [[COLOR]] statement is used to change the foreground and background colors for printing text.
The [[COLOR]] statement is used to change the foreground and background colors for printing text.


{{PageSyntax}}
{{PageSyntax}}
Line 6: Line 7:


{{PageDescription}}
{{PageDescription}}
* {{Parameter|background&}} colors are available in all QB64 color SCREEN modes.  
* {{Parameter|background&}} colors are available in all QB64 color SCREEN modes.
* [[SCREEN]] mode 10 has only 3 white foreground attributes including flashing.  
* [[SCREEN]] mode 10 has only 3 white foreground attributes including flashing.
* To change the {{Parameter|background&}} color only, use a comma and the desired color. Ex: [[COLOR]] , {{Parameter|background&}}
* To change the {{Parameter|background&}} color only, use a comma and the desired color. Ex: [[COLOR]] , {{Parameter|background&}}
* Graphic drawing statements like [[PSET]], [[PRESET]], [[LINE]], etc, also use the colors set by the [[COLOR]] statement if no color is passed when they are called.
* Graphic drawing statements like [[PSET]], [[PRESET]], [[LINE]], etc, also use the colors set by the [[COLOR]] statement if no color is passed when they are called.
Line 13: Line 14:




==Screen Mode Attributes==
== Screen Mode Attributes ==
* '''SCREEN 0''' {{Parameter|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. See [[_BLINK]].
* '''SCREEN 0''' {{Parameter|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. See [[_BLINK]].
** See example 7 below for more SCREEN 0 background colors.
** 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 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.
* '''SCREEN 2''' is '''monochrome''' with white forecolor and black background.
* '''SCREEN 7''' can use 16 ([[DAC]]) colors with background colors. RGB settings can be changed in colors 0 to 7 using [[_PALETTECOLOR]].  
* '''SCREEN 7''' can use 16 ([[DAC]]) colors with background colors. RGB settings can be changed in colors 0 to 7 using [[_PALETTECOLOR]].
* '''SCREEN 8''' has 16 color attributes with 16 background colors.  
* '''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 [[_PALETTECOLOR]] swap. RGB settings can be changed in colors 0 to 5 and 7 using [[_PALETTECOLOR]].
* '''SCREEN 9''' can use up to 64 [[DAC]] color hues in 16 color attributes with background colors assigned to attribute 0 with a [[_PALETTECOLOR]] swap. RGB settings can be changed in colors 0 to 5 and 7 using [[_PALETTECOLOR]].
* '''SCREEN 10''' has '''only 4 color attributes''' with black background. COLOR 0 = black, 1 = grey, 2 = flash white and 3 = bright white.
* '''SCREEN 10''' has '''only 4 color attributes''' with black background. COLOR 0 = black, 1 = grey, 2 = flash white and 3 = bright white.
Line 29: Line 30:
* [[_DEFAULTCOLOR]] returns the current color being used on an image or screen page handle.
* [[_DEFAULTCOLOR]] returns the current color being used on an image or screen page handle.


 
=== 24/32-Bit colors using QB64 ===
===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]].
* 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]] variables.'''
* Combined RGB function values returned are [[LONG]] values. '''Blue intensity values may be cut off using [[SINGLE]] variables.'''
* [[_ALPHA]] transparency values can range from 0 as transparent up to 255 which is fully opaque.  
* [[_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.
* [[_CLEARCOLOR]] can also be used to set a color as transparent.
* Colors can be mixed by using [[_BLEND]] (default) in 32-bit screen modes. [[_DONTBLEND]] disables blending.
* Colors can be mixed by using [[_BLEND]] (default) in 32-bit screen modes. [[_DONTBLEND]] disables blending.
Line 40: Line 40:
<p style="text-align: center">([[#toc|Return to Table of Contents]])</p>
<p style="text-align: center">([[#toc|Return to Table of Contents]])</p>


==RGB Palette Intensities==
 
== RGB Palette Intensities ==
RGB intensity values can be converted to hexadecimal values to create the [[LONG]] [[_PALETTECOLOR]] value in non-32-bit screens:
RGB intensity values can be converted to hexadecimal values to create the [[LONG]] [[_PALETTECOLOR]] value in non-32-bit screens:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12
alpha$ = "FF" 'solid alpha colors only
alpha$ = "FF" 'solid alpha colors only
{{Cl|OUT}} {{Cl|&H}}3C8, 0: {{Cl|OUT}} {{Cl|&H}}3C9, 0: {{Cl|OUT}} {{Cl|&H}}3C9, 0: {{Cl|OUT}} {{Cl|&H}}3C9, 20 'set black background to dark blue
{{Cl|PRINT}} "Attribute = Hex value      Red          Green        Blue "
{{Cl|PRINT}} "Attribute = Hex value      Red          Green        Blue "
{{Cl|PRINT}}
{{Cl|PRINT}}
{{Cl|COLOR}} 7  
{{Cl|COLOR}} 7
{{Cl|FOR...NEXT|FOR}} attribute = 0 {{Cl|TO}} 15
{{Cl|FOR...NEXT|FOR}} attribute = 1 {{Cl|TO}} 15
   {{Cl|OUT}} {{Cl|&H}}3C7, attribute 'set color attribute to read
   {{Cl|OUT}} {{Cl|&H}}3C7, attribute 'set color attribute to read
   red$ = {{Cl|HEX$}}({{Cl|INP}}({{Cl|&H}}3C9) * 4) 'convert port setting to 32 bit values
   red$ = {{Cl|HEX$}}({{Cl|INP}}({{Cl|&H}}3C9) * 255 / 63) 'convert port setting to 32 bit values
   grn$ = {{Cl|HEX$}}({{Cl|INP}}({{Cl|&H}}3C9) * 4)
   grn$ = {{Cl|HEX$}}({{Cl|INP}}({{Cl|&H}}3C9) * 255 / 63)
   blu$ = {{Cl|HEX$}}({{Cl|INP}}({{Cl|&H}}3C9) * 4)
   blu$ = {{Cl|HEX$}}({{Cl|INP}}({{Cl|&H}}3C9) * 255 / 63)
   {{Cl|IF...THEN|IF}} {{Cl|LEN}}(red$) = 1 {{Cl|THEN}} red$ = "0" + red$ '2 hex digits required
   {{Cl|IF...THEN|IF}} {{Cl|LEN}}(red$) = 1 {{Cl|THEN}} red$ = "0" + red$ '2 hex digits required
   {{Cl|IF...THEN|IF}} {{Cl|LEN}}(grn$) = 1 {{Cl|THEN}} grn$ = "0" + grn$ 'for low or zero hex values
   {{Cl|IF...THEN|IF}} {{Cl|LEN}}(grn$) = 1 {{Cl|THEN}} grn$ = "0" + grn$ 'for low or zero hex values
Line 61: Line 61:
   {{Cl|IF...THEN|IF}} attribute {{Cl|THEN}} {{Cl|COLOR}} attribute 'exclude black color print
   {{Cl|IF...THEN|IF}} attribute {{Cl|THEN}} {{Cl|COLOR}} attribute 'exclude black color print
   {{Cl|PRINT}} "{{Cl|COLOR}}" + {{Cl|STR$}}(attribute) + " = " + hex32$, red$, grn$, blu$ 'returns closest attribute
   {{Cl|PRINT}} "{{Cl|COLOR}}" + {{Cl|STR$}}(attribute) + " = " + hex32$, red$, grn$, blu$ 'returns closest attribute
{{Cl|NEXT}} '' ''
{{Cl|NEXT}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}Attribute  Hex value      Red        Green      Blue  
{{OutputStart}}Attribute  Hex value      Red        Green      Blue


{{text|COLOR 0 <nowiki>=</nowiki> &HFF000050      00        00        50|#A8A8A8}}
{{Text|COLOR 1 <nowiki>=</nowiki> &HFF0000AA       00        00        AA|#0000AA}}
{{text|COLOR 1 <nowiki>=</nowiki> &HFF0000A8       00        00        A8|#0050A8}}
{{Text|COLOR 2 <nowiki>=</nowiki> &HFF00AA00       00        AA         00|#00AA00}}
{{text|COLOR 2 <nowiki>=</nowiki> &HFF00A800       00        A8         00|#00A800}}
{{Text|COLOR 3 <nowiki>=</nowiki> &HFF00AAAA       00        AA         AA|#00AAAA}}
{{text|COLOR 3 <nowiki>=</nowiki> &HFF00A8A8       00        A8         A8|#00A8A8}}
{{Text|COLOR 4 <nowiki>=</nowiki> &HFFAA0000       AA         00        00|#AA0000}}
{{text|COLOR 4 <nowiki>=</nowiki> &HFFA80000       A8         00        00|#A80000}}
{{Text|COLOR 5 <nowiki>=</nowiki> &HFFAA00AA       AA         00        AA|#AA00AA}}
{{text|COLOR 5 <nowiki>=</nowiki> &HFFA800A8       A8         00        A8|#A800A8}}
{{Text|COLOR 6 <nowiki>=</nowiki> &HFFAA5500       AA         55         00|#AA5500}}
{{text|COLOR 6 <nowiki>=</nowiki> &HFFA85400       A8         54         00|#A85400}}
{{Text|COLOR 7 <nowiki>=</nowiki> &HFFAAAAAA       AA         AA         AA|#AAAAAA}}
{{text|COLOR 7 <nowiki>=</nowiki> &HFFA8A8A8       A8         A8         A8|#A8A8A8}}
{{Text|COLOR 8 <nowiki>=</nowiki> &HFF555555       55         55         55|#555555}}
{{text|COLOR 8 <nowiki>=</nowiki> &HFF545454       54         54         54|#545454}}
{{Text|COLOR 9 <nowiki>=</nowiki> &HFF5555FF       55         55         FF|#5555FF}}
{{text|COLOR 9 <nowiki>=</nowiki> &HFF5454FC       54         54         FC|#5454FC}}
{{Text|COLOR 10 <nowiki>=</nowiki> &HFF55FF55     55         FF         55|#55FF55}}
{{text|COLOR 10 <nowiki>=</nowiki> &HFF54FC54     54         FC         54|#54FC54}}
{{Text|COLOR 11 <nowiki>=</nowiki> &HFF55FFFF     55         FF         FF|#55FFFF}}
{{text|COLOR 11 <nowiki>=</nowiki> &HFF5454FC     54         FC         FC|#54FCFC}}
{{Text|COLOR 12 <nowiki>=</nowiki> &HFFFF5555     FF         55         55|#FF5555}}
{{text|COLOR 12 <nowiki>=</nowiki> &HFFFC5454     FC         54         54|#FC5454}}
{{Text|COLOR 13 <nowiki>=</nowiki> &HFFFF55FF     FF         55         FF|#FF55FF}}
{{text|COLOR 13 <nowiki>=</nowiki> &HFFFC54FC     FC         54         FC|#FC54FC}}
{{Text|COLOR 14 <nowiki>=</nowiki> &HFFFFFF55     FF         FF         55|#FFFF55}}
{{text|COLOR 14 <nowiki>=</nowiki> &HFFFCFC54     FC         FC         54|#FCFC54}}
{{Text|COLOR 15 <nowiki>=</nowiki> &HFFFFFFFF     FF         FF         FF|#F5F5F5}}
{{text|COLOR 15 <nowiki>=</nowiki> &HFFFCFCFC     FC         FC         FC|#FCFCFC}}
{{OutputEnd}}
{{OutputEnd}}
:''Explanation:'' The RGB intensity values are multiplied by 4 to get the [[_RGB]] intensity values as [[HEX$|hexadecimal]] 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.
:''Explanation:'' The DAC intensity values are multiplied by (255 / 63) to get the [[_RGB]] intensity values as [[HEX$|hexadecimal]] 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]]===
=== Read & write color port intensities with [[INP]] & [[OUT]] ===
* Legacy code may use [[INP]] and [[OUT]] to read or set color port intensities. '''QB64''' emulates VGA memory to maintain compatibility.
* Legacy code may use [[INP]] and [[OUT]] to read or set color port intensities. '''QB64''' emulates VGA memory to maintain compatibility.
* The same can be achieved using [[_PALETTECOLOR]] ('''recommended practice''').
* The same can be achieved using [[_PALETTECOLOR]] ('''recommended practice''').


:'''{{text|OUT &H3C7, attribute|green}}''' 'Set port to read RGB settings with:
:'''{{Text|OUT &H3C7, attribute|green}}''' 'Set port to read RGB settings with:
:'''{{text|color_intensity <nowiki>=</nowiki> INP(&H3C9)|green}}''' 'reads present intensity setting
:'''{{Text|color_intensity <nowiki>=</nowiki> INP(&H3C9)|green}}''' 'reads present intensity setting
 
:'''{{text|OUT &H3C8, attribute|green}}''' 'Set port to write RGB settings with:
:'''{{Text|OUT &H3C8, attribute|green}}''' 'Set port to write RGB settings with:
:'''{{text|OUT &H3C9, color_intensity|green}}''' 'writes new intensity setting
:'''{{Text|OUT &H3C9, color_intensity|green}}''' 'writes new intensity setting


* After every 3 reads or writes, changes to next higher color attribute. Loops can be used to set more than one attribute's intensities.
* After every 3 reads or writes, changes to next higher color attribute. Loops can be used to set more than one attribute's intensities.
Line 105: Line 102:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Reading the default RGB color settings of color attribute 15.
''Example 1:'' Reading the default RGB color settings of color attribute 15.
{{CodeStart}} '' ''
{{CodeStart}}
  {{Cl|OUT}} &H3C7, 15
  {{Cl|OUT}} &H3C7, 15
  red% = {{Cl|INP}}(&H3C9)
  red% = {{Cl|INP}}(&H3C9)
  green% = {{Cl|INP}}(&H3C9)
  green% = {{Cl|INP}}(&H3C9)
  blue% = {{Cl|INP}}(&H3C9)
  blue% = {{Cl|INP}}(&H3C9)
  {{Cl|PRINT}} red%, green%, blue% '' ''
  {{Cl|PRINT}} red%, green%, blue%
{{CodeEnd}}
{{CodeEnd}}


Line 118: Line 115:




''Example 2:'' Changing the color settings of attribute 0 (the background) to dark blue in [[SCREEN]]s 12 or 13.
''Example 2:'' Changing the color settings of attribute 0 (the background) to blue in [[SCREEN]]s 12 or 13.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12
{{Cl|OUT}} {{Cl|&H}}3C8, 0          'set color port attribute to write
{{Cl|OUT}} {{Cl|&H}}3C8, 0          'set color port attribute to write
{{Cl|OUT}} {{Cl|&H}}3C9, 0          'red intensity
{{Cl|OUT}} {{Cl|&H}}3C9, 0          'red intensity
{{Cl|OUT}} {{Cl|&H}}3C9, 0          'green intensity
{{Cl|OUT}} {{Cl|&H}}3C9, 0          'green intensity
{{Cl|OUT}} {{Cl|&H}}3C9, 30         'blue intensity  
{{Cl|OUT}} {{Cl|&H}}3C9, 42         'blue intensity


{{Cl|OUT}} {{Cl|&H}}3C7, 0
{{Cl|OUT}} {{Cl|&H}}3C7, 0
{{Cl|PRINT}} {{Cl|INP}}({{Cl|&H}}3C9); {{Cl|INP}}({{Cl|&H}}3C9); {{Cl|INP}}({{Cl|&H}}3C9)
{{Cl|PRINT}} {{Cl|INP}}({{Cl|&H}}3C9); {{Cl|INP}}({{Cl|&H}}3C9); {{Cl|INP}}({{Cl|&H}}3C9)
{{Cl|END}}'' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{CodeStart}} 0  0  30 {{CodeEnd}}
{{OutputStartBG1}}{{Text| 0  0  42|#FCFCFC}}{{OutputEnd}}




''Example 3:'' Printing in fullscreen SCREEN 0 mode with a color background under the text only.
''Example 3:'' Printing in fullscreen SCREEN 0 mode with a color background under the text only.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 0: {{Cl|_FULLSCREEN}} ' used for fullscreen instead of window
{{Cl|SCREEN}} 0: {{Cl|_FULLSCREEN}} ' used for fullscreen instead of window
{{Cl|COLOR}} 30, 6: {{Cl|LOCATE}} 12, 4: {{Cl|PRINT}} "Hello!" '' ''
{{Cl|COLOR}} 14, 6: {{Cl|LOCATE}} 4, 4: {{Cl|PRINT}} "Hello!"
{{CodeEnd}}
{{CodeEnd}}
:''Result:'' Hello! is printed in flashing high intensity yellow with brown background behind text only when in Qbasic [[_FULLSCREEN|fullscreen]].
{{OutputStart}}
 
 
    {{Ot|Hello!|#fcfc54|#aa5500}}
{{OutputEnd}}




''Example 4:'' Using [[CLS]] after setting the background color in SCREEN 0 to make the color cover the entire screen.
''Example 4:'' Using [[CLS]] after setting the background color in SCREEN 0 to make the color cover the entire screen.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 0: {{Cl|_FULLSCREEN}}
{{Cl|SCREEN}} 0: {{Cl|_FULLSCREEN}}
{{Cl|COLOR}} , 7: {{Cl|CLS}}
{{Cl|COLOR}} , 7: {{Cl|CLS}}
{{Cl|COLOR}} 9: {{Cl|PRINT}} "Hello" '' ''
{{Cl|COLOR}} 9: {{Cl|PRINT}} "Hello"
{{CodeEnd}}
{{CodeEnd}}
{{TextStart}}{{text|Hello|blue}}{{TextEnd}}
{{OutputStartBG7}}
:''Result:'' The blue word Hello is printed to a totally grey background in [[_FULLSCREEN|fullscreen]].
{{Text|Hello|#5454fc}}
{{OutputEnd}}




''Example 5:'' Using a different foreground color for each letter:
''Example 5:'' Using a different foreground color for each letter:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 0
{{Cl|SCREEN}} 0
{{Cl|COLOR}} 1: {{Cl|PRINT}} "H";
{{Cl|COLOR}} 1: {{Cl|PRINT}} "H";
Line 163: Line 165:
{{Cl|COLOR}} 12: {{Cl|PRINT}} "R";
{{Cl|COLOR}} 12: {{Cl|PRINT}} "R";
{{Cl|COLOR}} 13: {{Cl|PRINT}} "L";
{{Cl|COLOR}} 13: {{Cl|PRINT}} "L";
{{Cl|COLOR}} 14: {{Cl|PRINT}} "D" '' ''
{{Cl|COLOR}} 14: {{Cl|PRINT}} "D"
{{CodeEnd}}
 
{{OutputStart}}
{{text|H|darkblue}}{{text|E|darkcyan}}{{text|L|darkred}}{{text|L|darkmagenta}}{{text|O|goldenrod}}
{{text|W|blue}}{{text|O|cyan}}{{text|R|red}}{{text|L|magenta}}{{text|D|yellow}}
{{OutputEnd}}
 
 
''Example 6:'' Doing the same as Example 5 but in only a few lines:
{{CodeStart}} '' ''
{{Cl|SCREEN (statement)|SCREEN}} 0
text$ = "HelloWorld"
{{Cl|FOR...NEXT|FOR}} textpos = 1 {{Cl|TO}} {{Cl|LEN}}(text$)
  {{Cl|COLOR}} textpos
  {{Cl|IF...THEN|IF}} textpos <> 5 {{Cl|THEN}} {{Cl|PRINT}} {{Cl|MID$}}(text$, textpos, 1);
  {{Cl|IF...THEN|IF}} textpos = 5 {{Cl|THEN}} {{Cl|PRINT}} {{Cl|MID$}}(text$, textpos, 1)    'start print on next row
{{Cl|NEXT}}
 
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{text|H|darkblue}}{{text|e|darkgreen}}{{text|l|darkcyan}}{{text|l|darkred}}{{text|o|darkmagenta}}
{{text|W|goldenrod}}{{text|o|lightgrey}}{{text|r|dimgray}}{{text|l|blue}}{{text|d|lime}} '' ''
{{OutputEnd}}
: ''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 [[_PALETTECOLOR]] to change color intensities of color 0.
{{CodeStart}} '' ''
{{Cl|_PALETTECOLOR}} 0, _RGB32(255, 255, 255) 'change color 0 intensity
{{Cl|_PALETTECOLOR}} 8, _RGB32(0, 0, 0) 'change color 8 intensity
{{Cl|COLOR}} 8: {{Cl|PRINT}} "Black on bright white!" '' ''
{{CodeEnd}}
{{WhiteStart}}'''{{text|Black on bright white!|#000000}}'''
{{WhiteEnd}}
----
: ''Explanation:'' Since QB64 does not have [[DAC]] [[SCREEN]] 0 limitations, changing color intensities for custom background colors is possible.
''Example 8:'' Changing light gray text in [[SCREEN]] 0 to a 32 bit custom color using a [[LONG]] HTML hexadecimal value:
{{CodeStart}} '' ''
{{Cl|COLOR}} 7
{{Cl|PRINT}} "Color 7 is gray"
K$ = {{Cl|INPUT$}}(1)
{{Cl|_PALETTECOLOR}} 7, {{Cl|&H}}FFDAA520 ' FF alpha makes the color translucent
{{Cl|PRINT}} "Color 7 is now Goldenrod in {{Cl|SCREEN}} 0! '' ''
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
{{text|Color 7 is gray|#A8A8A8}}
{{Text|H|#0000aa}}{{Text|E|#00aaaa}}{{Text|L|#aa0000}}{{Text|L|#aa00aa}}{{Text|O|#aa5500}}
{{text|Color 7 is now Goldenrod in SCREEN 0!|#DAA520}}
{{Text|W|#5454fc}}{{Text|O|#54fcfc}}{{Text|R|#fc5454}}{{Text|L|#fc54fc}}{{Text|D|#fcfc54}}
{{OutputEnd}}
{{OutputEnd}}
: ''Explanation:'' [[_RGB32]] could be used to make custom 32 bit colors or HTML values could be used after &HFF for solid colors.
<p style="text-align: center">([[#toc|Return to Table of Contents]])</p>




Line 232: Line 187:
* [[_BACKGROUNDCOLOR]]
* [[_BACKGROUNDCOLOR]]
* [[_PALETTECOLOR]]
* [[_PALETTECOLOR]]
* [[Windows_Libraries#Color_Dialog_Box|Color Dialog Box]]
* [[Windows Libraries#Color_Dialog_Box|Color Dialog Box]]
* [http://www.w3schools.com/html/html_colornames.asp Hexadecimal Color Values]
* [http://www.w3schools.com/html/html_colornames.asp Hexadecimal Color Values]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 22:19, 11 February 2023

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


Syntax

COLOR [foreground&][, background&]


Description

  • background& colors are available in all QB64 color SCREEN modes.
  • SCREEN mode 10 has only 3 white foreground attributes including flashing.
  • To change the background& color only, use a comma and the desired color. Ex: COLOR , background&
  • Graphic drawing statements like PSET, PRESET, LINE, etc, also use the colors set by the COLOR statement if no color is passed when they are called.
  • The $COLOR metacommand adds named color constants for both text and 32-bit modes.


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. See _BLINK.
    • 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.
  • SCREEN 7 can use 16 (DAC) colors with background colors. RGB settings can be changed in colors 0 to 7 using _PALETTECOLOR.
  • 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 _PALETTECOLOR swap. RGB settings can be changed in colors 0 to 5 and 7 using _PALETTECOLOR.
  • 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.
  • SCREEN 12 can use 16 color attributes with a black background. 256K possible RGB color hues. Background colors can be used with QB64.
  • SCREEN 13 can use 256 color attributes with a black background. 256K possible RGB hues.
  • 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 variables.
  • _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. _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

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
PRINT "Attribute = Hex value      Red          Green         Blue "
PRINT
COLOR 7
FOR attribute = 1 TO 15
  OUT &H3C7, attribute 'set color attribute to read
  red$ = HEX$(INP(&H3C9) * 255 / 63) 'convert port setting to 32 bit values
  grn$ = HEX$(INP(&H3C9) * 255 / 63)
  blu$ = HEX$(INP(&H3C9) * 255 / 63)
  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 1 = &HFF0000AA       00         00         AA
COLOR 2 = &HFF00AA00       00         AA         00
COLOR 3 = &HFF00AAAA       00         AA         AA
COLOR 4 = &HFFAA0000       AA         00         00
COLOR 5 = &HFFAA00AA       AA         00         AA
COLOR 6 = &HFFAA5500       AA         55         00
COLOR 7 = &HFFAAAAAA       AA         AA         AA
COLOR 8 = &HFF555555       55         55         55
COLOR 9 = &HFF5555FF       55         55         FF
COLOR 10 = &HFF55FF55      55         FF         55
COLOR 11 = &HFF55FFFF      55         FF         FF
COLOR 12 = &HFFFF5555      FF         55         55
COLOR 13 = &HFFFF55FF      FF         55         FF
COLOR 14 = &HFFFFFF55      FF         FF         55
COLOR 15 = &HFFFFFFFF      FF         FF         FF
Explanation: The DAC intensity values are multiplied by (255 / 63) to get the _RGB intensity values as hexadecimal 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.

Read & write color port intensities with INP & OUT

  • Legacy code may use INP and OUT to read or set color port intensities. QB64 emulates VGA memory to maintain compatibility.
  • The same can be achieved using _PALETTECOLOR (recommended practice).
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
  • 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) in QBasic's legacy 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 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, 42         'blue intensity

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


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 14, 6: LOCATE 4, 4: PRINT "Hello!"


    Hello!


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


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


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage