RGB to Access conversion formula

Cowboy_BeBa

Registered User.
Local time
Today, 17:53
Joined
Nov 30, 2010
Messages
188
Ive been recently experimenting with changing control colours via vba (mostly for a menu/submenu, i have it colour coded, so when a user clicks a button it brings up a differently coloured submenu)
the problem is when i change a backcolour im very limited in the colours i choose
im using
me.controlname.backcolor=vbgreen
i tried putting in a hex value, no luck, i couldnt put in an RGB, i noticed that access vba uses weird numbers (i cant understand what they are) for instance i found that an access theme theme color for the control "Background Light Header", which equals "-2147483612" somehow
also found this page which gives me a list of numbers for certain colours
http://www.endprod.com/colors/

whilst the page i found is helpful and opens up my options im greedy, i want more
does anyone know of a formula or way to convert any RGB number to the msaccess numbers?
 
According to the link you provided
There are over 500 color definitions in this list.
 
youd be surprised how many of them are different shades of grey
also how many different combinations are available with rgb? 255, 255 and 255? over 16 million? compared to that 500 aint much
 
Yes I saw the 99 shades of grey --most looked black, but what do you want to do with say 8 million colors in Access?
You could experiment by adjusting the hex codes and looking at results --if it's important. Perhaps another forum or area --image processing, graphics.... would have more details re getting the 8-16 million colors.


And Dark Slate gray1 leaves a lot to the imagination!
dark slate gray 1 #97FFFF 151 255 255 16777111


Good luck.
 
The colour numbers in Access are made up of the RGB as three groups of eight bits expressed as a decimal number.

The lowest eight bits represent the red level, the next eight for green and the highest eight for blue.

Use the RGB() function
For curiosity's sake, here is its essence:

Code:
 Public Function RGB2Dec(ByVal Red As Integer, ByVal Green As Integer, ByVal Blue As Integer) As Long
 
     If Red > 256 Or Green > 256 Or Blue > 256 Then
        RGB2Dec = -1
    Else
        RGB2Dec = (Blue * 2 ^ 16) + (Green * 2 ^ 8) + Red
    End If
    
End Function
 
Last edited:
Remember the function I posted is just for curiosity's sake to show how it works.

Just use the built in Access RGB() function.
 
When you say that your color number looks like "-2147483612" that tells me that you have chosen to use color codes from your Windows Theme. All 24-bit RGB colors would be positive integers, but the "system colors" will be negative. Note that using the system colors isn't a wrong solution, but this number you showed by an example isn't using your own color scheme anyway, so RGB function never enters that picture.
 
The negative colours reflect to items in your form matching items in the active windows theme, rather than specific colours (as DOC Man just said)

I am not sure if these values resolve as the negative numbers
https://msdn.microsoft.com/en-us/library/office/gg264801.aspx

these are the colours in decimal
tricky finding the right google search for this, even when you know what you are looking for. I used this in the end "using system colours in ms access"

http://www.peterssoftware.com/sysclrs.htm



with regard to hex values, I thought you can put the colour pairs in together in later access versions - but if not, it's fiddly to evaluate the decimal value of a hex number.

so, eg rgb(255,0,255) (magenta) should show as
#FF00FF


I also think it is possible that the displayed hex RGB is actually inverted to hex BGR. I am sure I saw this note in relation to some colour processing topics
 
Last edited:
Dave, here is the fun part about the order of color value elements.

Access does RGB as three bytes in "little-Endian" order. In a hex string, the color string for a 24-bit color looks like %H0bgr for blue, green, red. I.e. red is %H000 through %H010 ending with %H0FF. Green is %X00000 ending with %h0FF00. Blue ends with %H0FF0000.

Excel does RGB as three bytes in "big-Endian" order. In a hex string, the color looks like %H0rgb for red, green, blue. Therefore, in Excel, red is %H0FF0000 and blue is %H0FF. That lucky green... always gets the middle.

If you look up color codes, be sure to know whether you looked up the Excel information or the Access information because they are backwards from each other as hex numbers.

For anyone who wants to look up the "system color codes" just get into any Access database, open up a code window, open the Object Browser, and in the search window, type "SystemColor" - which brings up the list of color constants that would link back to your current Windows Theme selection. It is VBA.SystemColorConstants that lists the definitions.

I'm not going to swear on this, but since this is part of the standard VBA library, you might even be able to open the properties for a control and assert the "vbxx" name of the Theme feature. But if not, just use Object Browser, select the Theme object you wanted to match, and the value will show up at the bottom of the O.B. display.

Technically, they look like this: %H800000xx where xx is 1 to the number of possible objects in a Windows Theme. That high-order bit is a flag of sorts, but it also occupies the 2's complement sign-bit of LONG integers, so that's why it looks negative.
 

Users who are viewing this thread

Back
Top Bottom