Convert RGB value to hex???

Just starting out? Need help? Post your questions and find answers here.
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Convert RGB value to hex???

Post by Hydrate »

I have been trying for some time now to convert RGB to a 6 character hex value, however I am failing quite miserably. I seem to be getting closer but im not quite there. The following code shows what i have:

Code: Select all

Procedure RGBtoHex(r.l,g.l,b.l);
  rval.s="";
  gval.s="";
  bval.s="";
  If r=0;
    rval="00";
  Else;
    rval=Hex(r);
  EndIf;
  If g=0;
    gval="00";
  Else;
    gval=Hex(g);
  EndIf;
  If r=0;
    bval="00";
  Else;
    bval=Hex(b);
  EndIf;
  Debug rval;
  Debug gval;
  Debug bval;
  Debug rval+gval+bval;
EndProcedure;

RGBtoHex(29,259,12);
Does anyone know where im going wrong, or perhaps a btter way to do it???
.::Image::.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

nothing's wrong

hex(29)= $1d
hex(259) = $103 (greater than 255 ;))
hex(12) = $C

so :

Code: Select all

Procedure RGBtoHex(r.l,g.l,b.l);
  rval.s="";
  gval.s="";
  bval.s="";
  
  If r=0;
    rval="00";
  Else;
    rval=RSet(Hex(r),2,"0");
  EndIf;
  
  If g=0;
    gval="00";
  Else;
    gval=RSet(Hex(g),2,"0");
  EndIf;
  
  If r=0;
    bval="00";
  Else;
    bval=RSet(Hex(b),2,"0");
  EndIf;
  
  Debug rval;
  Debug gval;
  Debug bval;
  Debug rval+gval+bval;

EndProcedure;

RGBtoHex(29,255,12);
“Fear is a reaction. Courage is a decision.” - WC
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

If it's 6 digits you're after:

Code: Select all

Procedure.s byte2hex(num)
  Protected ret$="00", hex$=Hex(num)
  Select Len(hex$)
    Case 1
      ret$ = "0"+hex$
    Case 2
      ret$ = hex$
  EndSelect
  ProcedureReturn ret$
EndProcedure

Procedure.s RGBtoHex(r.l,g.l,b.l); 
  rval.s=byte2hex(r)
  gval.s=byte2hex(g)
  bval.s=byte2hex(b)
  ProcedureReturn rval+gval+bval; 
EndProcedure; 

Debug RGBtoHex(255,10,255); 
I may look like a mule, but I'm not a complete ass.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Simpler one:

Code: Select all

;Convert a 24bit colour value to a Hex string
Procedure.s RGBToHex(Colour.l)
	Protected HexString.s = "#"
	HexString + RSet(Hex(Red(Colour)), 2, "0")
	HexString + RSet(Hex(Green(Colour)), 2, "0")
	HexString + RSet(Hex(Blue(Colour)), 2, "0")
	ProcedureReturn HexString
EndProcedure

Debug RGBToHex(RGB(0, 0, 0))
Debug RGBToHex(RGB(32, 23, 30))
Debug RGBToHex(RGB(255, 255, 255))
Converted to fit your needs:

Code: Select all

Procedure.s RGBToHex(r.b, g.b, b.b)
	Protected HexString.s = "#"
	HexString + RSet(Hex(Red(RGB(r,g,b))), 2, "0")
	HexString + RSet(Hex(Green(RGB(r,g,b))), 2, "0")
	HexString + RSet(Hex(Blue(RGB(r,g,b))), 2, "0")
	ProcedureReturn HexString
EndProcedure

Debug RGBToHex(0, 0, 0)
Debug RGBToHex(32, 23, 30)
Debug RGBToHex(255, 255, 255)
BTW: Search is your friend :wink:
http://www.purebasic.fr/english/viewtopic.php?t=13584
--Kale

Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Nice one. :)
I may look like a mule, but I'm not a complete ass.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Here's another i found in another thread:

Code: Select all

Debug Hex(RGB(32, 23, 30))
That's a bit simpler!!! :shock:
--Kale

Image
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

Kale wrote:Here's another i found in another thread:

Code: Select all

Debug Hex(RGB(32, 23, 30))
That's a bit simpler!!! :shock:
I wish, lol, it doesnt seem to work like that. The following code shows why (i fixed it, thanks guys):

Code: Select all

Procedure.s BytetoHex( b.l );
  If b=0;
    ProcedureReturn "00";
  Else;
    ProcedureReturn RSet ( Hex( b ), 2 , "0" );
  EndIf;
EndProcedure;

Procedure.s RGBtoHex( r.l , g.l , b.l );
  ProcedureReturn "#"+RSet( BytetoHex( r )+BytetoHex( g )+BytetoHex( b ), 6 , "0" );;
EndProcedure;

Debug RGBtoHex( 36 , 104 , 160 );

Debug Hex(RGB( 36 , 104 , 160 ))
As you can see, my code returns:
#2468A0

Your code returns:
A06824

Notice the blue value is moved to the front?? Not only that, i would rather code it myself, its more fun, lol.
.::Image::.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Hydrate wrote:As you can see, my code returns:
#2468A0

Your code returns:
A06824
Not my code. :wink: but yes, i should of tested it first. :lol:
--Kale

Image
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Hydrate wrote:
Kale wrote:Here's another i found in another thread:

Code: Select all

Debug Hex(RGB(32, 23, 30))
That's a bit simpler!!! :shock:
I wish, lol, it doesnt seem to work like that. The following code shows why (i fixed it, thanks guys)
This should work!

Endian code from my "tips" post http://www.purebasic.fr/english/viewtopic.php?t=14524

Code: Select all

Procedure.l Endian(val.l)
!MOV Eax,dword[p.v_val]
!BSWAP Eax
ProcedureReturn
EndProcedure

Debug Hex(Endian(RGB(36, 104, 160))>>8)
;the shift 8 bits to the right is to get rid of the 00 at the end.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Code: Select all

Procedure.l Swap_RGB(val.l)
!MOV Eax,dword[p.v_val]
!BSWAP Eax
!SHR Eax,8
ProcedureReturn
EndProcedure

rgb=RGB(36, 104, 160)

Debug Hex(rgb)

bgr=Swap_RGB(rgb)

Debug Hex(bgr)

rgb=Swap_RGB(bgr)

Debug Hex(rgb)
PS! I'm not entirely if this is a "proper" swap,
thank intel for that confusion, storing numbers backwards :P
It should work for your use, but if you plan to convert BGR/RGB
I believe a quick search shoud bring up some posts about that,
including RGBA and BGRA.
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post by MikeB »

Seems to me the easiest is -

Code: Select all

Procedure.s col2hex(value)
	back$ = Hex(value)
	back$ = Right(back$,2)+Mid(back$,3,2)+Left(back$,2)
	ProcedureReturn back$
EndProcedure
or is that too easy?
Mike.
(I'm never going to catch up with the improvements to this program)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

That looks like it'll work well, Mike, the only problem is that choosing a string-based solution is going to slow it down exponentially. If you have a lot of conversions to do on the fly it'll be a real problem. Shifting the bits arithmetically, either in PB code or ASM, will yield a more practical result.
BERESHEIT
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post by PB&J Lover »

Let the computer do the work. What about this?

Code: Select all

Procedure.s RGB2Hex(R.l,G.l,B.l)
ProcedureReturn Hex(Red(RGB(R,G,B)))+Hex(Green(RGB(R,G,B)))+Hex(Blue(RGB(R,G,B)))
EndProcedure

Debug RGB2Hex(36,104,160)
OR

Code: Select all

Procedure.s RGB2Hex(RGB.l)
ProcedureReturn Hex(Red(RGB))+Hex(Green(RGB))+Hex(Blue(RGB))
EndProcedure

Debug RGB2Hex(RGB(36,104,160))
OR (if you want the leading "0")

Code: Select all

Procedure.s RGB2Hex(RGB.l)
ProcedureReturn RSet(Hex(Red(RGB)),2,"0")+RSet(Hex(Green(RGB)),2,"0")+RSet(Hex(Blue(RGB)),2,"0")
EndProcedure

Debug RGB2Hex(RGB(12,4,16))
It is true the output of RGB() does reverse the input. This is why this Hex(RGB(32, 23, 30)) doesn't work like you want. It is not Hex()'s fault, it is correctly translating the output of RGB().

Code: Select all

Debug RGB(36,104,160)

Debug Bin(RGB(36,104,160))

10512420

1010 0000 0110 1000 0010 0100
  A   0    6    8    2    4
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Post Reply