Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365

    Get RGB colour


    If backcolor is 11322594 is there a way to calculate its RGB values ?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    a quick google found this link
    https://nolongerset.com/convertcolortorgb/

  3. #3
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    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

  4. #4
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    365
    Thank you.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    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.

  6. #6
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    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)

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    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.

  8. #8
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Again, second one is correct.
    As stated in post #3, red and blue must sometimes be swapped if color is unexpected.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    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.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    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.

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    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.

  12. #12
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    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.

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    #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.

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    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.

  15. #15
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    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

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Change colour of Excel Row
    By DMT Dave in forum Access
    Replies: 1
    Last Post: 01-24-2019, 01:47 PM
  2. VBA for colour coding
    By SWG in forum Forms
    Replies: 1
    Last Post: 08-29-2012, 05:20 PM
  3. Changing the colour of a cell?
    By WayneSteenkamp in forum Access
    Replies: 3
    Last Post: 03-08-2012, 10:12 AM
  4. subform background colour
    By gbmarlysis in forum Forms
    Replies: 1
    Last Post: 03-03-2012, 10:18 AM
  5. Colour Searching and Referencing...
    By ABitOfANoob in forum Programming
    Replies: 8
    Last Post: 11-23-2011, 04:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums