Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Find the closest color to a color, from giving list of colors

Tags:

python

rgb

I have a list of 20 colors, each is like this (0,0,0)(rgb) but with different values, and i need to find the closest to the color i am giving, for example (200, 191, 231). problem is i am not sure how i should check what color is the closes, and how am i suppose to set all these color values, in a list? in an array?

I've been thinking maybe add all the color for exmaple (1,2,3) = 4 an then find the closest but i am not sure if its a good idea..

Here's a list of the colors:

#(0, 0, 0) - Black
#(127, 127, 127) - Gray
#(136, 0, 21) - Bordeaux
#(237, 28, 36) - red
#(255, 127, 39) - orange
#(255, 242, 0) - yellow
#(34, 177, 76) - green
#(203, 228, 253) - blue
#(0, 162, 232) - dark blue
#(63, 72, 204) - purple
#(255, 255, 255) - white
#(195, 195, 195) - light gray
#(185, 122, 87) - light brown
#(255, 174, 201) - light pink
#(255, 201, 14) - dark yellow
#(239, 228, 176) - light yellow
#(181, 230, 29) - light green
#(153, 217, 234) - light blue
#(112, 146, 190) - dark blue
#(200, 191, 231) - light purple

And here is the function:

def paint(pixel):
  r,g,b,a = pix[x,y]
  print(str(r) + ' '+  str(g) + ' ' + str(b))
  sleep(0.20)

If you come up with a good solution or have any question please replay thank you for your help!

like image 704
yarin Cohen Avatar asked Jan 17 '19 18:01

yarin Cohen


2 Answers

Fast, efficient and clean solution

Lets say we have:

list_of_colors = [[255,0,0],[150,33,77],[75,99,23],[45,88,250],[250,0,255]]

For fast processing use numpy and transform into numpy array

import numpy as np

desired color

color = [155,155,155]

Complete code

import numpy as np

list_of_colors = [[255,0,0],[150,33,77],[75,99,23],[45,88,250],[250,0,255]]
color = [155,155,155]

def closest(colors,color):
    colors = np.array(colors)
    color = np.array(color)
    distances = np.sqrt(np.sum((colors-color)**2,axis=1))
    index_of_smallest = np.where(distances==np.amin(distances))
    smallest_distance = colors[index_of_smallest]
    return smallest_distance 

closest_color = closest(list_of_colors,color)
print(closest_color )

This algorithm is without loops and is super fast as it uses numpy

like image 153
Martin Avatar answered Nov 20 '22 12:11

Martin


You want to find the sum of the absolute difference between the red, green and blue numbers and choose the smallest one.

from math import sqrt

COLORS = (
    (181, 230, 99),
    (23, 186, 241),
    (99, 23, 153),
    (231, 99, 29),
)

def closest_color(rgb):
    r, g, b = rgb
    color_diffs = []
    for color in COLORS:
        cr, cg, cb = color
        color_diff = sqrt((r - cr)**2 + (g - cg)**2 + (b - cb)**2)
        color_diffs.append((color_diff, color))
    return min(color_diffs)[1]

closest_color((12, 34, 156))
# => (99, 23, 153)

closest_color((23, 145, 234))
# => (23, 186, 241)

EDIT: Improved code and used Euclidian distance calculation Sven mentioned above instead of basic diff sum.

like image 31
mVChr Avatar answered Nov 20 '22 12:11

mVChr