VB - Converting an RGB colour number to a colour name-VBForums
Results 1 to 5 of 5

Thread: VB - Converting an RGB colour number to a colour name

Threaded View

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,230

    VB - Converting an RGB colour number to a colour name

    As you may already know, a computer does not actually understand the names of colours, nor how to convert from one to the other.

    VB does hide this to a degree, as it gives you some constants for colours, such as vbWhite - which has a value of 16777215. These are the kinds of numbers that are actually used to create colours on-screen.

    You may think that you could just convert back to a "name" by just using If's or Select Case's to match the numbers, however there are a wide variety of computer colours which look very similar to the human eye.

    The method used below is to convert the number into separate R,G,B values, and then make a best-guess based on these as to what the colour is.

    You may not agree with the names I have used for the colours, but changing that is easy enough!
    VB Code:
    1. Private Function ColourLongToWords(ByVal lColour As Long) As String
    2. 'Converts an RGB colour number to a string equivalent
    3. 'By Si_the_geek, VBForums
    4.  
    5.     'ensure value is within range for colours
    6.   lColour = lColour And &HFFFFFF
    7.  
    8.     'convert to separate RGB values
    9. Dim iRed As Integer, iGreen As Integer, iBlue As Integer
    10.   iRed = lColour And &HFF
    11.   iGreen = (lColour \ &H100) And &HFF
    12.   iBlue = lColour \ &H10000
    13.  
    14.     '"guess" the colour based on these values
    15. Dim sColourName As String
    16.   Select Case iRed
    17.   Case Is > 170     'lots of red
    18.       Select Case iGreen
    19.       Case Is > 170     'lots of green
    20.           Select Case iBlue  'blue:  lots/medium/little
    21.           Case Is > 170:  sColourName = "White"
    22.           Case Is > 85:   sColourName = "Bright Yellow"
    23.           Case Else:      sColourName = "Yellow"
    24.           End Select
    25.       Case Is > 85      'medium green
    26.           Select Case iBlue  'blue:  lots/medium/little
    27.           Case Is > 170:  sColourName = "Pink"
    28.           Case Is > 85:   sColourName = "Magenta"
    29.           Case Else:      sColourName = "Orange"
    30.           End Select
    31.       Case Else         'little green
    32.           Select Case iBlue  'blue:  lots/medium/little
    33.           Case Is > 170:  sColourName = "Purple"
    34.           Case Is > 85:   sColourName = "Dark Pink"
    35.           Case Else:      sColourName = "Red"
    36.           End Select
    37.       End Select
    38.  
    39.   Case Is > 85      'medium red
    40.       Select Case iGreen
    41.       Case Is > 170     'lots of green
    42.           Select Case iBlue  'blue:  lots/medium/little
    43.           Case Is > 170:  sColourName = "Cyan"
    44.           Case Is > 85:   sColourName = "Green"
    45.           Case Else:      sColourName = "Bright Green"
    46.           End Select
    47.       Case Is > 85      'medium green
    48.           Select Case iBlue  'blue:  lots/medium/little
    49.           Case Is > 170:  sColourName = "Dark Blue"
    50.           Case Is > 85:   sColourName = "Dark Grey"
    51.           Case Else:      sColourName = "Dark Green"
    52.           End Select
    53.       Case Else         'little green
    54.           Select Case iBlue  'blue:  lots/medium/little
    55.           Case Is > 170:  sColourName = "Dark Blue"
    56.           Case Is > 85:   sColourName = "Purple"
    57.           Case Else:      sColourName = "Dark Red"
    58.           End Select
    59.       End Select
    60.  
    61.   Case Else         'little red
    62.       Select Case iGreen
    63.       Case Is > 170     'lots of green
    64.           Select Case iBlue  'blue:  lots/medium/little
    65.           Case Is > 170:  sColourName = "Cyan"
    66.           Case Is > 85:   sColourName = "Green"
    67.           Case Else:      sColourName = "Bright Green"
    68.           End Select
    69.       Case Is > 85      'medium green
    70.           Select Case iBlue  'blue:  lots/medium/little
    71.           Case Is > 170:  sColourName = "Blue"
    72.           Case Is > 85:   sColourName = "Dark Cyan"
    73.           Case Else:      sColourName = "Dark Green"
    74.           End Select
    75.       Case Else         'little green
    76.           Select Case iBlue  'blue:  lots/medium/little
    77.           Case Is > 170:  sColourName = "Bright Blue"
    78.           Case Is > 85:   sColourName = "Dark Blue"
    79.           Case Else:      sColourName = "Black"
    80.           End Select
    81.       End Select
    82.    End Select
    83.  
    84.   ColourLongToWords = sColourName
    85.  
    86. End Function
    Last edited by si_the_geek; Oct 20th, 2005 at 11:40 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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.