Wikipedia has a list of colors that I am using for a basis of color names. I want to find the nearest named color given an arbitrary RGB value.
For example #5D8AA8
hex triplet is “Air Force Blue”. This information will be stored in a database table (tbl_Color (HexTriplet,ColorName))
in my system
Suppose I created a color with #5D8AA7
. I need to get the nearest color available in the tbl_Color table. The expected answer is “#5D8AA8
– Air Force Blue”. This is because #5D8AA8
is the nearest color for #5D8AA7
.
Do we have any algorithm for finding the nearest color?
References
- Algorithm for parsing hex into color family
- Algorithm for finding the color between two others – in the colorspace of painted colors
A simple algorithm that gives reasonably good results: Add the squared difference of each color component (red, green, blue) between the color you are looking for and the color in your list of colors and choose the color where the sum of those squared differences is minimal.
For a more accurate result, see the wikipedia article on color difference and implement one of the algorithms described there.
3
You can treat RGB colours as a 3-dimensional vector.
The distance between any two points (colors) is given by the following formula:
Your color is given as: #5D8AA8
This is made up of three hexadecimal values: [5D][8A][A8]
Follow these steps:
- Strip the
#
off the string - Break the remainder into 2x2x2 pieces and
- Use a Hex2Dec converter to convert your color into a 3D decimal values representation
Use the formula provided above to calculate the change in color.
5