QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: TempodiBasic on September 27, 2019, 12:32:41 pm

Title: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: TempodiBasic on September 27, 2019, 12:32:41 pm
Hi guys

about color defined as CONST in a 32bit graphic program...

reading here https://www.qb64.org/forum/index.php?topic=1727.msg109661#msg109661 (https://www.qb64.org/forum/index.php?topic=1727.msg109661#msg109661) (the last call to the issue)
and here for a workaround solution while CONST 'll be fixed https://www.qb64.org/forum/index.php?topic=1335.msg106108#msg106108 (https://www.qb64.org/forum/index.php?topic=1335.msg106108#msg106108)

remarking that this solution has worked in other projects....
Quote
Turn

CONST x = 3: CONST y = x

into:

CONST x = 3
CONST y = x

And the glitch should go away, when it presents itself.

well now I run this code and got nothing... like Cobalt in the game of life!
here the code
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. CONST White = _RGBA(255, 255, 255, 255)
  3. CONST Black = _RGBA(0, 0, 0, 255)
  4. CONST Blu = _RGBA(0, 0, 200, 255)
  5. CONST Red = _RGBA(200, 0, 0, 255)
  6. CONST Green = _RGBA(0, 200, 0, 55)
  7.  
  8. textBase$ = "This is a try to show different" ' 30 chars
  9. LINE (2, 2)-(798, 598), Green, BF: SLEEP 1
  10. LINE (2, 2)-(798, 598), White, BF: SLEEP 1
  11. LINE (2, 2)-(798, 598), Blu, BF: SLEEP 1
  12. LINE (2, 2)-(798, 598), Red, BF: SLEEP 1
  13.  
  14.  
QB64ide is 1.3  from git 96937f0 version Stable

Well what does the hell happen?

Yes I fix it using _RGBA32 at the place of _RGBA  but...
Quote
_RGBA
The _RGBA function returns the closest palette index (legacy SCREEN modes) OR the 32-bit LONG color value (32-bit screens).

http://qb64.org/wiki/RGBA (http://qb64.org/wiki/RGBA) so is the bug  in CONST or in _RGBA ???
Just a feedback for developers and a warning for users.

Would it be better to cut away
Quote
OR the 32-bit LONG color value (32-bit screens).
and to limit _RGBA to legacy screen mode? _RGBA32 --> 32mode  &  _RGBA-->legacy QB mode from 0 to 13

Thanks to read
For a developer time is money.... falling on this kind of problem is great!
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: Petr on September 27, 2019, 12:51:30 pm
Hi. Rewrite 32 bit screen to 8 bit (,256) color screen without repairing CONSTs to _RGBA32, then it works correctly. _RGBA is for 8 bit screens (and for text mode). _RGBA32 is for 32 bit screens.
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: RhoSigma on September 27, 2019, 12:52:26 pm
If you wanna define 32bit colors, then you should always use the _RGB(A)32 functions.

The _RGB(A) functions doesn't define a new color, they just look for the color on your screen or its palette.

RULE:
define -> use _RGB(A)32
query -> use _RGB(A)
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: SMcNeill on September 27, 2019, 12:56:33 pm
You’re using SINGLE type variables and falling outside their range of precision, losing information.

CONST White~&& = ...

Just my guess, without actually running the code.


.....


Either that, or else you need a _DELAY after the SCREEN call so you can switch screen modes to a 32-bit screen, before the RGBA value is set.  It may be a race condition where you’re getting values before SCREEN has finished swapping screen modes.

Print the values and compare what the results are, to what you think they should be.
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: SMcNeill on September 27, 2019, 02:17:18 pm
You’re using SINGLE type variables and falling outside their range of precision, losing information.

CONST White~&& = ...

Just my guess, without actually running the code.


.....


Either that, or else you need a _DELAY after the SCREEN call so you can switch screen modes to a 32-bit screen, before the RGBA value is set.  It may be a race condition where you’re getting values before SCREEN has finished swapping screen modes.

Print the values and compare what the results are, to what you think they should be.

Actually, I was wrong with both my guesses at what the problem was, without first running the code...

The issue here is simply the fact that CONST has no idea what screen mode you're going to be using, and you never told it with the _RGBA command.

SCREEN 12
CONST Red = _RGB(255,0,0)

SCREEN _NEWIMAGE(640,480,32)
CONST Red = _RGB(255,0,0)

Now, since CONST are set in the precompile stage -- even before the translation of your code to C -- there's no way for any of them to know what SCREEN you're going to be using in your program since you can change it at any point in run time.  (See my latest post in the topic here for a brief demo, if needed, of swapping SCREENs inside a program: https://www.qb64.org/forum/index.php?topic=1741.msg109892#msg109892 )

So, since you're not telling the CONST command what screen mode you want the value for, it assumes you're working with a default screen -- SCREEN 0.

When using CONST to set values with _RGB and _RGBA, always use the extra parameter for the screen mode.

Give this little code a test run and see the difference in your color values:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. CONST White = _RGBA(255, 255, 255, 255)
  4. CONST Black = _RGBA(0, 0, 0, 255)
  5. CONST Blu = _RGBA(0, 0, 200, 255)
  6. CONST Red = _RGBA(200, 0, 0, 255)
  7. CONST Green = _RGBA(0, 200, 0, 55)
  8.  
  9. CONST White2 = _RGBA(255, 255, 255, 255, 32) 'See the ,32 at the end? That's to set a 32-bit color value
  10. CONST Black2 = _RGBA(0, 0, 0, 255, 32)
  11. CONST Blu2 = _RGBA(0, 0, 200, 255, 32)
  12. CONST Red2 = _RGBA(200, 0, 0, 255, 32)
  13. CONST Green2 = _RGBA(0, 200, 0, 55, 32)
  14.  
  15. PRINT White, White2
  16. PRINT Black, Black2
  17. PRINT Blu, Blu2
  18. PRINT Red, Red2
  19. PRINT Green, Green2

No real point to use _RGB or _RGBA for 32-bit color screens (unless you just want to for format's sake, or such), but you can also use the same syntax to designate CONST values for our other screen modes:

Code: QB64: [Select]
  1. CONST Red0 = _RGB(255, 0, 0, 0)
  2. CONST Red2 = _RGB(255, 0, 0, 2)
  3. CONST Red13 = _RGB(255, 0, 0, 13)
  4. CONST Red32 = _RGB(255, 0, 0, 32)
  5.  
  6. PRINT Red0
  7. PRINT Red2
  8. PRINT Red13
  9. PRINT Red32

Notice that we're getting the color value for red for:
SCREEN 0 -- 4
SCREEN 2 -- 0
SCREEN 12 -- 40
SCREEN 32 -- 4294901760

You need to set that last parameter to the screen mode which you want to use _RGB and _RGBA with, when using it inside a CONST.
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: TempodiBasic on September 27, 2019, 03:00:48 pm
Hi friends

thanks for all these good feedbacks!

This evening I have learned that
1.
screen _newimage 32 is not too clear/fast for CONST

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. CONST White = _RGBA(255, 255, 255, 255)

Quote
CONST are set in the precompile stage -- even before the translation of your code to C -- there's no way for any of them to know what SCREEN you're going to be using in your program
and so I must remark to _RGBA then the will to work in 32bit mode
and I have learnt that it is possible passing an extra parameter to the function _RGBA _RGB
Code: QB64: [Select]
  1. CONST White2 = _RGBA(255, 255, 255, 255, 32) 'See the ,32 at the end? That's to set a 32-bit color value

2.
the flexibility of _RGB and _RGBA in CONST's range using the parameter of the screen mode selected
Quote
You need to set that last parameter to the screen mode which you want to use _RGB and _RGBA with, when using it inside a CONST.

3.
but do why developers choose to build  this structure of Keywords with colors?
this is the reason
Quote
The _RGB(A) functions doesn't define a new color, they just look for the color on your screen or its palette.

RULE:
define -> use _RGB(A)32
query -> use _RGB(A)

This compendium of your affermations is just to be useful for me o for someother in the future.

@Petr thanks you're right and I agree that the issue arises in 32 bit mode.

Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: SMcNeill on September 27, 2019, 03:22:09 pm
Hi. Rewrite 32 bit screen to 8 bit (,256) color screen without repairing CONSTs to _RGBA32, then it works correctly. _RGBA is for 8 bit screens (and for text mode). _RGBA32 is for 32 bit screens.

@Petr thanks you're right and I agree that the issue arises in 32 bit mode.

Not quite.  As I was pointing out, the issue is that CONST doesn't know which screen mode you're planning on running your program in, so it assume SCREEN 0 as the default.  Here's an example of an 8-bit screen with the issue:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 256)
  2. CONST Red = _RGBA(255, 0, 0, 256)
  3.  
  4. COLOR Red
  5. PRINT Red
  6. COLOR _RGBA(255, 0, 0, 255)
  7. PRINT _RGBA(255, 0, 0, 255)
  8. PRINT "See the difference in the two color values above?"
  9. PRINT "The first is for a SCREEN 0 red value: "; Red
  10. PRINT "The second is for a SCREEN 256 red value: "; _RGBA(255, 0, 0, 255)
  11. CONST Red256 = _RGBA(255, 0, 0, 255, 256)
  12. COLOR Red256
  13. PRINT "If you remember that optional, last parameter, you get the correct color:"; Red256



Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: RhoSigma on September 27, 2019, 07:21:25 pm
Quote from: Steve
When using CONST to set values with _RGB and _RGBA, always use the extra parameter for the screen mode.

This seems unlogic to me, I mean obviously it works, but according to the Wiki for _RGB/_RGBA the last parameter is designated as image handle not as screen mode, so unless for the special case zero, which can be seen as mode as well as default handle, it shouldn't work that way.

However, I don't blame it is working that way for us. I've used the functions with image handles as well, so obviously both, handle and mode is valid for that last optional function argument. Probably just one more inconsistency in the Wiki.
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: SMcNeill on September 27, 2019, 07:55:53 pm
This seems unlogic to me, I mean obviously it works, but according to the Wiki for _RGB/_RGBA the last parameter is designated as image handle not as screen mode, so unless for the special case zero, which can be seen as mode as well as default handle, it shouldn't work that way.

However, I don't blame it is working that way for us. I've used the functions with image handles as well, so obviously both, handle and mode is valid for that last optional function argument. Probably just one more inconsistency in the Wiki.

You can’t load an image handle until runtime, and CONST works at precompile time.  The only way to get a valid color return with _RGB and _RGBA is to either:

1) Set some precompiler variable to change the default screen value we want them to work with.  (I actually preferred going this route — _SETDEFAULTSCREEN:32, as an example syntax.)

2) Pass the values to _RGB and _RGBA through the normally impossible-to-use image handle parameters.  The folks over at [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] preferred this method (“NO MORE KEYWORD BLOAT!!!”, I think was the emphatic cacophony of screams,) so it’s what I implemented when I added the commands into use with CONST.

Honestly, I would’ve preferred just setting a single value ONCE, rather than having to tack a ,screenvalue onto the end of EVERY CONST value that uses the RGB commands, but I went with what the vast majority of folks wanted at the time.  (And, if you want to know what really annoys me the most, just about all those folks who did nothing but bitch at any addition of a new keyword, or functionality, have completely moved on and abandoned the project.  The BLEEEPERS!)
Title: Re: Again about _RGBA...I have thougt to understand the lesson but it seems no. WHY?
Post by: TempodiBasic on September 28, 2019, 03:26:48 am
Thanks Steve
for your time and your energy that you share here

about setting CONST before SCREEN option into compiler is a feature of QB64 or it is so in some other languages?
From my point of view it is no a mistake but a characteristich that must be stressed a little more in the Wiki.

Thanks to read