Consulting

Results 1 to 5 of 5

Thread: OLE to RGB conversion

  1. #1

    OLE to RGB conversion

    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.



    Cheers,
    rrenis.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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

    [vba]

    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
    [/vba]

    You can test it like so

    [vba]

    Sub TestColours()
    ActiveCell.Interior.Color = WindowsColourToRGB(COLOR_BUTTON_FACE)
    End Sub
    [/vba]

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

    [vba]

    Function ColourTypeToRGB(ColourType) As Long
    ColourTypeToRGB = GetSysColor(ColourType And &HFF)
    End Function
    [/vba]

    which you can test like so

    [vba]

    Sub TestColours2()
    ActiveCell.Interior.Color = ColourTypeToRGB(&H8000000F)
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hey that's pretty cool Bob!

    Have another (metaphorical) Sapphire on me

    Richard

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks xld - have to say my post was a bit of a long shot - certainly didn't expect a reply so soon!!

    Thanks alot for sharing this!



    Cheers,
    rrenis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •