Mat4297

Color name fuonder

Apr 15th, 2022 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import numpy as np
  2. import webcolors as wc
  3.  
  4.  
  5. def get_approx_color(hex_color):
  6.     orig = wc.hex_to_rgb(hex_color)
  7.     similarity = {}
  8.     for hex_code, color_name in wc.CSS3_HEX_TO_NAMES.items():
  9.         approx = wc.hex_to_rgb(hex_code)
  10.         similarity[color_name] = sum(np.subtract(orig, approx) ** 2)
  11.     return min(similarity, key=similarity.get)
  12.  
  13. def get_color_name(hex_color):
  14.     try:
  15.         return wc.hex_to_name(hex_color)
  16.     except ValueError:
  17.         return get_approx_color(hex_color)
  18.  
  19. # this is the RGB value you want to get the corresponding color name of
  20. rgb = (100, 104, 195)
  21. hex_ = wc.rgb_to_hex(tuple(rgb))
  22. color_name_ = get_color_name(hex_)
  23.  
  24. print(color_name_)  # returns slateblue
Add Comment
Please, Sign In to add comment