If backcolor is 11322594 is there a way to calculate its RGB values ?
If backcolor is 11322594 is there a way to calculate its RGB values ?
a quick google found this link
https://nolongerset.com/convertcolortorgb/
I use this function
Code:Public Function DecimalToRGB(Dec) As String Dim Hexval As String, Red As String, Green As String, Blue As String Hexval = Hex(Dec) Hexval = Replace(Hexval, "#", "") Red = Val("&H" & Mid(Hexval, 1, 2)) Green = Val("&H" & Mid(Hexval, 3, 2)) Blue = Val("&H" & Mid(Hexval, 5, 2)) DecimalToRGB = "RGB(" & Red & "," & Green & "," & Blue & ")" End Function Immediate window ?decimaltorgb(11322594) RGB(172,196,226) Sometimes it doesn't appear to be the right color. If so, swap the red and blue in the last line.
Last edited by davegri; 04-12-2024 at 07:00 PM. Reason: show result
Thank you.
Function referenced by CJ is more reliable. Example for Green.
?DecimalToRGB(65280)
RGB(255,0,0)
?ConvertColorToRGB(65280)
RGB(0, 255, 0)
The second one is correct.
How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
Ah, if the decimal value converts to a hexadecimal value length of less than 6 characters, it needs to be left padded with zero to make length of 6.
Code:Public Function DecimalToRGB(Dec) As String Dim Hexval As String, Red As String, Green As String, Blue As String Hexval = Hex(Dec) Do While Len(Hexval) < 6 Hexval = "0" & Hexval Loop Red = Val("&H" & Mid(Hexval, 1, 2)) Green = Val("&H" & Mid(Hexval, 3, 2)) Blue = Val("&H" & Mid(Hexval, 5, 2)) DecimalToRGB = "RGB(" & Red & "," & Green & "," & Blue & ")" End Function ?decimaltorgb(65280) RGB(0,255,0)
Example for Cyan:
?DecimalToRGB(16776960)
RGB(255,255,0)
?ConvertColorToRGB(16776960)
RGB(0, 255, 255)
Again, second one is correct.
See table below. HexGBR can be used in place of RGB() in VBA.
ID ColorName ColorCodeDec ColorCodeRGB ColorCodeHex HexGBR 1 Black 0 000,000,000 000000 &H0 2 Red 255 255,000,000 FF0000 &HFF 3 Green 65280 000,255,000 00FF00 &HFF0000 4 Yellow 65535 255,255,000 FFFF00 &HFF00FF 5 Blue 16711680 000,000,255 0000FF &HFF00 6 Magenta 16711935 255,000,255 FF00FF &HFFFF 7 Cyan 16776960 000,255,255 00FFFF &HFFFF00 8 White 16777215 255,255,255 FFFFFF &HFFFFFF
How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
As stated in post #3, red and blue must sometimes be swapped if color is unexpected.Again, second one is correct.
This is annoying. I am trying to determine codes for Gold (as shown in font color chart in post editor toolbar). Hex is FFD700. #FFD700 works in BackColor property. Calculator tells me the decimal is 16766720. Dave's function correctly converts to RGB(255, 215, 0). The other gives RGB(0, 215, 255).
So neither function is 100% reliable.
Apparently, only the 8 vb colors have a corresponding &H value.
How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
I've never completely understood (or I just can't recall) why and when RGB is really BGR. I think Windows uses the latter, so maybe that can be more prevalent with things like html. However, I have run into this before but that was some time ago. IIRC, decimal values are used as RGB; hex conversion requires BGR. Not sure. Just seems that in some cases, the numbers are read from right to left.
Last edited by Micron; 04-13-2024 at 11:58 AM. Reason: correction
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Yes, I've encountered this frustration before as well.
I tried every combination for Gold &H - none show Gold color. Seems I read somewhere that only the VB colors have &H values and my testing supports.
How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
Not sure what your desired colour looks like; how about
9171455 (Windows?)
or 8BF1FF (hex)
or #FFF18B (property sheet), which I suppose many Access vba procedures convert to RGB values but will do so in the wrong order because the BGR thing may not be widely known.
Last edited by Micron; 04-13-2024 at 12:59 PM. Reason: clarification
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
#FFD700 shows color I was trying for. It looks like color named Gold in the font color tool of this post editor.
How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.
Should be #55295 I think. That means there is red and green but no blue - #FFD700 or RGB(255,215,00)
Some of this is coming back to me. So in the Office color picker, the hex values are displayed in RGB order as above.
The immediate window shows the value as decimal (no separators) as 55295. Hex(55295)=D7FF
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
This should be comprehensive and correct. Not all my original verbatim code - found in various places. I made adaptions to allow universal compatible conversions between the functions.
Note that DecToHex calls DecToRGB
Code:Function DecToRGB(Dec As Long) As String Dim R As Integer, G As Integer, B As Integer, Hexval As String If Dec > 16777215 Then Dec = 16777215 'FFFFFF is max allowed = white R = Dec And 255 G = (Dec \ 256) And 255 B = (Dec \ 65536) And 255 DecToRGB = R & "," & G & "," & B End Function Function RGBtoHEX(R As Integer, G As Integer, B As Integer) As String Dim H3 As Variant, H2 As Variant, H1 As Variant If R > 255 Or G > 255 Or B > 255 Then MsgBox "R, G, and B must all be between 0 and 255 inclusive.", vbOKOnly + vbCritical, " E R R O R " Exit Function End If If R < 16 Then 'pad with left zero H1 = 0 & Hex(R) Else H1 = Hex(R) End If If G < 16 Then 'pad with left zero H2 = 0 & Hex(G) Else H2 = Hex(G) End If If B < 16 Then 'pad with left zero H3 = 0 & Hex(B) Else H3 = Hex(B) End If RGBtoHEX = "#" & H1 & H2 & H3 End Function Function DecToHex(Dec As Long) As String Dim rgb As String, arr3() As String Dim R As Integer, G As Integer, B As Integer rgb = DecToRGB(Dec) arr3 = Split(rgb, ",") R = (arr3(0)): G = (arr3(1)): B = (arr3(2)) DecToHex = RGBtoHEX(R, G, B) End Function