$FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

$FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Lebostein »

I don't undderstand that difference. RGBA should return a quad and not negative number?

Code: Select all

Debug $FF0000FF ; 4278190335
Debug RGBA(255, 0, 0, 255) ; -16776961
User_Russian
Addict
Addict
Posts: 1594
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by User_Russian »

Code: Select all

Color1.l=$FF0000FF
Color2.i=$FF0000FF
Debug Color1 ; -16776961
Debug Color2 ; 4278190335
Debug RGBA(255, 0, 0, 255) ; -16776961
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Lebostein »

https://www.purebasic.com/documentation ... /rgba.html
return should not be long or integer. The manual says quad. Should not be negative.
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by mestnyi »

The problem I think is in the return type. [.i .l]
He also tormented me a lot. :D
Oops I'm late it turns out
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Lebostein »

mestnyi wrote: Wed Oct 29, 2025 11:21 am The problem I think is in the return type. [.i .l]
He also tormented me a lot. :D
Oops I'm late it turns out
No:

Code: Select all

color.q = RGBA(255,0,0,255)
Debug color ; -16776961
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Lebostein »

It seems the manual is wrong? It seems the return of RGBA is a 32 long
Mesa
Enthusiast
Enthusiast
Posts: 451
Joined: Fri Feb 24, 2012 10:19 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Mesa »

Debug returns an integer
RGBA() returns a quad.

Code: Select all

Debug $F0000FF ; 4278190335 = 00000000FF0000FF => Because it IS AN INTEGER

Debug RGBA(255, 0, 0, 255) ; -16776961 = FFFFFFFFFF0000FF=> Because it IS A QUAD

Define rgb.q=RGBA(255, 0, 0, 255)
Debug rgb ;  -16776961
;ShowMemoryViewer(@rgb, 8)
Debug LSet(Hex(rgb, #PB_Quad), 16, "-"); FFFFFFFFFF0000FF

rgb = rgb  & $FFFFFFFF ; set high dw to 0
Debug rgb ;  4278190335 <<<=======                   
Debug LSet(Hex(rgb, #PB_Quad), 16, "-");FF0000FF--------
; ShowMemoryViewer(@rgb, 8)

Debug (RGBA(255, 0, 0, 255)  & $FFFFFFFF) ; -16776961 => something wrong with debug ?
M.
SMaag
Enthusiast
Enthusiast
Posts: 330
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by SMaag »

Code: Select all

Q= $FF0000FF ; 4278190335
col= RGBA(255, 0, 0, 255) ; -16776961

Debug Q
Debug col
Q is set directly as quad value -> it's positive

col is converted inside RGBA form a Long to a Quad! That's a difference!

Because PureBasic do not support unsinged long, the long is negative and this negative long is converted to Quad.
Converting a negative Long to Quad will result in a negative Quad.

That is correct and not a bug.
Mesa
Enthusiast
Enthusiast
Posts: 451
Joined: Fri Feb 24, 2012 10:19 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Mesa »

And remember how we make a negative number in binary:
https://en.wikipedia.org/wiki/Signed_nu ... sentations
Lebostein
Addict
Addict
Posts: 841
Joined: Fri Jun 11, 2004 7:07 am

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by Lebostein »

can anone explain that irritating first line in the manual?
Color.q = RGBA(Red, Green, Blue, Alpha)
Why ist it needed to store the 32-Bit result in a quad?

Code: Select all

Define cl.l
Define cq.q
For r = 0 To 255
  For g = 0 To 255
    For b = 0 To 255
      For a = 0 To 255
        cl = RGBA(r,g,b,a)
        cq = RGBA(r,g,b,a)
        If cl <> cq
          MessageRequester("Error", "Error")
          End
        EndIf
      Next
    Next
  Next
Next
SMaag
Enthusiast
Enthusiast
Posts: 330
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by SMaag »

Purebasic returns always a Quad as Color Value. That is a decision Fred made!

But you don't have to use a Quad. If you want, you can use a Long.
I always use Long for Color values.

If you know how a x86/x64 returns values you can see it makes no difference.

on 32Bit x86 the two Registers EAX/EDX are used to return a 64Bit Quad.
If you use a Quad-Color in your program, EAX and EDX is copied to your var.
If you use a Long, only EAX is copied to your var.

on x64 all values up to 64Bit are returned in RAX.

Now let's check what happens if we want to return a Color as Quad.
There is an ASM Command to convert an unsigned long to quad.
Because PB do not support unsigned long. PB always uses signed long to quad.
If you use a Long Color in the x64 program, only the EDX part will be copied to your var!

That's the reason!

Yes it seems crazy but it is correct!
Last edited by SMaag on Wed Oct 29, 2025 12:06 pm, edited 3 times in total.
pfnuesu
User
User
Posts: 11
Joined: Tue May 03, 2022 8:17 pm

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by pfnuesu »

Did you see the remarks in the manual?
Result varies from 0 to 4 294 967 295 shades. It is therefore advisable to use a 'quad', (Result.q) and set unused bytes to zero.
So as per the manual:

Code: Select all

Q       = $FF0000FF                      ; 4278190335 
color.q = RGBA(255,0,0,255) & $FFFFFFFF  ; 4278190335
col     = RGBA(255, 0, 0, 255)           ;  -16776961

Debug q
Debug color 
Debug col
User avatar
mk-soft
Always Here
Always Here
Posts: 6324
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: $FF0000FF <> RGBA(255, 0, 0, 255) .... why?

Post by mk-soft »

... 8)

Code: Select all

col.l = RGBA(255,0,0,255)
Debug col
Debug StrU(col, #PB_Long)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply