gervais's picture

Color format for UInteger

I am trying to use the function Color3 or Color4 with a UInteger but so far all I am getting is either a red color or a shade of red or black
It work find if I use the byte value directly but I can’t use the UInteger value properly

Dim lAlpha As Long = pbAlpha
miColor = Convert.ToUInt32((lAlpha * &H1000000) + (pbRed * &H10000) + (pbGreen * &H100) + pbBlue)



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Does VB.Net even support unsigned integers? If yes, you should be able to do something similar to this:

' Not tested, see below for a better solution
Dim color As UInteger = DirectCast(pbAlpha << 24, UInteger) Or DirectCast(pbRed << 16, UInteger) Or _
    DirectCast(pbGreen << 8, UInteger) Or DirectCast(pbBlue, UInteger)

However, I'd suggest taking advantage of the System.Drawing.Color class to improve clarity:

Dim color As Integer = Color.FromArgb(pbAlpha, pbRed, pbGreen, pbBlue).ToArgb()
GL.Color4(Color.FromArgb(pbAlpha, pbRed, pbGreen, pbBlue))

Edit: it seems that they added shift operators to VB.Net! The code a lot cleaner this way.

gervais's picture

I try all the combination you did suggest and the Direct Case do not like the conversion in VB and the color return a signed integer with a value of -1 if I try my old code the returned value is 4294967295 which is in hex "FFFFFFFF" which is the correct value but if a I use color4 with a unsigned integer with the correct value all it do is return me a shade of red like it only see the red value????

The ARGB values are all set to 255

it react only to the Alpha value if i turn the alpha value down the red color also go down

the Fiddler's picture

In two's complement arithmetic, (int32) '-1' has the same bit-pattern as (uint32) '4294967295': 'FFFF FFFF'.

Question: what are you trying to achieve?

In most cases, you should prefer the Color4 overloads that take 4 floats, 4 bytes or 1 System.Drawing.Color.

gervais's picture

I am testing what are the posibility and what is the format to use for creating texture manualy so that i can creat user inteface using image data and basicaly i was to see if i could use the old way of formating the color valu i was using for direct X

if i use 4 byte value with this function it work fine but with the unsigned intege it is no go

the Fiddler's picture

Ok, I think I've found the issue. You are using a method that takes a single UInteger, right? If this is true, then be warned that this method expects an *array* of 4 integers, i.e.:

Dim rgba as UInteger = New UInteger() { &Hffffffff, &Hffffffff, &Hffffffff, &Hffffffff }

Not intuitive, but this is how this method is defined.

In other words, always use the Color4 overloads that take 4 distinct parameters, i.e.:

GL.Color4(&Hffffffff, &Hffffffff, &Hffffffff, &Hffffffff)