Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C Find closest color (rgb) match

I have a predefined array of rgb values. I want to be able to compare a user defined color to my array and return the closest match in Objective C.

Any help is greatly appreciated.

like image 212
yesimarobot Avatar asked Oct 14 '25 16:10

yesimarobot


2 Answers

You could try finding the Sum-of-Squared-Differences between your predefined color and the user defined color and choose the predefined color with the minimum "distance".

E.g. suppose the user-defined color is [120 300 200] and a predefined color is [100 250 150], then the sum of squared differences and the score is:

(120-100)*(120-100) + (300-250)*(300-250) + (200-150)*(200-150) = 5400 - and choose the prefefined color with the least difference.

like image 200
Jacob Avatar answered Oct 17 '25 06:10

Jacob


You need to decide in what colour space you are testing.

HSL is arguably a better colour space than RGB because you can give more weight to "hue difference" and less to "lightness". In RGB space you need to skew your differences because the eye is better at discerning shades of green than other colours.

like image 20
Jeff Avatar answered Oct 17 '25 06:10

Jeff