Hex Color values reverse the Red and Blue

Just starting out? Need help? Post your questions and find answers here.
User avatar
susan
User
User
Posts: 17
Joined: Mon Aug 08, 2022 4:06 am

Hex Color values reverse the Red and Blue

Post by susan »

Just starting out, and not confident to post this in the Bug Reports.

When I use RGB(255, 0, 0) in a function that needs a color (like SetGadgetColor) on the screen, it is red as expected.

But when using a Hex color of $FF0000, blue is shown on the screen. (And $0000FF is red.)

I regularly use hex colour codes in paint programs and in local html files and have not seen this reversal before.

I can just use the RGB function, but the behaviour is unexpected, and I wonder if anyone has encountered it before. (And if I did use Hex codes, should I worry that users will not be seeing what I intended?)
AZJIO
Addict
Addict
Posts: 2238
Joined: Sun May 14, 2017 1:48 am

Re: Hex Color values reverse the Red and Blue

Post by AZJIO »

Inside your program, no one will see how you wrote the color code. You already know about this problem and always set the color correctly. If you read the color from an ini file, simply flip it when reading:

ColorValidate()

Code: Select all

Procedure RGBtoBGR(c)
; 	ProcedureReturn RGB(Blue(c), Green(c), Red(c))
	ProcedureReturn ((c & $00FF00) | ((c & $0000FF) << 16) | ((c & $FF0000) >> 16))
EndProcedure
infratec
Always Here
Always Here
Posts: 7691
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Hex Color values reverse the Red and Blue

Post by infratec »

The behaviour of RGB() or RGBA are never unexpected :!:

Your Problem is, thet PB uses (A)BGR internally and not RGB(A)

Code: Select all

Debug Hex(RGBA(1, 2, 3, 4), #PB_Long)
So if you using direct hex values the format is ABGR.
User avatar
susan
User
User
Posts: 17
Joined: Mon Aug 08, 2022 4:06 am

Re: Hex Color values reverse the Red and Blue

Post by susan »

infratec wrote: Sun Nov 09, 2025 10:58 am Your Problem is, thet PB uses (A)BGR internally and not RGB(A)
Thank you, I have also now found the Color Table topic in the Help, which confirms this.
submit the color value as whole value - but then the red and blue parts must be swapped, i.e. the color submitted in the BGR format.
I made a wrong assumption, and appreciate the answers.
mestnyi
Addict
Addict
Posts: 1109
Joined: Mon Nov 25, 2013 6:41 am

Re: Hex Color values reverse the Red and Blue

Post by mestnyi »

AZJIO wrote: Sun Nov 09, 2025 10:48 am Inside your program, no one will see how you wrote the color code. You already know about this problem and always set the color correctly. If you read the color from an ini file, simply flip it when reading:

ColorValidate()

Code: Select all

Procedure RGBtoBGR(c)
; 	ProcedureReturn RGB(Blue(c), Green(c), Red(c))
	ProcedureReturn ((c & $00FF00) | ((c & $0000FF) << 16) | ((c & $FF0000) >> 16))
EndProcedure
Yes, this is a solution, but why is it like this and not like all other programs, which have the same sequence? It would be more logical to use rgba in hex as well.
Joubarbe
Enthusiast
Enthusiast
Posts: 725
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Hex Color values reverse the Red and Blue

Post by Joubarbe »

Legacy stuff. Would cause a ton of problems if changed now.
User avatar
kenmo
Addict
Addict
Posts: 2065
Joined: Tue Dec 23, 2003 3:54 am

Re: Hex Color values reverse the Red and Blue

Post by kenmo »

When formatting a color for a human to read (HTML, CSS, image editors, some config files) they're usually written in intuitive RRGGBB order, often prefixed with "#"

But in memory and in low level programming, they're usually BBGGRR prefixed with 0x (or "$" in PB syntax)

I assume PB made this choice early to match OS APIs (at least Windows) and it's been that way ever since. (Internally! You can display it to the end user however you want)

For example: Windows defines Red as 0x0000FF
https://learn.microsoft.com/en-us/windo ... i/colorref
Post Reply