Last active
December 30, 2019 00:19
-
-
Save zperk13/8e35850216384f16974bf3488259cd6b to your computer and use it in GitHub Desktop.
Calculates the distance between colors, treating the rgb value as xyz in a 3 dimensional space, then finds the color with the shortest distance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def abs(x): | |
if x < 0: | |
return 0 - x | |
else: | |
return x | |
def calculate_distance(rgb1, rgb2): | |
r1 = rgb1[0] | |
b1 = rgb1[1] | |
g1 = rgb1[2] | |
r2 = rgb2[0] | |
b2 = rgb2[1] | |
g2 = rgb2[2] | |
return abs(((r2 - r1) ** 2 + (g2 - g1) ** 2 + (b2 - b1) ** 2) ** 0.5) | |
def color_namer(rgb, color_set='html'): | |
color_sets = {'html': | |
{'indianred': (205, 92, 92), | |
'lightcoral': (240, 128, 128), | |
'salmon': (250, 128, 122), | |
'darksalmon': (233, 150, 122), | |
'lightsalmon': (255, 160, 122), | |
'crismon': (220, 20, 60), | |
'red': (255, 0, 0), | |
'firebrick': (178, 34, 34), | |
'darkred': (139, 0, 0), | |
'pink': (255, 192, 203), | |
'lightpink': (255, 182, 203), | |
'hotpink': (255, 182, 193), | |
'deeppink': (255, 20, 147), | |
'mediumvioletred': (199, 21, 133), | |
'palevioletred': (219, 112, 147), | |
'coral': (255, 127, 80), | |
'tomato': (255, 99, 71), | |
'orangered': (255, 69, 0), | |
'darkorange': (255, 140, 0), | |
'orange': (255, 165, 0), | |
'gold': (255, 215, 0), | |
'yellow': (255, 255, 0), | |
'lightyellow': (255, 255, 224), | |
'lemonchiffon': (255, 250, 205), | |
'lightgoldenrodyyellow': (250, 250, 210), | |
'papayawhip': (255, 239, 213), | |
'moccasin': (255, 228, 181), | |
'peachpuff': (255, 218, 185), | |
'palegoldenrod': (238, 232, 170), | |
'khaki': (240, 230, 140), | |
'darkhaki': (189, 183, 107), | |
'lavender': (230, 230, 250), | |
'thistle': (216, 191, 216), | |
'plum': (221, 160, 221), | |
'violet': (238, 130, 238), | |
'orchid': (218, 112, 214), | |
'fuchia': (255, 0, 255), | |
'magenta': (255, 0, 255), | |
'mediumorchid': (186, 85, 211), | |
'mediumpurple': (147, 112, 219), | |
'rebeccapurple': (102, 51, 153), | |
'blueviolet': (138, 43, 226), | |
'darkviolet': (148, 0, 211), | |
'darkorchid': (153, 50, 204), | |
'darkmagenta': (139, 0, 139), | |
'purple': (128, 0, 128), | |
'indigo': (75, 0, 130), | |
'slateblue': (106, 90, 205), | |
'darkslateblue': (71, 61, 139), | |
'mediumslateblue': (123, 104, 238), | |
'greenyellow': (173, 255, 47), | |
'chartreuse': (127, 255, 0), | |
'lawngreen': (124, 252, 0), | |
'lime': (0, 255, 0), | |
'limegreen': (50, 205, 50), | |
'palegreen': (152, 251, 152), | |
'lightgreen': (144, 238, 144), | |
'mediumspringgreen': (0, 250, 154), | |
'springgreen': (0, 255, 127), | |
'mediumseagreen': (60, 179, 113), | |
'seagreen': (46, 139, 87), | |
'forestgreen': (34, 139, 23), | |
'green': (0, 128, 0), | |
'darkgreen': (0, 100, 0), | |
'yellowgreen': (154, 205, 50), | |
'olivedrab': (107, 142, 35), | |
'olive': (128, 128, 0), | |
'darkolivegreen': (85, 107, 47), | |
'mediumaquamarine': (102, 205, 170), | |
'darkseagreen': (143, 188, 139), | |
'lightseagreen': (32, 178, 170), | |
'darkcyan': (0, 139, 139), | |
'teal': (0, 128, 128), | |
'aqua': (0, 255, 255), | |
'cyan': (0, 255, 255), | |
'lightcyan': (224, 255, 255), | |
'paleturquoise': (175, 238, 238), | |
'aquamarine': (127, 255, 212), | |
'turquoise': (64, 224, 208), | |
'mediumturquoise': (72, 209, 204), | |
'darkturquoise': (0, 206, 209), | |
'cadetblue': (95, 158, 160), | |
'steelblue': (70, 130, 180), | |
'lightsteelblue': (176, 196, 222), | |
'powerderblue': (176, 224, 230), | |
'lightblue': (173, 216, 230), | |
'skyblue': (135, 206, 235), | |
'lightskyblue': (135, 206, 250), | |
'deepskyblue': (0, 191, 255), | |
'dodgerblue': (30, 144, 255), | |
'cornflowerblue': (100, 149, 237), | |
'royalblue': (65, 105, 255), | |
'blue': (0, 0, 255), | |
'mediumblue': (0, 0, 205), | |
'darkblue': (0, 0, 139), | |
'navy': (0, 0, 128), | |
'midnightblue': (25, 25, 112), | |
'cornsilk': (255, 248, 220), | |
'blanchedalmond': (255, 235, 205), | |
'bisque': (255, 228, 196), | |
'navajowhite': (255, 222, 173), | |
'wheat': (245, 22, 179), | |
'burlywood': (222, 184, 135), | |
'tan': (210, 180, 140), | |
'rosybrown': (188, 143, 143), | |
'sandybrown': (244, 164, 96), | |
'goldenrod': (218, 165, 32), | |
'darkgoldenrod': (184, 134, 11), | |
'peru': (205, 133, 63), | |
'choclate': (210, 105, 30), | |
'saddlebrown': (139, 69, 19), | |
'sienna': (160, 82, 45), | |
'brown': (165, 42, 42), | |
'maroon': (128, 0, 0), | |
'white': (255, 255, 255), | |
'snow': (255, 250, 250), | |
'honeydew': (240, 255, 240), | |
'mintcream': (245, 255, 250), | |
'azure': (240, 255, 255), | |
'aliceblue': (240, 248, 255), | |
'ghostwhite': (248, 248, 255), | |
'whitesmoke': (245, 245, 245), | |
'seashell': (255, 245, 238), | |
'beige': (245, 245, 220), | |
'oldlace': (253, 245, 230), | |
'floralwhite': (255, 250, 240), | |
'ivory': (255, 255, 240), | |
'antiquewhite': (250, 235, 215), | |
'linen': (250, 240, 230), | |
'lavenderblush': (255, 240, 245), | |
'mistyrose': (255, 228, 225), | |
'gainsboro': (220, 220, 220), | |
'lightgray': (211, 211, 211), | |
'silver': (192, 192, 192), | |
'darkgray': (169, 169, 169), | |
'gray': (128, 128, 128), | |
'dimgray': (105, 105, 105), | |
'lightslategray': (119, 136, 153), | |
'slategray': (112, 128, 144), | |
'darkslategray': (47, 79, 79), | |
'black': (0, 0, 0) | |
}, | |
'turtle': | |
{'alice blue': (240, 248, 255), | |
'antique white': (250, 235, 215), | |
'aquamarine': (127, 255, 212), | |
'azure': (240, 255, 255), | |
'beige': (245, 245, 220), | |
'bisque': (255, 228, 196), | |
'black': (0, 0, 0), | |
'blanched almond': (255, 235, 205), | |
'blue': (0, 0, 255), | |
'blue violet': (138, 43, 226), | |
'brown': (165, 42, 42), | |
'burlywood': (222, 184, 135), | |
'cadet blue': (95, 158, 160), | |
'chartreuse': (127, 255, 0), | |
'chocolate': (210, 105, 30), | |
'coral': (255, 127, 80), | |
'cornflower blue': (100, 149, 237), | |
'cornsilk': (255, 248, 220), | |
'crimson': (220, 20, 60), | |
'cyan': (0, 255, 255), | |
'dark blue': (0, 0, 139), | |
'dark cyan': (0, 139, 139), | |
'dark goldenrod': (184, 134, 11), | |
'dark gray': (169, 169, 169), | |
'dark green': (0, 100, 0), | |
'dark khaki': (189, 183, 107), | |
'dark magenta': (139, 0, 139), | |
'dark olive green': (85, 107, 47), | |
'dark orange': (255, 140, 0), | |
'dark orchid': (153, 50, 204), | |
'dark red': (139, 0, 0), | |
'dark salmon': (233, 150, 122), | |
'dark sea green': (143, 188, 143), | |
'dark slate blue': (72, 61, 139), | |
'dark slate gray': (47, 79, 79), | |
'dark turquoise': (0, 206, 209), | |
'dark violet': (148, 0, 211), | |
'deep pink': (255, 20, 147), | |
'deep sky blue': (0, 191, 255), | |
'dim gray': (105, 105, 105), | |
'dodger blue': (30, 144, 255), | |
'firebrick': (178, 34, 34), | |
'floral white': (255, 250, 240), | |
'forest green': (34, 139, 34), | |
'gainsboro': (220, 220, 220), | |
'ghost white': (248, 248, 255), | |
'gold': (255, 215, 0), | |
'goldenrod': (218, 165, 32), | |
'gray': (128, 128, 128), | |
'green': (0, 128, 0), | |
'green yellow': (173, 255, 47), | |
'honeydew': (240, 255, 240), | |
'hot pink': (255, 105, 180), | |
'indian red': (205, 92, 92), | |
'indigo': (75, 0, 130), | |
'ivory': (255, 255, 240), | |
'khaki': (240, 230, 140), | |
'lavender': (230, 230, 250), | |
'lavender blush': (255, 240, 245), | |
'lawn green': (124, 252, 0), | |
'lemon chiffon': (255, 250, 205), | |
'light blue': (173, 216, 230), | |
'light coral': (240, 128, 128), | |
'light cyan': (224, 255, 255), | |
'light goldenrod yellow': (250, 250, 210), | |
'light gray': (211, 211, 211), | |
'light green': (144, 238, 144), | |
'light pink': (255, 182, 193), | |
'light salmon': (255, 160, 122), | |
'light sea green': (32, 178, 170), | |
'light sky blue': (135, 206, 250), | |
'light slate blue': (132, 112, 255), | |
'light slate gray': (119, 136, 153), | |
'light steel blue': (176, 196, 222), | |
'light yellow': (255, 255, 224), | |
'lime': (0, 255, 0), | |
'lime green': (50, 205, 50), | |
'linen': (250, 240, 230), | |
'magenta': (255, 0, 255), | |
'maroon': (128, 0, 0), | |
'medium aquamarine': (102, 205, 170), | |
'medium blue': (0, 0, 205), | |
'medium orchid': (186, 85, 211), | |
'medium purple': (147, 112, 219), | |
'medium sea green': (60, 179, 113), | |
'medium slate blue': (123, 104, 238), | |
'medium spring green': (0, 250, 154), | |
'medium turquoise': (72, 209, 204), | |
'medium violet red': (199, 21, 133), | |
'midnight blue': (25, 25, 112), | |
'mint cream': (245, 255, 250), | |
'misty rose': (255, 228, 225), | |
'moccasin': (255, 228, 181), | |
'navajo white': (255, 222, 173), | |
'navy': (0, 0, 128), | |
'old lace': (253, 245, 230), | |
'olive': (128, 128, 0), | |
'olive drab': (107, 142, 35), | |
'orange': (255, 165, 0), | |
'orange red': (255, 69, 0), | |
'orchid': (218, 112, 214), | |
'pale goldenrod': (238, 232, 170), | |
'pale green': (152, 251, 152), | |
'pale turquoise': (175, 238, 238), | |
'pale violet red': (219, 112, 147), | |
'papaya whip': (255, 239, 213), | |
'peach puff': (255, 218, 185), | |
'peru': (205, 133, 63), | |
'pink': (255, 192, 203), | |
'plum': (221, 160, 221), | |
'powder blue': (176, 224, 230), | |
'purple': (128, 0, 128), | |
'rebecca purple': (102, 51, 153), | |
'red': (255, 0, 0), | |
'rosy brown': (188, 143, 143), | |
'royal blue': (65, 105, 225), | |
'saddle brown': (139, 69, 19), | |
'salmon': (250, 128, 114), | |
'sandy brown': (244, 164, 96), | |
'sea green': (46, 139, 87), | |
'seashell': (255, 245, 238), | |
'sienna': (160, 82, 45), | |
'silver': (192, 192, 192), | |
'sky blue': (135, 206, 235), | |
'slate blue': (106, 90, 205), | |
'slate gray': (112, 128, 144), | |
'snow': (255, 250, 250), | |
'spring green': (0, 255, 127), | |
'steel blue': (70, 130, 180), | |
'tan': (210, 180, 140), | |
'teal': (0, 128, 128), | |
'thistle': (216, 191, 216), | |
'tomato': (255, 99, 71), | |
'turquoise': (64, 224, 208), | |
'violet': (238, 130, 238), | |
'violet red': (204, 50, 153), | |
'wheat': (245, 222, 179), | |
'white': (255, 255, 255), | |
'white smoke': (245, 245, 245), | |
'yellow': (255, 255, 0), | |
'yellow green': (154, 205, 50)}} | |
if isinstance(color_set, str): | |
color_set = color_set.lower() | |
color_set = color_sets[color_set] | |
elif isinstance(color_set, dict): | |
pass | |
else: | |
raise TypeError('color_set must be a string identifying a color set or a color set dictionary') | |
keys = list(color_set.keys()) | |
best_distance = calculate_distance(rgb, color_set[keys[0]]) | |
for key in keys[1:]: | |
color = color_set[key] | |
color_distance = calculate_distance(rgb, color) | |
if color_distance < best_distance: | |
best_distance = color_distance | |
result = [rgb, best_distance] | |
for key in keys: | |
color = color_set[key] | |
color_distance = calculate_distance(rgb, color) | |
if color_distance == best_distance: | |
result.append([key, color]) | |
return result | |
def print_color_namer(rgb, color_set='html'): | |
result = color_namer(rgb, color_set) | |
print(f'Your color: {result[0]}\nBest Color Distance: {result[1]}\n') | |
s = '' | |
if len(result) > 3: | |
s = 's' | |
print(f'Color{s} found:') | |
for found in result[2:]: | |
print(f'{found[0]} {found[1]}') | |
if __name__ == '__main__': | |
print_color_namer((100, 149, 237), color_set='turtle') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment