tmighty tmighty - 1 year ago 178 Question

Converting RGB to ARGB

I'm trying to emulate the Color.ToArgb() Net function in VB6.

For my attempts I have chosen the color Red.

In .NET I'm getting the result

Color.Red.ToArgb() = -65536

However, I'm not able to reproduce this in VB6.

My function RGBToARGB(vbRed, 255) returns -16776961

Public Function RGBToARGB(ByVal uColor As Long, ByVal uAlpha As Integer) As Long

Dim newColor As Long

' To add an Alpha channel to the RGB, simply
If uAlpha > 127 Then
newColor = uColor Or ((uAlpha - 128) * &H1000000 Or &H80000000)
newColor = uColor Or (uAlpha * &H1000000)
End If

RGBToARGB = newColor

End Function

Does anybody see what I'm doing wrong / different than the .NET version?

Answer Source

There might be a more optimal way to do this but this should work:

Function RGB24ToARGB(ByVal RGB24 As Long, ByVal Alpha As Byte) As Long
    RGB24ToARGB = CLng(Alpha And Not &H80) * &H1000000
    If Alpha And &H80 Then RGB24ToARGB = RGB24ToARGB Or &H80000000
               Or (RGB24 And &HFF&) * &H10000 _
               Or (RGB24 And &HFF00&) _
               Or (RGB24 And &HFF0000) \ &H10000
End Function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download