Skip to content

Instantly share code, notes, and snippets.

@SuzanaK
Created March 14, 2013 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuzanaK/5162620 to your computer and use it in GitHub Desktop.
Save SuzanaK/5162620 to your computer and use it in GitHub Desktop.
get closest colorname for an RGB color or a list of image files
import colorific
fixed_colors = {
"red" : (255, 0, 0),
'green' : (0, 255, 0),
'blue' : (0, 0, 255),
'orange' : (255, 127, 0),
'pink' : (255, 0, 127),
'violet' : (127, 0, 255),
'yellow' : (255, 255, 0),
'rose' : (255, 127, 127),
'turquoise' : (0, 255, 255),
'blue-green' : (0, 255, 127),
}
def is_grey(r, g, b):
return (r + g + b) > 150 and (abs(r - g) + abs(r - b) + abs(g - b)) < 30
def is_white(r, g, b):
return (r + g + b) > (3 * 180)
def get_distance((r1, g1, b1), (r2, g2, b2)):
return abs(r1-r2) + abs(g1-g2) + abs(b1-b2)
def get_closest_color(r, g, b):
color = (r, g, b)
if is_white(r, g, b):
return ""
if is_grey(r, g, b):
return ""
distances = []
distance_colors = []
for c in fixed_colors.keys():
comp_color = fixed_colors.get(c)
dis = get_distance (color, comp_color)
distances.append(dis)
distance_colors.append(c)
index = distances.index(min(distances))
nearest_color = distance_colors[index]
return nearest_color
def get_color_names(filenames):
allcolors = []
for f in filenames:
p = colorific.extract_colors("images/images/" + f)
colors = [c.value for c in p.colors]
allcolors.append([f] + colors)
return allcolors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment