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.
我有一个预定义的rgb值数组。我希望能够将用户定义的颜色与我的数组进行比较,并在Objective C中返回最接近的匹配。
Any help is greatly appreciated.
非常感谢您的帮助。
4 个解决方案
#1
2
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.
HSL可以说是比RGB更好的颜色空间,因为您可以赋予“色度差异”更多的权重,而不是“亮度”。在RGB空间中,你需要倾斜你的差异,因为眼睛更善于辨别绿色的深浅。
#2
1
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 300 200]和预定义的颜色是[100 250 150],那么平方差异和分数的总和为:
(120-100)*(120-100) + (300-250)*(300-250) + (200-150)*(200-150) = 5400
- and choose the prefefined color with the least difference.
(120-100)*(120-100) +(300-250)*(300-250) +(200-150)*(200-150) = 5400 -选择差异最小的预热颜色。
#3
1
This begs the question or what closeness in colors is.
这就引出了一个问题,或者说颜色的亲密程度。
You will need to try this, colors are not all math. If one want to find the distance between two points in 3D space (there are three colors) the math given color1 and color2 would be: ((r2-r1)^2 + (b2-b2)^2 + (g2-g1)^2)^0.5 (the final square root is not necessary for comparison purposes).
你需要尝试一下,颜色并不都是数学。如果一个人想要找到两个点之间的距离在3 d空间(有三个颜色)的数学color1和color2:((r2-r1)^ 2 +(b2-b2)^ 2 +(g2-g1)^ 2)^ 0.5(最后一根没有必要进行比较)。
Possible a better way would be to do the calculations in HSB space, possible just looking at Hue.
可能更好的方法是在HSB空间中进行计算,可能只看色调。
#4
0
First, you should define a distance function. The trivial one is a vector length function: sum of sq. of difference by all dimensions.
首先,您应该定义一个距离函数。最小的是一个向量长度函数:平方和。所有维度的差异。
Then just run through your array and select the closet one (with minimal distance).
然后在数组中运行,选择壁橱一个(最小的距离)。
#1
2
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.
HSL可以说是比RGB更好的颜色空间,因为您可以赋予“色度差异”更多的权重,而不是“亮度”。在RGB空间中,你需要倾斜你的差异,因为眼睛更善于辨别绿色的深浅。
#2
1
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 300 200]和预定义的颜色是[100 250 150],那么平方差异和分数的总和为:
(120-100)*(120-100) + (300-250)*(300-250) + (200-150)*(200-150) = 5400
- and choose the prefefined color with the least difference.
(120-100)*(120-100) +(300-250)*(300-250) +(200-150)*(200-150) = 5400 -选择差异最小的预热颜色。
#3
1
This begs the question or what closeness in colors is.
这就引出了一个问题,或者说颜色的亲密程度。
You will need to try this, colors are not all math. If one want to find the distance between two points in 3D space (there are three colors) the math given color1 and color2 would be: ((r2-r1)^2 + (b2-b2)^2 + (g2-g1)^2)^0.5 (the final square root is not necessary for comparison purposes).
你需要尝试一下,颜色并不都是数学。如果一个人想要找到两个点之间的距离在3 d空间(有三个颜色)的数学color1和color2:((r2-r1)^ 2 +(b2-b2)^ 2 +(g2-g1)^ 2)^ 0.5(最后一根没有必要进行比较)。
Possible a better way would be to do the calculations in HSB space, possible just looking at Hue.
可能更好的方法是在HSB空间中进行计算,可能只看色调。
#4
0
First, you should define a distance function. The trivial one is a vector length function: sum of sq. of difference by all dimensions.
首先,您应该定义一个距离函数。最小的是一个向量长度函数:平方和。所有维度的差异。
Then just run through your array and select the closet one (with minimal distance).
然后在数组中运行,选择壁橱一个(最小的距离)。