检查颜色是否在特定颜色范围内
如何检查 RGB 或十六进制值是否在特定的颜色范围内?最好用红宝石。
我使用 ruby 和 rmagick 从图像中提取颜色(量化和 color_histogram),然后将这些颜色存储在数据库中。如果有人搜索类似的颜色(十六进制或RGB),我希望能够返回该颜色。
例如,如果有人搜索#f4f4f4,我想返回#f5f5f5、#f3f3f3 和所有其他接近的十六进制值。
How would you check if a rgb or hex value is within a specific range of colors? Preferably with ruby.
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.
e.g. If someone searched for #f4f4f4 I'd like to return #f5f5f5, #f3f3f3, and all the other close hex values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将 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:
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.