Skip to content
Advertisement

Convert RGB values to color name

How can I convert a color in RGB format into a color name?

Examples:

  • (0, 0, 255) -> “blue”
  • (255, 0, 0) -> “red”
  • (128, 128, 128) -> “grey”

Advertisement

Answer

  1. Populate a list of “known” colors.
  2. For each known color compute:

    rgbDistance = Math.abs(myColor.getRed() - colorI.getRed() + 
                  Math.abs(myColor.getGreen() - colorI.getGreen()) +
                  Math.abs(myColor.getBlue() - colorI.getBlue())  
    
  3. Return the name of colorI for which rgbDistance was smallest.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement