Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import webcolors as wc
- def get_approx_color(hex_color):
- orig = wc.hex_to_rgb(hex_color)
- similarity = {}
- for hex_code, color_name in wc.CSS3_HEX_TO_NAMES.items():
- approx = wc.hex_to_rgb(hex_code)
- similarity[color_name] = sum(np.subtract(orig, approx) ** 2)
- return min(similarity, key=similarity.get)
- def get_color_name(hex_color):
- try:
- return wc.hex_to_name(hex_color)
- except ValueError:
- return get_approx_color(hex_color)
- # this is the RGB value you want to get the corresponding color name of
- rgb = (100, 104, 195)
- hex_ = wc.rgb_to_hex(tuple(rgb))
- color_name_ = get_color_name(hex_)
- print(color_name_) # returns slateblue
Add Comment
Please, Sign In to add comment