The new vbAccelerator Site - more VB and .NET Code and Controls

Get an RGB Colour from an OLE_COLOR


Steve McMahon(


API,GDI,Graphics,Windows Controls



Other Tips
All Tips
By Date
By Subject

API (33)
Manipulation (3)

Clipboard (3)
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Box (2)

Windows (11)
Controls (10)


Sometimes you need to know the Red, Green and Blue values of a Visual Basic OLE Colour, particularly if you are going to use the colour in an API function. This tip shows you how to correctly convert an OLE_COLOR type to a RGB value using the OleTranslateColor API call exposed by OLEPRO32.DLL. It works for all colours, whether they are RGB colours, system colours of the type &H80000000F (vbButtonFace) or palette-matching colours such as &H2EECC99.

Start a new project in VB. Add the following code to the project's form:

Private Declare Function OleTranslateColor Lib "OLEPRO32.DLL" (ByVal OLE_COLOR As Long, ByVal HPALETTE As Long, pccolorref As Long) As Long
Private Const CLR_INVALID = -1

Private Function TranslateColor(ByVal oClr As OLE_COLOR, _
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Optional hPal As Long = 0) As Long
&nbsp &nbsp
' Convert Automation color to Windows color
&nbsp &nbsp If OleTranslateColor(oClr, hPal, TranslateColor) Then
&nbsp &nbsp &nbsp &nbsp TranslateColor = CLR_INVALID
&nbsp &nbsp End If
End Function

To test out the function, add a Combo box, a Label and a Text Box to the project's form. Set the style of the Combo box to 2 (Drop Down List) and then add the following code:

Private Sub Combo1_Click()
Dim lRGB As Long
&nbsp &nbsp
&nbsp &nbsp Label1.BackColor = Combo1.ItemData(Combo1.ListIndex)
&nbsp &nbsp lRGB = TranslateColor(Combo1.ItemData(Combo1.ListIndex))
&nbsp &nbsp Text1.Text = "R=" & (lRGB And &HFF&) & ",G=" & (lRGB And &HFF00&) \ &H100 & ",B=" & (lRGB And &HFF0000) \ &H10000

End Sub

Private Sub Form_Load()
Dim i As Long
Dim sNum As String
&nbsp &nbsp For i = 1 To &H18&
&nbsp &nbsp &nbsp &nbsp sNum = Hex$(i)
&nbsp &nbsp &nbsp &nbsp If Len(sNum) = 1 Then sNum = "0" & sNum
&nbsp &nbsp &nbsp &nbsp Combo1.AddItem "&H800000" & sNum
&nbsp &nbsp &nbsp &nbsp Combo1.ItemData(Combo1.NewIndex) = &H80000000 + i
&nbsp &nbsp Next i
&nbsp &nbsp Combo1.ListIndex = 0
End Sub

When you change the selected item in the combo box , the text box will be set to the R,G,B components of the colour.


Related Tips and Articles:


AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( All Rights Reserved.
Last updated: 09/08/98