How can I tell basic color a hex code is closest to?

The following hex values would all probably be considered “red” by the layman’s eye:

enter image description here

Is there a way, using only the hex code, to determine which basic color a given color is most similar to? I would like to be able to do this for red, blue, green, yellow, green, purple, brown, pink, black, and white as well.


You’ll have to define what ‘closest’ means and what your target colors are. Color can be thought of as a cube, a cylinder, and many other shapes, and your goal is essentially to find the closest color within that 3 dimensional shape.

If you restrict yourself to only the primary colors, then the most straightforward manner would be to convert your colors to the HSV colorspace.

Adding in pink makes it a bit trickier, because in HSV Pink and red would have the same hue. Now you would have to factor in the luminance/value. You could do a two stage process where you only compare the luminance between two target colors of the same hue.

A similar test would take place for white and black. In HSV, whites and blacks can have bizzarely varying hues, so you would want to ignore the hue when testing for that.

With that all said, you may be able to take the square root of the sums of differences in RGB channels between your sample and a target, use that as a measure of distance, and compare all your targets and select the one with the minimum distance. Computing the distances in difference color spaces may yield different result characteristics.

Source : Link , Question Author : Lincoln Bergeson , Answer Author : whatsisname

Leave a Comment