Results 1 to 6 of 6

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

  1. #1

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

    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 10:40 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

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

    hello! si_the_geek!

    it's me again..

    ahm, can i cast a rgb value into a string not similar to the post above?
    like in c#:

    colorcast{
    "255,255,255": "white"
    "255,0,0": "red"
    }

    is this possible in vb6?

    interest controls curiosity.
    conscience controls karma.
    you alone can control yourself.
    know yourself and be above those who are lost.
    skemb321
    hehehe

  3. #3

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

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

    If I understand what you mean, it would be like this:
    Code:
    Select Case lngColour   '(replace with your variable etc)
    Case vbWhite
       strColour = "white"
    Case vbRed
       strColour = "red"
    Case RGB(123,45,67)
       strColour = "another colour"
    ...
    Case Else
       strColour = "unknown"
    End Select
    ...but because this kind of method only picks up very specific colours, it will leave the vast majority of colours as "unknown" (eg: people will think RGB(254,253,255) is white, but this kind of code would not be treat it as that).

    If you want most/all colour codes to be given a name, you should use the kind of method shown in post #1 above.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    24

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

    omg. so i will use long values then.

    it is hard to use long values because they are "long"


    interest controls curiosity.
    conscience controls karma.
    you alone can control yourself.
    know yourself and be above those who are lost.
    skemb321
    hehehe

  5. #5
    Member
    Join Date
    Dec 2010
    Location
    PORTO, PORTUGAL
    Posts
    55

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

    Quote Originally Posted by skemb321 View Post
    hello! si_the_geek!

    it's me again.. can i cast a rgb value
    Code:
    Of course You can.
    
    IF You want to use several colours by name
    
    Made public the mame of colours. I make it in separate módulo type
    Keep.bas
    
    
    Public red$
    Public myfavoriteRose$
    Public Black$
    etc. etc.
    
    dim would do the same
    
    And made in
    Form load
    Red$= RGB (255,0,0)
    MyfavoriteRose$= RGB(255,240,240)
    Public Black$ = RGB(0,0,0)
    etc. Etc
    
    Is this what You want?
    Or do You want to compare colours by number?

  6. #6
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

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

    EDIT: How did I even get here? Removed post
    Last edited by DEXWERX; Feb 16th, 2016 at 04:24 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width