我想知道基于 9 RGB 颜色范围的最接近或最接近的 RGB 值

发布于 2025-01-15 06:06:59 字数 1616 浏览 1 评论 0原文

我知道有类似的问题。 查找图像中每个像素最接近的 RGB 颜色

但是我的是我想知道基于颜色的范围。

#the brighter values of colors
low_color = [(0, 0, 0),         #black
                (0, 90, 10),      #brown
                (255, 0, 0),       #red
                (211, 104, 62),     #orange
                (255, 255, 0),     #yellow
                (0, 255, 0),       #green
                (0, 0, 255),       #blue
                (200, 0, 255),     #violet
                (128, 128, 128),   #gray
                (255, 255, 255)]   #white

#the darker values of colors
high_color = [(179, 255, 93),      #black
              (15, 250, 100),      #brown
              (204, 0, 0),       #red
              (199, 90, 57),     #orange
              (255, 204, 102),  #yellow
              (6, 85, 28),      #green
              (40, 73, 86),      #blue
              (110, 0, 51),      #violet
              (73, 65, 62),      #gray
              (250, 250, 250)]   #white

我如何仅使用 rgb 值检查这些范围(c[0],c[1])的颜色基础。 我想知道颜色最匹配的索引。

前任。如果寻找的颜色是 rgb=(93, 30, 38) 它应该选择第二个索引(棕色)

def findNearest(rgb):
    dist = ((low_color[0][0]-rgb[0])*0.3)**2 + ((low_color[0][1]-rgb[1])*0.59)**2 + ((low_color[0][2]-rgb[2])*0.11)**2
    index = 0
    for i in range(1,len(high_color)):
        d = ((low_color[i][0]-rgb[0])*0.3)**2 + ((low_color[i][1]-rgb[1])*0.59)**2 + ((low_color[i][2]-rgb[2])*0.11)**2
        if d < dist:
            dist = d
            index = i
    return low_color[index]

i know there are similar question like this one. Find closest RGB color for every pixel in image

But mine is i want to know base on the range of the color.

#the brighter values of colors
low_color = [(0, 0, 0),         #black
                (0, 90, 10),      #brown
                (255, 0, 0),       #red
                (211, 104, 62),     #orange
                (255, 255, 0),     #yellow
                (0, 255, 0),       #green
                (0, 0, 255),       #blue
                (200, 0, 255),     #violet
                (128, 128, 128),   #gray
                (255, 255, 255)]   #white

#the darker values of colors
high_color = [(179, 255, 93),      #black
              (15, 250, 100),      #brown
              (204, 0, 0),       #red
              (199, 90, 57),     #orange
              (255, 204, 102),  #yellow
              (6, 85, 28),      #green
              (40, 73, 86),      #blue
              (110, 0, 51),      #violet
              (73, 65, 62),      #gray
              (250, 250, 250)]   #white

how would i check the color base on these ranges(c[0],c[1]) using rgbvalues only.
I would like to know the index where the color is best matched of.

ex. if the sought out color is rgb=(93, 30, 38) it should select the 2nd index(brown)

def findNearest(rgb):
    dist = ((low_color[0][0]-rgb[0])*0.3)**2 + ((low_color[0][1]-rgb[1])*0.59)**2 + ((low_color[0][2]-rgb[2])*0.11)**2
    index = 0
    for i in range(1,len(high_color)):
        d = ((low_color[i][0]-rgb[0])*0.3)**2 + ((low_color[i][1]-rgb[1])*0.59)**2 + ((low_color[i][2]-rgb[2])*0.11)**2
        if d < dist:
            dist = d
            index = i
    return low_color[index]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文