# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB - Converting an RGB colour number to a colour name

## si_the_geek

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:
Private Function ColourLongToWords(ByVal lColour As Long) As String
'Converts an RGB colour number to a string equivalent
'By Si_the_geek, VBForums
     'ensure value is within range for colours
  lColour = lColour And &HFFFFFF
     'convert to separate RGB values
Dim iRed As Integer, iGreen As Integer, iBlue As Integer
  iRed = lColour And &HFF
  iGreen = (lColour \ &H100) And &HFF
  iBlue = lColour \ &H10000
     '"guess" the colour based on these values
Dim sColourName As String
  Select Case iRed
  Case Is > 170     'lots of red
      Select Case iGreen
      Case Is > 170     'lots of green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "White"
          Case Is > 85:   sColourName = "Bright Yellow"
          Case Else:      sColourName = "Yellow"
          End Select
      Case Is > 85      'medium green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Pink"
          Case Is > 85:   sColourName = "Magenta"
          Case Else:      sColourName = "Orange"
          End Select
      Case Else         'little green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Purple"
          Case Is > 85:   sColourName = "Dark Pink"
          Case Else:      sColourName = "Red"
          End Select
      End Select
   Case Is > 85      'medium red
      Select Case iGreen
      Case Is > 170     'lots of green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Cyan"
          Case Is > 85:   sColourName = "Green"
          Case Else:      sColourName = "Bright Green"
          End Select
      Case Is > 85      'medium green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Dark Blue"
          Case Is > 85:   sColourName = "Dark Grey"
          Case Else:      sColourName = "Dark Green"
          End Select
      Case Else         'little green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Dark Blue"
          Case Is > 85:   sColourName = "Purple"
          Case Else:      sColourName = "Dark Red"
          End Select
      End Select
   Case Else         'little red
      Select Case iGreen
      Case Is > 170     'lots of green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Cyan"
          Case Is > 85:   sColourName = "Green"
          Case Else:      sColourName = "Bright Green"
          End Select
      Case Is > 85      'medium green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Blue"
          Case Is > 85:   sColourName = "Dark Cyan"
          Case Else:      sColourName = "Dark Green"
          End Select
      Case Else         'little green
          Select Case iBlue  'blue:  lots/medium/little
          Case Is > 170:  sColourName = "Bright Blue"
          Case Is > 85:   sColourName = "Dark Blue"
          Case Else:      sColourName = "Black"
          End Select
      End Select
   End Select
   ColourLongToWords = sColourName
 End Function

----------


## skemb321

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?

----------


## si_the_geek

If I understand what you mean, it would be like this:


```
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.

----------


## skemb321

omg. so i will use long values then.

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

 :Frown:

----------


## GESPO

> hello! si_the_geek!
> 
> it's me again.. can i cast a rgb value




```
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?
```

----------


## DEXWERX

*EDIT:* How did I even get here? Removed post

----------

