CLEARCOLOR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "CLEARCOLOR" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_CLEARCOLOR}}
{{DISPLAYTITLE:_CLEARCOLOR}}
The [[_CLEARCOLOR]] statement sets a specific color to be treated as transparent when an image is later put (via [[_PUTIMAGE]]) onto another image.  
The [[_CLEARCOLOR]] statement sets a specific color to be treated as transparent when an image is later put (via [[_PUTIMAGE]]) onto another image.


{|align="right"
|__TOC__
|}


{{PageSyntax}}
{{PageSyntax}}
:[[_CLEARCOLOR]] {{{Parameter|color&}}|_NONE}[, {{Parameter|Dest_Handle&}}]
:[[_CLEARCOLOR]] {{{Parameter|color&}}|_NONE}[, {{Parameter|Dest_Handle&}}]


{{Parameters}}
 
{{PageParameters}}
* In color modes using a palette, {{Parameter|color&}} is the palette index of the new transparent color value or _NONE designates no clear colors.
* In color modes using a palette, {{Parameter|color&}} is the palette index of the new transparent color value or _NONE designates no clear colors.
* If {{Parameter|color&}} is not a valid palette index, an [[ERROR Codes|illegal function call]] error will occur.
* If {{Parameter|color&}} is not a valid palette index, an [[ERROR Codes|illegal function call]] error will occur.
Line 26: Line 24:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' Using _CLEARCOLOR to "mask" the background color of an image.
''Example 1:'' Using _CLEARCOLOR to "mask" the background color of an image.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN (statement)|SCREEN}} 13
{{Cl|SCREEN}} {{Text|13|#F580B1}}
img& = {{Cl|_LOADIMAGE}}("qb64_trans.png")
img& = {{Cl|_LOADIMAGE}}({{Text|<nowiki>"qb64_trans.png"</nowiki>|#FFB100}})
{{Cl|_PUTIMAGE}} , img&, 0               'place actual image with background
{{Cl|_PUTIMAGE}} , img&, {{Text|0|#F580B1}} {{Text|<nowiki>'place actual image with background</nowiki>|#919191}}
K$ = INPUT$(1)
K$ = {{Cl|INPUT$}}({{Text|1|#F580B1}})
{{Cl|CLS}} , {{Cl|_RGB}}(255, 0, 0)             'clear screen with red background
{{Cl|CLS}} , {{Cl|_RGB}}({{Text|255|#F580B1}}, {{Text|0|#F580B1}}, {{Text|0|#F580B1}}) {{Text|<nowiki>'clear screen with red background</nowiki>|#919191}}
{{Cl|_CLEARCOLOR}} {{Cl|_RGB}}(255, 255, 255), img&
{{Cl|_CLEARCOLOR}} {{Cl|_RGB}}({{Text|255|#F580B1}}, {{Text|255|#F580B1}}, {{Text|255|#F580B1}}), img&
{{Cl|_PUTIMAGE}} , img&, 0               'place image without white background
{{Cl|_PUTIMAGE}} , img&, {{Text|0|#F580B1}} {{Text|<nowiki>'place image without white background</nowiki>|#919191}}
{{Cl|PRINT}} {{Cl|_CLEARCOLOR}}(img&)           'displays closest clear color attribute
{{Cl|PRINT}} {{Cl|_CLEARCOLOR (function)|_CLEARCOLOR}}(img&) {{Text|<nowiki>'displays closest clear color attribute</nowiki>|#919191}}
{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}


----


''Example 2:'' Using a _CLEARCOLOR transparency with images created on a [[_NEWIMAGE]] page. Does not require an image file.
''Example 2:'' Using a _CLEARCOLOR transparency with images created on a [[_NEWIMAGE]] page. Does not require an image file.
{{CodeStart}}{{Cl|SCREEN (statement)|SCREEN}} {{Cl|_NEWIMAGE}}(512, 384, 32) ' screen uses handle value
{{CodeStart}}
{{Cl|CIRCLE}}(50, 50), 50, {{Cl|_RGB}}(128, 0, 0) ' create a red ball image
{{Cl|SCREEN}} {{Cl|_NEWIMAGE}}({{Text|512|#F580B1}}, {{Text|384|#F580B1}}, {{Text|32|#F580B1}}) {{Text|<nowiki>' screen uses handle value</nowiki>|#919191}}
{{Cl|PAINT}}(50, 50), {{Cl|_RGB}}(255, 0, 0), {{Cl|_RGB}}(128, 0, 0)
{{Cl|CIRCLE}} ({{Text|50|#F580B1}}, {{Text|50|#F580B1}}), {{Text|50|#F580B1}}, {{Cl|_RGB}}({{Text|128|#F580B1}}, {{Text|0|#F580B1}}, {{Text|0|#F580B1}}) {{Text|<nowiki>' create a red ball image</nowiki>|#919191}}
redball = {{Cl|_NEWIMAGE}}(101, 101, 32) ' create a new image page
{{Cl|PAINT}} ({{Text|50|#F580B1}}, {{Text|50|#F580B1}}), {{Cl|_RGB}}({{Text|255|#F580B1}}, {{Text|0|#F580B1}}, {{Text|0|#F580B1}}), {{Cl|_RGB}}({{Text|128|#F580B1}}, {{Text|0|#F580B1}}, {{Text|0|#F580B1}})
{{Cl|_PUTIMAGE}} , 0, redball,(0, 0)-(101, 101) ' put screen page 0 image onto redball page
redball = {{Cl|_NEWIMAGE}}({{Text|101|#F580B1}}, {{Text|101|#F580B1}}, {{Text|32|#F580B1}}) {{Text|<nowiki>' create a new image page</nowiki>|#919191}}
{{Cl|_CLEARCOLOR}} {{Cl|_RGB}}(0, 0, 0), redball ' makes black become see-through
{{Cl|_PUTIMAGE}} , {{Text|0|#F580B1}}, redball, ({{Text|0|#F580B1}}, {{Text|0|#F580B1}})-({{Text|101|#F580B1}}, {{Text|101|#F580B1}}) {{Text|<nowiki>' put screen page 0 image onto redball page</nowiki>|#919191}}
{{Cl|CLS}}, {{Cl|_RGB}}(0, 0, 255) ' erase original ball and create a blue background
{{Cl|_CLEARCOLOR}} {{Cl|_RGB}}({{Text|0|#F580B1}}, {{Text|0|#F580B1}}, {{Text|0|#F580B1}}), redball {{Text|<nowiki>' makes black become see-through</nowiki>|#919191}}
{{Cl|DO...LOOP|DO}}
{{Cl|CLS}} , {{Cl|_RGB}}({{Text|0|#F580B1}}, {{Text|0|#F580B1}}, {{Text|255|#F580B1}}) {{Text|<nowiki>' erase original ball and create a blue background</nowiki>|#919191}}
{{Cl|_PUTIMAGE| _PUTIMAGE}} ({{Cl|RND}} * 512, {{Cl|RND}} * 384), redball
{{Cl|DO}}
{{Cl|SLEEP| SLEEP}} 1 ' one second delay
    {{Cl|_PUTIMAGE}} ({{Cl|RND}} * {{Text|512|#F580B1}}, {{Cl|RND}} * {{Text|384|#F580B1}}), redball
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> "" '' ''
    {{Cl|SLEEP}} {{Text|1|#F580B1}} {{Text|<nowiki>' one second delay</nowiki>|#919191}}
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|INKEY$}} <> {{Text|<nowiki>""</nowiki>|#FFB100}}
{{CodeEnd}}
{{CodeEnd}}


----


''Example 3:'' Fading an image with a _CLEARCOLOR background using a new page image to prevent [[_SETALPHA]] changes.
''Example 3:'' Fading an image with a _CLEARCOLOR background using a new page image to prevent [[_SETALPHA]] changes.
{{CodeStart}} '' ''
{{CodeStart}}
mainscreen = {{Cl|_NEWIMAGE}}(640, 480, 32) ' Main Screen (viewable)
mainscreen = {{Cl|_NEWIMAGE}}({{Text|640|#F580B1}}, {{Text|480|#F580B1}}, {{Text|32|#F580B1}}) {{Text|<nowiki>' Main Screen (viewable)</nowiki>|#919191}}
{{Cl|SCREEN}} mainscreen
{{Cl|SCREEN}} mainscreen
{{Cl|_SCREENMOVE}} {{Cl|_SCREENMOVE|_MIDDLE}}
{{Cl|_SCREENMOVE}} {{Cl|_MIDDLE}}
Image1& = {{Cl|_LOADIMAGE}}("qb64_trans.png") '<<<<<< any image with one background color to clear
Image1& = {{Cl|_LOADIMAGE}}({{Text|<nowiki>"qb64_trans.png"</nowiki>|#FFB100}}) {{Text|<nowiki>'<<<<<< any image with one background color to clear</nowiki>|#919191}}


{{Cl|IF...THEN|IF}} Image1& < -1 {{Cl|THEN}} 'check loaded image handle value before using!
{{Cl|IF}} Image1& < {{Text|-1|#F580B1}} {{Cl|THEN}} {{Text|<nowiki>'check loaded image handle value before using!</nowiki>|#919191}}
  {{Cl|_SOURCE}} Image1&
    {{Cl|_SOURCE}} Image1&
  clr~& = {{Cl|POINT}}(0, 0) 'get background color from image source
    clr~& = {{Cl|POINT}}({{Text|0|#F580B1}}, {{Text|0|#F580B1}}) {{Text|<nowiki>'get background color from image source</nowiki>|#919191}}
  {{Cl|_CLEARCOLOR}} clr~&, Image1& 'clear background color of loaded image
    {{Cl|_CLEARCOLOR}} clr~&, Image1& {{Text|<nowiki>'clear background color of loaded image</nowiki>|#919191}}
  NewImage1& = {{Cl|_NEWIMAGE}}({{Cl|_WIDTH (function)|_WIDTH}}(Image1&), {{Cl|_HEIGHT}}(Image1&), 32) 'new image page
    NewImage1& = {{Cl|_NEWIMAGE}}({{Cl|_WIDTH (function)|_WIDTH}}(Image1&), {{Cl|_HEIGHT}}(Image1&), {{Text|32|#F580B1}}) {{Text|<nowiki>'new image page</nowiki>|#919191}}
  {{Cl|_PUTIMAGE}} , Image1&, NewImage1& 'put image without background color on new page
    {{Cl|_PUTIMAGE}} , Image1&, NewImage1& {{Text|<nowiki>'put image without background color on new page</nowiki>|#919191}}
  {{Cl|_FREEIMAGE}} Image1& 'free loaded image from memory
    {{Cl|_FREEIMAGE}} Image1& {{Text|<nowiki>'free loaded image from memory</nowiki>|#919191}}
{{Cl|END IF}}
{{Cl|END IF}}
{{Cl|_DEST}} mainscreen:
{{Cl|_DEST}} mainscreen:


a& = 0: d = 1
a& = {{Text|0|#F580B1}}: d = {{Text|1|#F580B1}}
DO
{{Cl|DO}}
  {{Cl|_LIMIT}} 10 'regulate speed of fades
    {{Cl|_LIMIT}} {{Text|10|#F580B1}} {{Text|<nowiki>'regulate speed of fades</nowiki>|#919191}}
  {{Cl|CLS}}
    {{Cl|CLS}}
  a& = a& + d
    a& = a& + d
  {{Cl|IF...THEN|IF}} a& = 255 {{Cl|THEN}} d = -d 'reverse fade  
    {{Cl|IF}} a& = {{Text|255|#F580B1}} {{Cl|THEN}} d = -d {{Text|<nowiki>'reverse fade</nowiki>|#919191}}
  {{Cl|_SETALPHA}} a&, , NewImage1& 'sets alpha level of all colors to fade image page in/out
    {{Cl|_SETALPHA}} a&, , NewImage1& {{Text|<nowiki>'sets alpha level of all colors to fade image page in/out</nowiki>|#919191}}
  {{Cl|_PUTIMAGE}} (0, 342), NewImage1&  
    {{Cl|_PUTIMAGE}} ({{Text|0|#F580B1}}, {{Text|342|#F580B1}}), NewImage1&
  {{Cl|LOCATE}} 1, 1: {{Cl|PRINT}} "Alpha: "; a&
    {{Cl|LOCATE}} {{Text|1|#F580B1}}, {{Text|1|#F580B1}}: {{Cl|PRINT}} {{Text|<nowiki>"Alpha: "</nowiki>|#FFB100}}; a&
  {{Cl|_DISPLAY}}
    {{Cl|_DISPLAY}}
{{Cl|LOOP}} {{Cl|UNTIL}} a& = 0
{{Cl|DO...LOOP|LOOP UNTIL}} a& = {{Text|0|#F580B1}}
{{Cl|END}}
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
:''Note:'' If the _CLEARCOLOR image background was not put onto a separate page, [[_SETALPHA]] would display it also.
:''Note:'' If the _CLEARCOLOR image background was not put onto a separate page, [[_SETALPHA]] would display it also.




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[_CLEARCOLOR (function)]]
* [[_CLEARCOLOR (function)]]
* [[_SETALPHA]] {{text|(sets transparency level)}}
* [[_SETALPHA]] {{Text|(sets transparency level)}}
* [[_ALPHA]], [[_ALPHA32]] {{text|(read functions)}}
* [[_ALPHA]], [[_ALPHA32]] {{Text|(read functions)}}
* [[Images]], [[Creating Sprite Masks]]
* [[Images]], [[Creating Sprite Masks]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 14:05, 20 March 2023

The _CLEARCOLOR statement sets a specific color to be treated as transparent when an image is later put (via _PUTIMAGE) onto another image.


Syntax

_CLEARCOLOR {color&|_NONE}[, Dest_Handle&]


Parameters

  • In color modes using a palette, color& is the palette index of the new transparent color value or _NONE designates no clear colors.
  • If color& is not a valid palette index, an illegal function call error will occur.
  • In 32-bit color modes, color& is the _LONG color value of the new transparent color.
  • If Dest_Handle& is omitted, the destination is assumed to be the current write page. Zero can designate the current program screen.


Description

  • If Dest_Handle& is an invalid handle, then an invalid handle error is returned. Check for bad handle values of -1 first.
  • In 32-bit color modes, it simply sets the Alpha to 0 for all pixels matching the specified color.
  • In the second syntax, transparency is disabled for color modes using a palette.
  • Note: _SETALPHA can affect any _CLEARCOLOR alpha setting within the color range set.
  • NOTE: 32 bit _NEWIMAGE screen page backgrounds are transparent black or _ALPHA 0. Use _DONTBLEND or CLS for opaque.


Examples

Example 1: Using _CLEARCOLOR to "mask" the background color of an image.

SCREEN 13
img& = _LOADIMAGE("qb64_trans.png")
_PUTIMAGE , img&, 0 'place actual image with background
K$ = INPUT$(1)
CLS , _RGB(255, 0, 0) 'clear screen with red background
_CLEARCOLOR _RGB(255, 255, 255), img&
_PUTIMAGE , img&, 0 'place image without white background
PRINT _CLEARCOLOR(img&) 'displays closest clear color attribute
END

Example 2: Using a _CLEARCOLOR transparency with images created on a _NEWIMAGE page. Does not require an image file.

SCREEN _NEWIMAGE(512, 384, 32) ' screen uses handle value
CIRCLE (50, 50), 50, _RGB(128, 0, 0) ' create a red ball image
PAINT (50, 50), _RGB(255, 0, 0), _RGB(128, 0, 0)
redball = _NEWIMAGE(101, 101, 32) ' create a new image page
_PUTIMAGE , 0, redball, (0, 0)-(101, 101) ' put screen page 0 image onto redball page
_CLEARCOLOR _RGB(0, 0, 0), redball ' makes black become see-through
CLS , _RGB(0, 0, 255) ' erase original ball and create a blue background
DO
    _PUTIMAGE (RND * 512, RND * 384), redball
    SLEEP 1 ' one second delay
LOOP UNTIL INKEY$ <> ""

Example 3: Fading an image with a _CLEARCOLOR background using a new page image to prevent _SETALPHA changes.

mainscreen = _NEWIMAGE(640, 480, 32) ' Main Screen (viewable)
SCREEN mainscreen
_SCREENMOVE _MIDDLE
Image1& = _LOADIMAGE("qb64_trans.png") '<<<<<< any image with one background color to clear

IF Image1& < -1 THEN 'check loaded image handle value before using!
    _SOURCE Image1&
    clr~& = POINT(0, 0) 'get background color from image source
    _CLEARCOLOR clr~&, Image1& 'clear background color of loaded image
    NewImage1& = _NEWIMAGE(_WIDTH(Image1&), _HEIGHT(Image1&), 32) 'new image page
    _PUTIMAGE , Image1&, NewImage1& 'put image without background color on new page
    _FREEIMAGE Image1& 'free loaded image from memory
END IF
_DEST mainscreen:

a& = 0: d = 1
DO
    _LIMIT 10 'regulate speed of fades
    CLS
    a& = a& + d
    IF a& = 255 THEN d = -d 'reverse fade
    _SETALPHA a&, , NewImage1& 'sets alpha level of all colors to fade image page in/out
    _PUTIMAGE (0, 342), NewImage1&
    LOCATE 1, 1: PRINT "Alpha: "; a&
    _DISPLAY
LOOP UNTIL a& = 0
END
Note: If the _CLEARCOLOR image background was not put onto a separate page, _SETALPHA would display it also.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link