234 views

1. 3 months ago

### Martin T

Nov 10 Pre-Release Testers Germany
Edited 3 months ago

Hi,

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:

`&h00FB2711`

How can I do this with Xojo?

See post in context

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 = c.red
dim g as integer = c.green
dim b as integer= c.blue
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)
END FUNCTION```
2. ### Jim S

Nov 10 Central Florida

DIm c as color = &cFB271100
Dim s as String = Str(c)

3. ### Martin T

Nov 10 Pre-Release Testers Germany

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

Nov 10

Nov 10
6. ### Julian S

Nov 10 Pre-Release Testers, Xojo Pro UK

@MartinTrippensee I need the literal value (Integer)

```Dim c As Color = &cFB271100
Dim i As Integer = Integer(c)```
7. ### Martin T

Nov 10 Pre-Release Testers Germany
Edited 3 months ago

@Julian S `Dim i As Integer = Integer(c)`

Type mismatch error: Expected Integer, but got Color

8. ### Julian S

Nov 10 Pre-Release Testers, Xojo Pro UK

Interesting that breaks in 64bit. Try:

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

Nov 10
10. ### Jean-Yves P

Nov 10 Pre-Release Testers, Xojo Pro Europe (France, Besançon)
Edited 3 months ago

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

11. ### Julian S

Nov 10 Pre-Release Testers, Xojo Pro UK

Uhh what do you need to use it for because we've given you both answers ;)

12. ### Martin T

Nov 10 Pre-Release Testers Germany
Edited 3 months ago

Interesting that breaks in 64bit.

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

13. ### Martin T

Nov 11 Pre-Release Testers Germany
Edited 3 months ago

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)```

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

14. ### Dave S

Nov 11 Answer San Diego, California USA

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 = c.red
dim g as integer = c.green
dim b as integer= c.blue
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)
END FUNCTION```
15. ### Julian S

Nov 11 Pre-Release Testers, Xojo Pro UK
Edited 3 months ago

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```