Page 1 of 1
How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 2:33 am
by moob
Hi...
How to convert Hex color to RGB color and RGB color to hex color???
when my aplication run read the hex color of one file and i need to pass this color for a button.
i try use "$" symbol since it is the same color but when i convert to a number this symbol disappears.
I can't add this symbol to a number?
thanks and sorry for english
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 2:40 am
by STARGÅTE
Code: Select all
; Syntax: $BBGGRR
Color = $0000FF ; blue
; or
Color = Val("$0000FF") ; blue
Debug "$"+RSet(Hex(Color,#PB_Long),6,"0")
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 2:54 am
by moob
STARGÅTE wrote:Code: Select all
; Syntax: $BBGGRR
Color = $0000FF ; blue
; or
Color = Val("$0000FF") ; blue
Debug "$"+RSet(Hex(Color,#PB_Long),6,"0")
Hi STARGÅTE thanks
but the result is a string in Debug, need to be a number with "$"
example:
PureCOLOR_SetButtonColor(#btnExibi, $000000, i need put here the Color passed from file in hex )
or convert the hex color to rgb and i don't know how.
or convert to a number but if i use the Val() function the "$" disappears
thanks
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 3:13 am
by citystate
convert the hex value using Val() and use that - it's still the same value, just the way of displaying it is different
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 4:08 am
by moob
citystate wrote:convert the hex value using Val() and use that - it's still the same value, just the way of displaying it is different
thanks citystate
i have use that, but not work.
the color read from bin file in hex is red and when pass to the button show black the result is 0.
the color in hex is $FF000
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 5:07 am
by rsts
Got some code that shows the problem?
cheers
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 8:06 am
by citystate
sounds like you might be having a problem with how you are working out the value of the hex... remember to add a dollar sign to the beginning if it is a string
Code: Select all
c_with_dollar.s="$FF0000"
c_without_dollar.s="FF0000"
debug "no $:"+str(val(c_without_dollar))
debug "yes $:"+str(val(c_with_dollar))
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 8:08 am
by Baldrick
This might help:
Code: Select all
mycolor$="$ABCDEF"
mycolor= Val(mycolor$)
Debug "my decimal number is : "+Str(mycolor)+" or as hex : "+Hex(mycolor)
Debug "we now bitshift the colors into their individual unsigned bytes as Red,Green,Blue"
Debug "display these bytes in hex"
myred.a=mycolor >>16
Debug Hex(myred)
mygreen.a=mycolor>>8
Debug Hex(mygreen)
myblue.a=mycolor
Debug Hex(myblue)
Debug "as decimal "
Debug myred
Debug mygreen
Debug myblue
Debug "to get back to correct number we now bitshift the bytes back to set the individual color bytes back into order"
mynewcolor=myred<<16+mygreen<<8+myblue
Debug "my decimal number is : "+Str(mynewcolor)+" which as hex is : "+Hex(mynewcolor)
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 3:10 pm
by rudd68
@Baldrick: This is wrong.
color = val("$ABCDEF") has the format: BBGGRR, but you use the format RRGGBB.
See here:
Code: Select all
mycolor$ = "$FFEEDD" ; Format: BBGGRR, RR(Red) = DD, GG(Green) = EE, BB(Blue) = FF
mycolor = Val(mycolor$)
Debug "my decimal number is : "+Str(mycolor)+" or as hex : "+Hex(mycolor)
Debug "we now bitshift the colors into their individual unsigned bytes as Red,Green,Blue"
Debug "display these bytes in hex"
;
myred.a = mycolor >>16
systemred.a = Red(mycolor)
Debug " "
Debug "Red: myred, systemred"
Debug Hex(myred)
Debug Hex(systemred)
;
mygreen.a=mycolor>>8
systemgreen.a = Green(mycolor)
Debug " "
Debug "Green; mygreen, systemgreen"
Debug Hex(mygreen)
Debug Hex(systemgreen)
;
myblue.a = mycolor
systemblue.a = Blue(mycolor)
Debug " "
Debug "Blue: myblue, systemblue"
Debug Hex(myblue)
Debug Hex(systemblue)
;
systemcolorfrommyredgreenblue = RGB(myred, mygreen, myblue)
systemcolorfromsystemredgreenblue = RGB(systemred, systemgreen, systemblue)
Debug " "
Debug "Color: systemcolorfrommyredgreenblue, systemcolorfromsystemredgreenblue"
Debug Hex(systemcolorfrommyredgreenblue)
Debug Hex(systemcolorfromsystemredgreenblue)
This is correct:
Code: Select all
Complete_Color_string$ = "$FFEEDD"
Complete_Color_value = Val(Complete_color_string$)
;
Red_Value.a = Red(Complete_color_value)
Green_Value.a = Green(Complete_color_value)
Blue_Value.a = Blue(Complete_Color_value)
;
Debug "Complete_Color, Red, Green, Blue"
Debug Hex(Complete_Color_value)
Debug Hex(Red_Value)
Debug Hex(Green_Value)
Debug Hex(Blue_Value)
;
New_Complete_Color_Value = RGB(Red_Value, Green_Value, Blue_Value)
Debug " "
Debug "New_Complete_Color"
Debug Hex(New_Complete_Color_Value)
If you must write a color_value you can use the Complete_Color_Value or the RGB() function.
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 4:20 pm
by Baldrick
rudd68 wrote:@Baldrick: This is wrong.
Technically yes & BGR syntax was already noted in a previous post by Stargate which did not satisfy moob.
If you read moob's last post where he has stated $FF0000 as being red then the code I posted will do exactly what he has requested!!
moob wrote:the color read from bin file in hex is red and when pass to the button show black the result is 0.
the color in hex is $FF000
which I translate to him saying he wants RRGGBB to match whatever it is he is doing, not BBGGRR....
Hence the post by rsts where he asks for sample code so it can be worked out without being all guesswork.....
Re: How to convert Hex color to RGB color
Posted: Sat Aug 06, 2011 4:35 pm
by rudd68
OK, I haven't read exactly enough and misunderstood the problem.
Re: How to convert Hex color to RGB color
Posted: Sun Aug 07, 2011 3:37 am
by moob
Thanks guys...
I have resolve the problem.
i use the example of netmaestro in other post and i read de value color from bin and i past directly for a button and works.
Code: Select all
If OpenFile(0, "myfile.bin")
seek_offset.s = "10EB13C"
seek_offset = RemoveString(seek_offset, "0x")
If Left(seek_offset, 1) <> "$"
seek_offset = "$"+seek_offset
EndIf
s_offset.i = Val(seek_offset)
datalength.s = "4"
datalength = RemoveString(datalength, "0x")
If Left(datalength, 1) <> "$"
datalength = "$"+datalength
EndIf
d_length.i = Val(datalength)
; change the actual cursor to the specified offset
FileSeek(0, s_offset)
; allocating memory to store the data read
*mem = AllocateMemory(d_length)
; reading the data
ReadData(0, *mem, d_length)
out$ = ""
For i=0 To d_length-1
out$ + Hex(PeekA(*mem+i)) + " "
Next
Value.l = PeekL(*mem)
PureCOLOR_SetButtonColor(#btnExibi, $000000, Value.l)
CloseFile(0)
EndIf
thanks to all
Re: How to convert Hex color to RGB color
Posted: Sun Aug 07, 2011 4:00 am
by citystate
good to hear, I'd encourage you to make sure you understand
why it works, though
