检查颜色是否在特定颜色范围内 [英] Checking if a color is in a specific range of colors
问题描述
如何检查rgb或十六进制值是否在特定颜色范围内?最好用ruby。
How would you check if a rgb or hex value is within a specific range of colors? Preferably with ruby.
我使用ruby和rmagick从图像中提取颜色(quantize和color_histogram),然后将这些颜色存储在数据库中。如果有人搜索类似的颜色(hex或rgb),我想要能够返回该颜色。
I'm using ruby and rmagick to extract colors (quantize and color_histogram) from images and then store those colors in the database. If someone searched for a color(hex or rgb) that is similar I want to be able to return that color.
例如。如果有人搜索#f4f4f4,我想返回#f5f5f5,#f3f3f3和所有其他关闭十六进制值。
e.g. If someone searched for #f4f4f4 I'd like to return #f5f5f5, #f3f3f3, and all the other close hex values.
推荐答案
如果将RGB视为三维空间,以R,G和B为轴,则可以将接近颜色定义为颜色周围的立方体或球体,并返回其内部的所有颜色(或检查给定颜色,如果它足够近)。它的形式很简单:
If you treat RGB as a three-dimensional space with R, G and B being the axes, you can define "close colors" as a cube or a sphere around a color and return all the colors inside it (or check for a given color if it's close enough). Formulars for that are quite simple:
Original color R, G, B
Cube with side length L around it:
All colors between (R - L/2, G - L/2, B - L/2) and (R + L/2, G + L/2, B + L/2)
Sphere with radius R around it:
New color R_new, G_new, B_new is inside if
delta_r * delta_r + delta_g * delta_g + delta_b * delta_b < R * R
where
delta_r = abs(R - R_new)
delta_g = abs(G - G_new)
delta_b = abs(B - B_new)
使用球体而不是多维数据集是正确的方式,但它不会对小的并且立方体内的颜色有点更容易计算。
Using a sphere instead of a cube is the "correct" way, but it won't make much of a difference for small ones and the colors inside the cube are a bit easier to calculate.
这篇关于检查颜色是否在特定颜色范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!