Page 1 of 1
Convert RGB value to hex???
Posted: Wed Nov 22, 2006 12:08 am
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???
Posted: Wed Nov 22, 2006 12:28 am
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);
Posted: Wed Nov 22, 2006 12:32 am
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);
Posted: Wed Nov 22, 2006 12:49 am
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
http://www.purebasic.fr/english/viewtopic.php?t=13584
Posted: Wed Nov 22, 2006 12:54 am
by srod
Nice one.

Posted: Wed Nov 22, 2006 12:56 am
by Kale
Here's another i found in another thread:
That's a bit simpler!!!

Posted: Wed Nov 22, 2006 1:04 am
by Hydrate
Kale wrote:Here's another i found in another thread:
That's a bit simpler!!!

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.
Posted: Wed Nov 22, 2006 1:15 am
by Kale
Hydrate wrote:As you can see, my code returns:
#2468A0
Your code returns:
A06824
Not my code.

but yes, i should of tested it first.

Posted: Wed Nov 22, 2006 6:33 am
by Rescator
Hydrate wrote:Kale wrote:Here's another i found in another thread:
That's a bit simpler!!!

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.
Posted: Wed Nov 22, 2006 6:59 am
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

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.
Posted: Fri Nov 24, 2006 11:40 am
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?
Posted: Fri Nov 24, 2006 12:11 pm
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.
Posted: Sun Nov 26, 2006 7:54 am
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