Author Topic: RGB question(s)  (Read 4024 times)

0 Members and 1 Guest are viewing this topic.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
RGB question(s)
« 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?
Logic is the beginning of wisdom.

FellippeHeitor

  • Guest
Re: RGB question(s)
« Reply #1 on: July 06, 2021, 08:00:40 am »
Colors are 32bit color values, which also include alpha. If not passed, alpha is set at 255. Does your online tool deal with alpha?

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: RGB question(s)
« Reply #2 on: July 06, 2021, 09:07:02 am »
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?
Logic is the beginning of wisdom.

FellippeHeitor

  • Guest
Re: RGB question(s)
« Reply #3 on: July 06, 2021, 10:02:03 am »
Exactly. What you get with _RGB(255, 255, 255) in 32bit mode is actually _RGBA32(255, 255, 255, 255) - or _RGB32(255) for short.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: RGB question(s)
« Reply #4 on: July 06, 2021, 10:10:54 am »
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...
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: RGB question(s)
« Reply #5 on: July 06, 2021, 12:30:26 pm »
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)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: RGB question(s)
« Reply #6 on: July 06, 2021, 07:08:07 pm »
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
Logic is the beginning of wisdom.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: RGB question(s)
« Reply #7 on: July 06, 2021, 07:55:16 pm »
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)

Code: QB64: [Select]
  1. c = 4294967295
  2.  
  3. A~%% = _SHR(c, 24) 'alpha is simple, just dump all 24 color bits
  4. Tmp~& = _SHL(c, 8) 'dump the alpha bits
  5. R~%% = _SHR(Tmp~&, 24) 'move the value back for RED
  6. Tmp~& = _SHL(c, 16) 'dump alpha and red
  7. G~%% = _SHR(Tmp~&, 24) 'shove it back for GREEN
  8. Tmp~& = _SHL(c, 24) 'dump alpha, red, and green
  9. B~%% = _SHR(Tmp~&, 24) 'shove it back for BLUE
  10.  
  11. PRINT A~%%; R~%%; G~%%; B~%%
  12.  
  13.  
« Last Edit: July 06, 2021, 08:18:04 pm by Cobalt »
Granted after becoming radioactive I only have a half-life!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: RGB question(s)
« Reply #8 on: July 06, 2021, 08:43:15 pm »
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
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: RGB question(s)
« Reply #9 on: July 06, 2021, 08:59:00 pm »
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.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: RGB question(s)
« Reply #10 on: July 06, 2021, 10:58:37 pm »
Cool... Much appreciated....

J
Logic is the beginning of wisdom.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: RGB question(s)
« Reply #11 on: July 07, 2021, 12:40:36 am »
 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  ?

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: RGB question(s)
« Reply #12 on: July 07, 2021, 05:19:19 am »
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.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: RGB question(s)
« Reply #13 on: July 07, 2021, 06:01:51 am »
@NOVARSEG

As you pointed out BGRA

Referring to @SMcNeill MEM Tutorial #2


  [ You are not allowed to view this attachment ]