0

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]

4u Don
  • 26
  • 2
  • Your question looks interesting, but I don't understand what you are looking for... How `low_color` and `high_color` related to the question? What are `c[0]` and c[1] in `ranges(c[0],c[1])`? The sample method `findNearest(rgb)` returns the "nearest rgbvalues base on 9 rgb color". I am confused. – Rotem Mar 20 '22 at 21:05
  • this is the flow, i have rgb base on x,y and then i have to check wether that pixel belongs to a color range. In between the LowRGBValue and HighRGBBalue. if it is in between that, then that color index is in that array index. – 4u Don Mar 23 '22 at 04:40
  • **1.** Measure the "distance" to all colors in `low_color`, and save the minimum as "low_dist". **2.** Measure the "distance" to all colors in `high_color`, and save the minimum as "high_dist". **3.** If `low_dist` < `high_dist`, the index is `0` and if `low_dist` > `high_dist` the index is `1`. – Rotem Mar 23 '22 at 17:25
  • Why are you writing "rgb=(93, 30, 38) it should select the 2nd index (**brown**)" what is the meaning of `brown`? Doesn't the output supposed to be only `0` or `1`. Why "brown" and not `1`? – Rotem Mar 23 '22 at 17:29

0 Answers0