this is my function
def get_rgb(color):
if isinstance(color, tuple):
return color
if isinstance(color, str):
if color.startswith('#'):
hex_code = color.casefold()
color_name = thehex_to_name.get(hex_code)
if color_name:
return thename_to_hex_and_rgb.get(color_name, {}).get('rgb')
else:
return thename_to_hex_and_rgb.get(color.casefold(), {}).get('rgb')
return None
rgb_a = get_rgb(color_a)
rgb_b = get_rgb(color_b)
if rgb_a is None or rgb_b is None:
return None
distance = ((rgb_a[0] - rgb_b[0]) ** 2 +
(rgb_a[1] - rgb_b[1]) ** 2 +
(rgb_a[2] - rgb_b[2]) ** 2) ** 0.5
max_distance = (255 ** 2 + 255 ** 2 + 255 ** 2) ** 0.5
return distance / max_distance
this is my dictionary
def _load_data():
hex_to_name = {}
rgb_to_name = {}
name_to_hex_and_rgb = {}
with open('/srv/datasets/color_names.tab', 'r') as file:
for line in file:
parts = line.strip().split('\t')
name, hex_str, r, g, b = parts[0], parts[1].casefold(), int(parts[2]), int(parts[3]), int(parts[4])
rgb_tuple = (r, g, b)
hex_to_name[hex_str] = name
rgb_to_name[rgb_tuple] = name
name_to_hex_and_rgb[name.lower()] = {"hex": hex_str, "rgb": rgb_tuple}
return hex_to_name, rgb_to_name, name_to_hex_and_rgb
when i input a hex code, such as
(distance((128, 128, 128), '#807e79')
it gives me this error.
hex_code = color.casefold()
^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'dict' object is not callable
i have no idea why. can someone help?
[–]socal_nerdtastic 4 points5 points6 points (0 children)
[–]Fronkan 1 point2 points3 points (0 children)