Please get your mouse pointer off my face. Thanks

My Unused Social Networks

Facebook Twitter DA_buttonBW2 Google+

The Personal Web Site of Ben Margolis

 Information Architect, Database Engineer, and CGI Illustrator

Free VB Code

Be sure to read my disclaimer and copyright notice regarding free VB code.

Function DeRGB()

The Visual Basic RGB() function returns long integer “Windows Color Code” when you input a set of three decimal RGB values. This enables you to set an control’s Color Property to an RGB value, as in:

MyObject.Color = RGB(255, 0, 0)

But what if you have an Access Control and you want to know its color as an RGB value?

Then you use my DeRGB Function!
It returns a string that is a set of three RGB values when you enter a long integer “Windows Color Code.”

strRGBColor = DeRGB(MyObject.color)

It’s actually a set of four functions, three individual functions that extract the red, blue and green values, and then a fourth that puts them all together. In practice, if you’re going to perform any mathematical calculation on the color (like making it a shade lighter), you’re going to use the individual color functions more often then the summing function.

This example sets the color of “MyObject” to one shade lighter than “MyOtherObject”

MyObject.Color = RGB(_


The Code:

'Function DeRGB() Converts positive long Color Codes into RGB
'(c) 2007 Ben Margolis

Function DeRGB(lngColor As Long) As String
Dim r As Integer, g As Integer, b As Integer
r = DeRGB_R(lngColor)
g = DeRGB_G(lngColor)
b = DeRGB_B(lngColor)
DeRGB = r & "," & g & "," & b
End Function

Function DeRGB_R(lngColor As Long)
DeRGB_R = Val("&H" & right(CStr(Hex(lngColor)), 2))
End Function

Function DeRGB_G(lngColor As Long)
DeRGB_G = Val("&H" & Mid(CStr(Hex(lngColor)), 3, 2))
If Len(CStr(Hex(lngColor))) = 4 Then DeRGB_G = Val("&H" & Left(CStr(Hex(lngColor)), 2))
If Len(CStr(Hex(lngColor))) = 2 Then DeRGB_G = 0
End Function

Function DeRGB_B(lngColor As Long)
DeRGB_B = Val("&H" & Left(CStr(Hex(lngColor)), 2))
If Len(CStr(Hex(lngColor))) < 5 Then DeRGB_B = 0
End Function


How it works:
The “Windows Color Code” Long Integer when converted into hex becomes a set of three two-digit hexadecimal numbers (just like an RGB hex) but for some reason these values are in the order Green, Blue, then Red. (Don’t ask me why.)

My DeRGB functions simply converts the long into a hex, then coverts the hex into a string, uses Right(), Left(), or Mid$() to extract the right two digits, and then converts them back into integers.

Windows also uses NEGATIVE values in the color code to represent “system colors” (i.e. colors determined by the “color scheme” or set by the user through the desktop control panel). Entering a negative value into DeRGB will result in an incorrect result, but NOT an error message. Catching this error or preferably deciphering system colors into RGB is NYI.


Home | Ben's A.I. Blog | Ben's CGI Blog | SpaceArt | Science Fiction | Free VB Code | Who I Am | About the Site | Contact |

 © 2014 Ben Margolis All Rights Reserved. Privacy Statement