RGB to Hex?

Hi,
does by chance anyone have a method which will convert Hex to RGB?

If I type in A9 BC 4D - I would like the equivalent RGB code displayed.

I have found the following very simple little javascript function (7 lines of code) - which does exactly that - but I am not advanced enough to convert it into Xojo:
Function links

cut the hex characters. Use val("&h"+s) to convert them to integers. Than you can display them.

so val("&hFF") returns 255.

Christian,

I have textfield 1 (which contains 2 characters A9)
I have textfield 2 (which contains 2 characters BC)
I have textfield 3 (which contains 2 characters 4D)

Dim RGBResult as String = ???

you can take the text property of each, put a “&h” in front, pass it to val() function and get the number back. Than you can either use RGB() to get a color or display the decimal numbers somewhere.

Christian, do you mean something like this:

[code]Dim RResult as variant = “&h” + Val(textfield1.text)
Dim GResult as variant = “&h” + Val(textfield2.text)
Dim BResult as variant = “&h” + Val(textfield3.text)

Dim RGBResult as Variant = RResult + GResult + BResult[/code]

Just noticed the mistake in my code above :frowning:

I think my code below should now be correct???

[code]Dim R As String = “&h” + textfield1.text
Dim G As String = “&h” + textfield2.text
Dim B As String = “&h” + textfield3.text

Dim RResult As Integer = Val®
Dim GResult As Integer = Val(G)
Dim BResult As Integer = Val(B)

Dim RGBResult As String = str(RResult) + “,” + str(GResult) + “,” + str(BResult)[/code]

Looks right to me.

yes, great progress!

Wow - I am definitely starting to comprehend things - that makes a change :slight_smile:
Thank you!

Two final questions:

  1. Does the &h simply declare that the proceeding values are hexadecimal, resulting in the Val function taking the string as hexadecimal and then converting it to the integer equivalent?

  2. Is it a relatively simple process to do the reverse - take 3 separate RGB text fields (255) (0) (0) and convert each of them to hexadecimal?
    A pointer would be appreciated, so that I can then try and work out how to do that.

Thank you.

Yes. Also &b for binary and, IIR, &o for octal.

See the Hex function to go the other way.

dim s as string = Hex( 255 ) // "FF"

and &b for binary

Thanks - I will look in to that in half an hour or so, and then post back my code - to see if it looks correct :slight_smile:

Thanks.

So, if:
textfield1 has a value of 255
textfield2 has a value of 0
textfield3 has a value of 149

My code is as simple as:

[code]Dim R As String = Hex(textfield1.text)
Dim G As String = Hex(textfield2.text)
Dim B As String = Hex(textfield3.text)

Dim HexResult As String = R + G + B[/code]

No, you have to convert to a number via Val first.

Dim R As String = Hex(Val(textfield1.text))
Dim G As String = Hex(Val(textfield2.text))
Dim B As String = Hex(Val(textfield3.text))

Dim HexResult As String = R + G + B

This goes the other way, of course, from decimal to hex.

[code]Sub Action()
Dim c1 as Color
Dim c2 As Color

Dim b as Boolean
c1=CMY(.35,.9,.6) //choose the default color shown in color picker
b=SelectColor(c1,“Select a Color”)

If b Then
RoundRectangle1.FillColor=c1
Label1.Text=Str(c1)

RoundRectangle2.FillColor=RGB(c1.Red,c1.Green,c1.Blue)
Label2.Text="RGB(" + Str(c1.Red)+"," + Str(c1.Green)+ "," + Str(c1.Blue) + ")"

End If
End Sub
[/code]

Thank you both !

Kem, you wrote:

[quote]Dim R As String = Hex(Val(textfield1.text))
Dim G As String = Hex(Val(textfield2.text))
Dim B As String = Hex(Val(textfield3.text))

Dim HexResult As String = R + G + B

This goes the other way, of course, from decimal to hex.[/quote]

What did you mean by “goes the other way”?
Are you saying that although my code below is correct - there is a more compact way of converting Hex to RGB?

[code]Dim R As String = “&h” + textfield1.text
Dim G As String = “&h” + textfield2.text
Dim B As String = “&h” + textfield3.text

Dim RResult As Integer = Val®
Dim GResult As Integer = Val(G)
Dim BResult As Integer = Val(B)

Dim RGBResult As String = str(RResult) + “,” + str(GResult) + “,” + str(BResult)[/code]

No, I read too quickly. Disregard.

There are various ways of converting back and forth. Like defining a color and the set its .red, .green, blue integer values.

Or concatenate your textfields to one Colorstring and then use this string to define a color, like in this function:

Function imStringToColor(ColorString As String) As Color Dim v As Variant = ColorString Dim c As Color = v.ColorValue Return c End Function

I use Str(color) to save Color definitions as strings into a database and then retrieve it to colors with the above function.

Whew - thought I was going mad for a moment :slight_smile:

My very last query regarding colour conversions:
Is it (within my grasp) to compare 2 different text fields for the same colour value?

Example:
text field 1 has the b[/b] value of A9
textfield 4 has the b[/b] value of 255

Does this look correct?

[code]Dim H1 As String = “&h” + textfield1.text
Dim H1Result As Integer = Val(H1)

Dim RResult As Integer = Val(textfield4.text)

If H1Result <> RResult then
MsgBox(“Does not Match!”)
End if[/code]

Thanks.