QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: johnno56 on July 06, 2021, 07:23:16 am
-
Pure white = rgb(255,255,255)
Online RGB to Decimal converter = 16777215
Using a 32 bit screen (Screen _newscreen(800,600,32)) _rgb(255,255,255) = 4294967295
(_rgb32) gives the same value)
Can someone explain why QB64's _RGB() command gives a different value?
-
Colors are 32bit color values, which also include alpha. If not passed, alpha is set at 255. Does your online tool deal with alpha?
-
No. It is a simple tool. Input r, g and b values and it spits out a decimal result.
Are you implying that QB64's rgb values are inclusive of the alpha channel?
-
Exactly. What you get with _RGB(255, 255, 255) in 32bit mode is actually _RGBA32(255, 255, 255, 255) - or _RGB32(255) for short.
-
Reason for asking. I was tinkering with a basic routine to convert Decimal to RGB. The traditional calculation does not cater for the alpha channel. Looks like it is "back to the old drawing board". Thank you for the assist. I might try tinkering in another direction...
-
Reason for asking. I was tinkering with a basic routine to convert Decimal to RGB. The traditional calculation does not cater for the alpha channel. Looks like it is "back to the old drawing board". Thank you for the assist. I might try tinkering in another direction...
You don’t need a routine…. Just use QB64’s in-built commands.
DIM c AS _UNSIGNED LONG
c = 4294967295
PRINT _RED32(c), _GREEN32(c), _BLUE32(c)
-
Steve,
You are correct. QB64 has a method of conversion. That is too easy and I do not learn anything except for 'easy'. I am trying to find out if "I" can do it. It is not my intent to seem ungrateful, I do appreciate the help and advice, but I need to keep what functional 'gray matter' I have, active...
QB64 will give me the results that I seek, but, it does not show me 'how' it came to those results... Knowing is half the fun of coding...
But, in case I fail, I will still have your example... Thank you for responding so quickly and thank you for the example.
J
-
so to calculate our RGB32 color code the formula is
RGB32Color = A*16777216 + R*65536 + G*256 + B
where ,unless using RGBA, A is assumed 255
Another easy way to look at it too is to look at the HEX value, using Steves value for example
c = 4294967295
which gives us the HEX value
&HFFFFFFFF
now FF is 255 and each pair is a BYTE so we have 4 BTYEs or a 32bit color, in this case the color 255,255,255,255 or (opaque) bright white.
you can pull the RGB out with BIT Shifting if you wanted to do it yourself.(for some strange reason)
c = 4294967295
A~%%
= _SHR(c
, 24) 'alpha is simple, just dump all 24 color bitsTmp~&
= _SHL(c
, 8) 'dump the alpha bitsR~%%
= _SHR(Tmp~&
, 24) 'move the value back for REDTmp~&
= _SHL(c
, 16) 'dump alpha and redG~%%
= _SHR(Tmp~&
, 24) 'shove it back for GREENTmp~&
= _SHL(c
, 24) 'dump alpha, red, and greenB~%%
= _SHR(Tmp~&
, 24) 'shove it back for BLUE
PRINT A~%%; R~%%; G~%%; B~%%
-
Cobalt,
Thank you for the explanation. Much appreciated.
"for some strange reason"... For me, anyway, the reason is not strange at all. I am here to learn. As I said earlier, "knowing is half the fun of coding"... I may very well take the "easy" path, but at least, I will know how it is done, especially if I am using a Basic language that does not using the same commands as QB64.
Thank you.
J
-
LET c = Desired color
DIM m AS _MEM: m = _MEM(c)
Alpha = _MEMGET(m, m.OFFSET, _UNSIGNED _BYTE)
Red = _MEMGET(m, m.OFFSET + 1, _UNSIGNED _BYTE)
Green = _MEMGET(m, m.OFFSET + 2, _UNSIGNED _BYTE)
Blue = _MEMGET(m, m.OFFSET + 3, _UNSIGNED _BYTE)
And ^that is basically how QB64 does it with fetching values. Colors are 4 bytes, from alpha, red, green, blue in memory. (32-bit colors, that is.)
-
Cool... Much appreciated....
J
-
Intel x86 and AMD64 / x86-64 series of processors use the little-endian format where the most significant byte of a number is stored at higher memory address.
-->higher memory
BGRA ?
-
Steve may recall (from some years ago) that I was also flummoxed by RGB being greater than 16777215 - never forget alpha!
The fact that it must be so is in the clue 32-bit: 2^32 = 4,294,967,296
Each colour in RGB has 256 values: 1 byte (8 bits)
To get to 32 bits, four lots of 256 are required: 2^32 = 256^4. So there is room for alpha as well.
Colour without alpha is three lots of 256 which is 24-bit.
-
@NOVARSEG
As you pointed out BGRA
Referring to @SMcNeill MEM Tutorial #2
[ This attachment cannot be displayed inline in 'Print Page' view ]