by Eric O'Sullivan ()
Category: Graphics
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating:
(2 Votes)
This procedure takes a Long colour value and splits it into it's component Red, Green and Blue values.
Public Sub GetRGB(ByVal lngColour As Long, _
Optional ByRef intRed As Integer, _
Optional ByRef intGreen As Integer, _
Optional ByRef intBlue As Integer)
'Convert Long to RGB:
' Sys Cols, Blue , Green , Red
Const RED_MASK As Long = 255 'Binary: 00000000,00000000,00000000,11111111
Const GREEN_MASK As Long = 65280 'Binary: 00000000,00000000,11111111,00000000
Const BLUE_MASK As Long = 16711680 'Binary: 00000000,11111111,00000000,00000000
Const MAX_COLOUR As Long = 16777216 'Binary: 01111111,11111111,11111111,11111111
'if the lngcolour value is greater than acceptable
'then half the value
If (lngColour > MAX_COLOUR) Or _
(lngColour < -MAX_COLOUR) Then
'system colour range
Exit Sub
End If
'get the red, green and blue values
intRed = (lngColour And RED_MASK)
intGreen = (lngColour And GREEN_MASK) / 256 'shift 8 bits to the right
intBlue = (lngColour And BLUE_MASK) / 65536 'shift 16 bits to the right
End Sub