Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Following page list colors with names http://en.wikipedia.org/wiki/List_of_colors.

For example #5D8AA8 Hex Triplet is "Air Force Blue". This information will be stored in a databse table (tbl_Color (HexTriplet,ColorName)) in my system

Suppose I created a color with #5D8AA7 Hex Triplet. I need to get the nearest color available in the tbl_Color table. The expected anaser 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? How to write it using C# / Java?

REFERENCE

  1. http://stackoverflow.com/questions/5440051/algorithm-for-parsing-hex-into-color-family
  2. http://stackoverflow.com/questions/6130621/algorithm-for-finding-the-color-between-two-others-in-the-colorspace-of-painte

Suggested Formula:

Suggested by @user281377. Choose the color where the sum of those squared differences is minimal

 (Square(Red(source)-Red(target))) + (Square(Green(source)-Green(target))) +(Square(Blue(source)-Blue(target)))
share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
1  
You 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 Aug 7 '12 at 22:10
    
Do you mean the following formula - (Square(Red(source)-Red(target))) + (Square(Green(source)-Green(target))) +(Square(Blue(source)-Blue(target))) ? –  Lijo Aug 9 '12 at 6:12
1  
Lijo: Yes, exactly. –  user281377 Aug 9 '12 at 6:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.