Color Value Conversions - November 1998

This two part tip shows how to:
  1. Convert a Long Color to its RGB Values, and 
  2. Convert a System Color to its Long Color value.

1. Convert Long Color to RGB Values

In January 1996, I needed to 'break' apart the red, green and blue values from a long color variable. I did this so that I could save the individual values in the registry. So I wrote several subroutines and functions to perform that function. I enhanced the routines in July 1996 using a tip I found on the MSDN. This becomes the Tip-Of-The-Month.

Demo Program

I wrote a VB program to demonstrate the functions. The demo:

Convert Long Color to RGB (4527 bytes)

Enter any valid long color number (in hexadecimal notation) then click 'Convert Long to RGB Values'. You can enter a number directly into the Red, Green or Blue number boxes then press enter or use the scroll bars to change the color.

modRGB.bas Subroutines and Functions

Function or Subroutine Description
ColorCodeToRGB Supply a long color variable. The red, green and blue integer values are returned.
See the next paragraph.
RGBtoColorCode Supply red, green and blue integers, the long color variable is returned.
Similar to RGB(ired, igreen, iblue) with error trapping.
RGB_Red Supply a long color variable, the red value is returned.
RGB_Green Supply a long color variable, the green value is returned.
RGB_Blue Supply a long color variable, the blue value is returned.
ColorCodeToRGBString Supply the long color code. Used to store colors in the registry, returns: "128, 128, 255"
ColorCodeToRGBValue Return the RGB value from the color code string created using ColorCodeToRGBString.

The 'Operative' Code

Public Function ColorCodeToRGB(lColorCode As Long, iRed As Integer, iGreen As Integer, iBlue As Integer) As Boolean
' 1996/01/16 Return the individual colors for lColorCode.
' 1996/07/15 Use Tip 171: Determining RGB Color Values, MSDN July 1996.

' Enter with:
'     lColorCode contains the color to be converted
' Return:
'     iRed    contains the red component
'     iGreen  the green component
'     iBlue   the blue component

    Dim lColor As Long
    lColor = lColorCode      'work long
    iRed = lColor Mod &H100  'get red component
    lColor = lColor \ &H100  'divide
    iGreen = lColor Mod &H100 'get green component
    lColor = lColor \ &H100  'divide
    iBlue = lColor Mod &H100 'get blue component

    ColorCodeToRGB = True
End Function

Help Uses modConnectToWebSite and modInfoAbout

The demo program uses modConnectToWebSite.bas to display this page. You do not need to include it in your application. For details on the use of that module click here. Help uses module modInfoAbout to provide helpful information and an About box. You do not need to include it in your application.

Include in Your Application

You will need to add the following module to your application if you want to use this function:

modRGB.bas The routine described in this document.

Download VB Code for Demo 1

Click here to download the VB6 Source Code [7k].

2. Convert a System Color to its Long Color

In December 1999, I needed a way to convert a system color [vbActiveTitleBar] to its long color value. I began using a third-party control that did not support system colors. I posted a question on '' news group, in forum 'vb.general'. Klaus H. Probst provided the API information I needed to accomplish the conversion. I decided to include this code with the tip described above. And I wrote a demo program:

Demo Program

Drop Down Combo Box
Drop Down Combo Box

The System Color to RGB Color Demo

Select a system color from the combo box. The converted long, RGB values and color are shown. The demo uses two functions:

SystemColorToRGBColor(lSystemColor, lColor)

Download VB Code for Demo 2

Click here to download the System Color to RGB Demo VB 6 Source Code [4k].

Previous Tip    Tip Index    Next Tip


Updated 2006/03/23