Convert Hex String to Color

Hey gang,

In the user preferences settings of one of my apps, the user can select a color for some of the displayed text. I am saving this color to disk by obtaining the string value of the color:

dim c as color = &cAABBCC00
Dim s as string = str(c)

Works fine. Now what’s the best way to read in that string value and convert it into a color? Presently I am doing this:


Dim c1 as string = DataArray(DataArray.Ubound)  // DataArray is all the lines of data read in from a binary stream  The color is always the last element in the array

Dim v as variant = c1

Dim c as color = v.ColorValue

Is that the best way to do this? If not, what is?

Jon

I’m using this :

[code]Function ToColor(extends ColorString As String) As Color
Dim v As Variant = ColorString
Dim c As Color = v.ColorValue
Return c

End Function
[/code]

must be something I’ve grabbed on this forum anyway …

Thanks. That’s basically what I am doing. But I’m also thinking in terms of the new framework where there are no variants. Just wondering how you do it then…

I’m not doing any new framework for now … sorry I can’t help more !
yes my solution is the same as your’s.

Maybe like this :

Function ToColor(Extends colorText As Text) As Color using xojo.core dim red_ as integer = Integer.FromHex(colorText.mid(4,2)) dim green_ as integer = Integer.FromHex(colorText.mid(6,2)) dim blue_ as integer = Integer.FromHex(colorText.mid(8,2)) dim alpha_ as integer = Integer.FromHex(colorText.mid(2,2)) 'RGBA does not seem to exist in desktop but RGB does the same return RGB(red_,green_,blue_,alpha_) End Function

I have no idea how to proceed the other way without Str… Seems there is nothing in the new framework to express a color in Text form. Or anything like color.red, color.green, and so on. Unless there are some undocumented secrets not mentioned in the LR, for the time being, this is so indigent only the classic framework can do.

Thanks, Michel. I figured you could do it with the RGB values like you did. Was hoping there was an “easier” method. I was hoping there was some sort of trick using the hex or color literals. But nope…

Using the variant works well and for now, that’s what I’m sticking with. Hopefully the new framework will continue to mature and end up with more capabilities.