Objective C Find the closest color (rgb)

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

Any help is greatly appreciated.

+3
source share
4 answers

You need to decide in which color space you are testing.

HSL is arguably a better color space than RGB, because you can give more weight to the “shade of difference” and less to the “lightness”. In the RGB space, you need to skew your differences because the eye is better at penetrating shades of green than other colors.

+2
source

You can try to find the “Total Difference” between your predefined color and the given color and choose a predefined color with a minimum “distance”.

eg. Suppose that the custom color is [120 300 200] and the predefined color is [100 250 150] , then the sum of the squared differences and the estimate:

(120-100)*(120-100) + (300-250)*(300-250) + (200-150)*(200-150) = 5400 - and select a predefined color with the smallest difference.

+1
source

This asks a question or what kind of proximity in colors.

You need to try this; colors are not all maths. If you want to find the distance between two points in three-dimensional space (three colors), then the one indicated in color 1 and color2 will be: ((r2-r1) ^ 2 + (b2-b2) ^ 2 + (g2-g1) ^ 2) ^ 0.5 (the final square root is not needed for comparison purposes).

Perhaps the best way is to do the calculations in the HSB space, maybe just look at Hue.

+1
source

You must first determine the distance function. The trivial is the length function of the vector: the sum of sq. Differences across all dimensions.

Then just run your array and select the cabinet one (with the minimum distance).

0
source

Source: https://habr.com/ru/post/946356/


All Articles