PDA

View Full Version : OLE to RGB conversion



rrenis
08-23-2007, 02:57 AM
Hi - does anyone know how to convert an OLE value to RGB? I'm specifically after the RGB Button Face value (which in OLE is H8000000F) but if anyone know of some VBA code or application that can perform a conversion from an OLE value I'd be very grateful if you could share it.

: pray2:

Cheers,
rrenis.

Bob Phillips
08-23-2007, 04:09 AM
It is not a straight-forward conversion, as these colours are obtained from the windows settings, and so they are indexed into a colour table. The value can be obtained using the GetSysColor API, if you know the index of that colour type.

This simple example holds a table of those indexes and returns the RGB value from a function



Declare Function GetSysColor Lib "user32" ( _
ByVal nIndex As Long) As Long

Public Enum Colour_Constants
COLOR_SCROLLBAR = 0
COLOR_BACKGROUND = 1
COLOR_ACTIVE_CAPTION = 2
COLOR_INACTIVE_CAPTION = 3
COLOR_MENU = 4
COLOR_WINDOW = 5
COLOR_WINDOWFRAME = 6
COLOR_MENU_TEXT = 7
COLOR_WINDOW_TEXT = 8
COLOR_CAPTION_TEXT = 9
COLOR_ACTIVE_BORDER = 10
COLOR_INACTIVE_BORDER = 11
COLOR_APP_WORKSPACE = 12
COLOR_HIGHLIGHT = 13
COLOR_HIGHLIGHT_TEXT = 14
COLOR_BUTTON_FACE = 15
COLOR_BUTTON_SHADOW = 16
COLOR_GRAY_TEXT = 17
COLOR_BUTTON_TEXT = 18
COLOR_INACTIVE_CAPTION_TEXT = 19
COLOR_BUTTON_HIGHLIGHT = 20
COLOR_BUTTON_DARK_SHADOW = 21
COLOR_BUTTON_LIGHT_SHADOW = 22
COLOR_TOOLTIP_TEXT = 23
COLOR_TOOLTIP = 24
End Enum

Function WindowsColourToRGB(ColourType As Colour_Constants) As Long
WindowsColourToRGB = GetSysColor(ColourType)
End Function


You can test it like so



Sub TestColours()
ActiveCell.Interior.Color = WindowsColourToRGB(COLOR_BUTTON_FACE)
End Sub


It can be simplified as it seems the index is held within that value, so you can strip that and pass it



Function ColourTypeToRGB(ColourType) As Long
ColourTypeToRGB = GetSysColor(ColourType And &HFF)
End Function


which you can test like so



Sub TestColours2()
ActiveCell.Interior.Color = ColourTypeToRGB(&H8000000F)
End Sub

RichardSchollar
08-23-2007, 05:00 AM
Hey that's pretty cool Bob!

Have another (metaphorical) Sapphire on me :wine:

Richard

Bob Phillips
08-23-2007, 05:16 AM
The mixer is in the fridge.

I wish I had cracked this a couple of years ago. I remember amending an app with some images on it, and the images had a white halo. The client wanted it to be the same as form background colour, and I ended up taking a screenprint of the form, pasting that into Fireworks, and using the colour dipper to get the colour RGB, and then putting that colour into the original images. Convoluted or what? This would have made life easier then.

rrenis
08-23-2007, 05:34 AM
Thanks xld - have to say my post was a bit of a long shot - certainly didn't expect a reply so soon!! :cloud9:

Thanks alot for sharing this!

:clap2:

Cheers,
rrenis