Author Topic: _CLEARCOLOR Verification  (Read 4919 times)

0 Members and 1 Guest are viewing this topic.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
_CLEARCOLOR Verification
« on: May 09, 2020, 03:17:46 pm »
The code below uses _CLEARCOLOR to set the transparent color of an image brought in. The image contains bright magenta (255, 0, 255) that I'm setting as the transparent color. This works fine as seen in the _PUTIMAGE line where only the red oval appears.

However...

When I try to use _CLEARCOLOR as a function later on to retrieve the transparent color I always get the value of zero. The Wiki entry on this command is very vague but the line that caught my attention is this:

"In 32-bit color modes, zero is returned."

Is zero the only value ever returned with 32bit color images? How in the world am I supposed to load an image in 32bit mode and determine the transparent color? How can I set the transparent color of a 32bit color image and then later on retrieve it? Am I missing something here?

Code: QB64: [Select]
  1. image& = _LOADIMAGE("redoval.png", 32)
  2. _CLEARCOLOR _RGB32(255, 0, 255), image&
  3. SCREEN _NEWIMAGE(640, 480, 32)
  4. CLS , _RGB32(127, 127, 127)
  5. _PUTIMAGE (10, 10), image&
  6. LOCATE 10,1
  7. PRINT _CLEARCOLOR(image&) ' always returns zero ??
« Last Edit: May 09, 2020, 03:20:16 pm by TerryRitchie »
In order to understand recursion, one must first understand recursion.

FellippeHeitor

  • Guest
Re: _CLEARCOLOR Verification
« Reply #1 on: May 09, 2020, 03:29:57 pm »
We can peek in the C code for this function to see what's being done there, but my assumption is that you should read the color value before clearing it.

Contrary to _SETALPHA, which attempts to only affect transparency, this one promises to actually clear the color you indicate, which results in 0 being returned (0 red, 0 green, 0 blue, 0 alpha).

It also makes sense to specify the return value as 0-only for 32bit images since 256-images assign one of the indexes to be treated as transparent (in gifs, for instance), in which case it'd make sense to return the "transparent" color.
« Last Edit: May 09, 2020, 03:34:24 pm by FellippeHeitor »

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #2 on: May 09, 2020, 03:38:58 pm »
Ah, so it doesn't just set the alpha value but sets all values to 0. Well then the return value of 0 makes sense. So an indexed image, like a 256 color GIF as you stated, would have the index value returned by _CLEARCOLOR. The Wiki could use some more clearly worded info on this and perhaps a few examples as well.

Thanks Fell.
In order to understand recursion, one must first understand recursion.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #3 on: May 09, 2020, 04:03:16 pm »
Really, it works for 256 colors images, not for 32 bit screens and also not for PNG images with transparent colors.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 256)
  2. CLS , 15
  3.  
  4. LINE (0, 0)-(639, 479), 14, BF
  5. LINE (200, 200)-(439, 279), 13, BF
  6. img = _LOADIMAGE("dog.png", 32)
  7.  


Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #4 on: May 09, 2020, 04:16:04 pm »
Thanks for the clarification Petr. I found the same thing when playing with it some more after Fell's reply. I wish there was something similar for 32bit but after thinking about Fell's reply for a while it makes sense. How could _CLEARCOLOR retrieve just one value in a 32bit image when there could be potentially hundreds of colors with varying degrees of transparency?

My original thinking was that if you set a transparent color with _CLEARCOLOR that later on you could use _CLEARCOLOR as a function to return that value set earlier. Nope.
In order to understand recursion, one must first understand recursion.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #5 on: May 10, 2020, 06:52:51 am »
Interisting aspect of 32 mode graphic

saying this
Quote
When I try to use _CLEARCOLOR as a function later on to retrieve the transparent color I always get the value of zero. The Wiki entry on this command is very vague but the line that caught my attention is this:

"In 32-bit color modes, zero is returned."
plus this one
Quote
this one promises to actually clear the color you indicate, which results in 0 being returned (0 red, 0 green, 0 blue, 0 alpha).
in other words:
so in this screen setting the only transparent color is black transparent  _RGB32(0,0,0,0), so _clearcolor sets the defined color to black trasparent color and for this reason the function _CLEARCOLOR returns always 0 in 32mode.
In this case we must remember to be specific when we use _SETALPHA
Quote
The _SETALPHA statement sets the alpha channel transparency level of some or all of the pixels of an image.
http://www.qb64.org/wiki/SETALPHA  because a generic _SETALPHA 100 works also on the cleared color changing its alpha value to semitransparent.

In a different way if we want only make transparent a color we can use the alpha channel setting to 0 for that color.
Thanks to talk about it, you have teached me another piece of knowledge .
Yep in graphic we must have one hundred opened eyes !
Programming isn't difficult, only it's  consuming time and coffee

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #6 on: May 10, 2020, 07:19:41 pm »
Playing around with _CLEARCOLOR some more while creating a pixel perfect collision demo for the tutorial I can verify that _CLEARCOLOR does exactly what it's name states. It literally clears all color from the selected 32bit color and sets it to (0, 0, 0, 0). It has nothing to do with transparency or alpha channel really (though it does set alpha to 0), just clear all color from selection.

Because of this I suggest only using _CLEARCOLOR for 256 color images. _SETALPHA is a much better option for 32bit color.
In order to understand recursion, one must first understand recursion.

FellippeHeitor

  • Guest
Re: _CLEARCOLOR Verification
« Reply #7 on: May 10, 2020, 07:38:48 pm »
The thing with _SETALPHA is that it won't set a certain color's alpha. It'll set the whole image's alpha. In that sense, _CLEARCOLOR is what's more proper.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #8 on: May 10, 2020, 11:00:43 pm »
The thing with _SETALPHA is that it won't set a certain color's alpha. It'll set the whole image's alpha. In that sense, _CLEARCOLOR is what's more proper.

Transparency and alpha channel use in QB64 has always been confusing to me. That's why I've always opted to just create PNG files with transparency using my graphic program and load them into QB64. Even creating the tutorials I find I need to play with transparency statements to get it right. It's probably just me, but color and transparency statements just don't have a natural feel to them. They don't feel intuitive even after using them over time. Again, more than likely just me.
In order to understand recursion, one must first understand recursion.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #9 on: May 11, 2020, 03:23:04 pm »
If I take the second example of _clearcolor wiki page

and change the statement for using _setalpha at the place of _clearcolor it seems to work the same.
original code
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(512, 384, 32) ' screen uses handle value
  2. CIRCLE (50, 50), 50, _RGB(128, 0, 0) ' create a red ball image
  3. PAINT (50, 50), _RGB(255, 0, 0), _RGB(128, 0, 0)
  4. redball = _NEWIMAGE(101, 101, 32) ' create a new image page
  5. _PUTIMAGE , 0, redball, (0, 0)-(101, 101) ' put screen page 0 image onto redball page
  6. _CLEARCOLOR, _RGB(0, 0, 0), redball ' makes black become see-through
  7. CLS , _RGB(0, 0, 255) ' erase original ball and create a blue background
  8.     _PUTIMAGE (RND * 512, RND * 384), redball
  9.     SLEEP 1 ' one second delay
  10.  

MOD _setalpha
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(512, 384, 32) ' screen uses handle value
  2. CIRCLE (50, 50), 50, _RGB(128, 0, 0) ' create a red ball image
  3. PAINT (50, 50), _RGB(255, 0, 0), _RGB(128, 0, 0)
  4. redball = _NEWIMAGE(101, 101, 32) ' create a new image page
  5. _PUTIMAGE , 0, redball, (0, 0)-(101, 101) ' put screen page 0 image onto redball page
  6. _SETALPHA 0, _RGB(0, 0, 0), redball ' makes black become see-through
  7. CLS , _RGB(0, 0, 255) ' erase original ball and create a blue background
  8.     _PUTIMAGE (RND * 512, RND * 384), redball
  9.     SLEEP 1 ' one second delay
  10.  
Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: _CLEARCOLOR Verification
« Reply #10 on: May 12, 2020, 07:40:29 pm »
@FellippeHeitor
Thanks for this one pot of gold!
https://www.qb64.org/forum/index.php?topic=2487.msg117932#msg117932
Accurate informations are very useful!
Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: _CLEARCOLOR Verification
« Reply #11 on: May 12, 2020, 09:33:25 pm »
❤️