Color to hexadecimal literal


I have a color, say &cFB271100. Now I want to convert it into a hex value (Integer -> &h…). The pattern of the hex value is the following:

&hAARRGGBB ' Alpha, Red, Green, Blue

For my example color the conversion would look like this:


How can I do this with Xojo?

DIm c as color = &cFB271100
Dim s as String = Str©

Thanks Jim. This returns the hex value as String. I need the literal value (Integer), mean from “&h00FB2711” to &h00FB2711.

Dim c As Color = &cFB271100 Dim i As Integer = Integer(c)

Type mismatch error: Expected Integer, but got Color

Interesting that breaks in 64bit. Try:

Dim c As Color = &cFB271100 Dim i As Integer = Int32(c)

0xFB2711 (base 16) is equal to 16459537 (base 10) !

Uhh what do you need to use it for because we’ve given you both answers :wink:

My mistake, sorry Julian. Thanks to your help, I now have the value I need.

I need to come back again. I have the following declare, which implicit needs a color as hex literal Integer Value:

g.ForeColor = &cEA8906

Dim pen As Integer
Declare Function GdipCreatePen1 Lib “Gdiplus” (colour As Int32, width As Single, unit As GraphicsUnit, ByRef pen As Integer) As Status
s = GdipCreatePen1(Int32(g.ForeColor), g.PenWidth, GraphicsUnit.Pixel, pen)[/code]

Int32(g.ForeColor) doesn’t work. It needs to be &hFFEA8906 (Pattern: &hAARRGGBB). How do I get this to work?

An Xojo color value is RRGGBBAA and the alpha value looks like it is inverted (00=opaque in Xojo, but might be FF for GDI)

So do some bitwise math to strip the alpha off the end, divide by 256 (or rotR 8) then OR the alpha back on the front again

or for brute force

FUNCTION GDI_RGB(c as Color) As Integer
dim r as integer =
dim g as integer =
dim b as integer=
dim a as integer = 255 - c.alpha
dim s as string="&H"+right("00"+hex(a),2)+right("00"+hex(r),2)right("00"+hex(g),2)+right("00"+hex(b),2)
return val(s)

If you’re trying to get from a value without an alpha included then this will put the FF at the front (for a windows declare):

Dim i As Int32 = 15370502 '&hea8906 i = i + &hff000000 system.DebugLog(str(i)) '&hffea8906