Version 2.0!
Features
Tutorials
Files
Glossary
Projects
Contact
Links
Message Board
Extras
LuckyCam
Old News
Sign Guestbook
View Guestbook
VB Horoscope
VB Photo Album
.
ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!

Converting Long Integers to Hexadecimal or RGB Values

Okay guys, this is it :) Plain and simple

The following code converts a Decimal Value to a Hexadecimal value, then takes the 3 RGB elements of the Hex number and converts them back into their 8-bit integers. It's not hard, 5 easy lines :)

Dim HexadecimalValue As String 'This will store the Hexadecimal Value as a string
Dim RedValue As Integer 'This will store the integer Red value (value of 0 to 255)
Dim GreenValue As Integer 'This will store the integer Greenvalue (value of 0 to 255)
Dim BlueValue As Integer 'This will store the integer Blue value (value of 0 to 255)

Public Sub Decimal_to_RGB (DecimalValue As Long)

    'This line will convert a Long decimal to Hexadecimal
    HexadecimalValue = Hex(Val(DecimalValue))
    If Len(Hexadecimal.Text) < 6 Then
      'Give is 6 digits for easy RGB conversion.
      HexadecimalValue = String(6 - Len(HexadecimalValue), "0") + HexadecimalValue
    End If

    'the following lines all work out the decimal value from the Hexadecimal Value the
    'hex number is broken up as such: &H RR GG BB
    'The first two characters correspond to the red value, if you read it as a String,
    'and add "&H" to the beginning of it, when CLng converts it from a string into a
    'number, it will see it as a hex number, not a decimal and thus, the red value is found
    RedValue = CLng("&H" + Mid(HexadecimalValue, 1, 2))
    'The second two characters correspond to the green value, characters 3 and 4.
    GreenValue = CLng("&H" + Mid(HexadecimalValue, 3, 2))
    'The next two characters correspond to the blue value, characters 5 and 6.
    BlueValue = CLng("&H" + Mid(HexadecimalValue, 5, 2))

End Sub

That's all there is to it.

Tutorial by Steven Blom

Special thanks to:
Chris, KoPP3x, Martin on Lucky's VB Gaming Message Board
(I remembered your discussion, but I didn't refer to it to write this.)