How to convert Hex color to RGB color

Just starting out? Need help? Post your questions and find answers here.
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

How to convert Hex color to RGB color

Post 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
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: How to convert Hex color to RGB color

Post by STARGÅTE »

Code: Select all

; Syntax: $BBGGRR

Color = $0000FF  ; blue
; or
Color = Val("$0000FF") ; blue

Debug "$"+RSet(Hex(Color,#PB_Long),6,"0")
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to convert Hex color to RGB color

Post 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
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: How to convert Hex color to RGB color

Post 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
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to convert Hex color to RGB color

Post 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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: How to convert Hex color to RGB color

Post by rsts »

Got some code that shows the problem?

cheers
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: How to convert Hex color to RGB color

Post 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))
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: How to convert Hex color to RGB color

Post 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)
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: How to convert Hex color to RGB color

Post 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.
Baldrick
Addict
Addict
Posts: 860
Joined: Fri Jul 02, 2004 6:49 pm
Location: Australia

Re: How to convert Hex color to RGB color

Post 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.....
rudd68
User
User
Posts: 29
Joined: Wed Jun 09, 2010 2:55 pm

Re: How to convert Hex color to RGB color

Post by rudd68 »

OK, I haven't read exactly enough and misunderstood the problem.
moob
User
User
Posts: 68
Joined: Mon Aug 01, 2011 6:16 pm

Re: How to convert Hex color to RGB color

Post 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
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: How to convert Hex color to RGB color

Post by citystate »

good to hear, I'd encourage you to make sure you understand why it works, though :)
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
Post Reply