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
- Populate a list of “known” colors.
For each known color compute:
rgbDistance = Math.abs(myColor.getRed() - colorI.getRed() + Math.abs(myColor.getGreen() - colorI.getGreen()) + Math.abs(myColor.getBlue() - colorI.getBlue())
Return the name of
colorI
for whichrgbDistance
was smallest.