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
2 Answers 2
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.
-
1You might also want to read a little about dithering; this way the image will look more natural in a different palette. (If there is a picture, of course)K.Steff– K.Steff08/07/2012 22:10:07Commented Aug 7, 2012 at 22:10
-
Do you mean the following formula - (Square(Red(source)-Red(target))) + (Square(Green(source)-Green(target))) +(Square(Blue(source)-Blue(target))) ?LCJ– LCJ08/09/2012 06:12:33Commented Aug 9, 2012 at 6:12
-
1
You can treat RGB colours as a 3-dimensional vector.
The distance between any two points (colors) is given by the following formula:
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.
-
This is essentially the same as the other answer (a bit better described - though more could be done). I would note that it's the difference for those RGB values. You could have a stronger answer by working through the steps to find the nearest name of the origin color (#5D8AA7) from Air Force Blue (#5D8AA8) and Carolina Blue (#56A0D3)user40980– user4098011/26/2014 16:22:48Commented Nov 26, 2014 at 16:22
-
Since you're only comparing magnitude, taking the square root is unnecessary. if X>Y, sqrt(X) > sqrt(Y) . See also: math.stackexchange.com/q/376730/3862Brian– Brian10/23/2015 03:52:01Commented Oct 23, 2015 at 3:52
-
-
1Thinking about this again (as the question got bumped) there are some additional complications to consider. Our eyes are less sensitive to blue than green. This was taken advantage of with 8 bit color giving RRRGGGBB. This can again be seen in the ppmtopgm program where it uses gray = .299 r + .587 g + .114 b. So two colors, being the same mathematical distance apart from a source, the one that is closer in green would be more similar to our eyes.user40980– user4098010/23/2015 13:21:32Commented Oct 23, 2015 at 13:21
-
1Actually calculating the square root (as the given formula suggests) is a waste of resources. You can just as well only calculate
(R²+G²+B²)
and select the color with the minimal (squared) distance.user281377– user28137712/17/2018 11:47:16Commented Dec 17, 2018 at 11:47