检查颜色是否在特定颜色范围内

发布于 2024-10-22 21:46:28 字数 209 浏览 10 评论 0原文

如何检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2024-10-29 21:46:28

如果将 RGB 视为以 R、G 和 B 为轴的三维空间,则可以将“接近颜色”定义为围绕颜色的立方体或球体,并返回其中的所有颜色(或检查给定颜色)如果足够接近)。其公式非常简单:

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)

使用球体而不是立方体是“正确”的方法,但对于小球体来说不会有太大区别,而且立方体内部的颜色更容易计算。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文