'Why is the webcolors library giving me an atribute error when the function is specified in the documentation
I am trying to create a color mixer-calculator type thing that will tell you the color name based on the RGB code you input. As in the webcolor database there are no 256^3 color names (niether in real life, I think) I included aproximations to the colors that didn't have names to the nearest similar color. The problem pops out when using the
webcolors.cc3_hex_to_name
It raises an atribute error:
AttributeError: module 'webcolors' has no attribute 'css3_hex_to_name'
This is very strange as the webcolor's documentation clearly includes this atribute: Webcolors documentation extract
Here's the whole code for reference:
import webcolors
webcolors.css3_hex_to_name
def closest_colour(requested_colour):
min_colours = {}
for key, name in webcolors.css3_hex_to_names.items(): #Error line
r_c, g_c, b_c = webcolors.hex_to_rgb(key)
rd = (r_c - requested_colour[0]) ** 2
gd = (g_c - requested_colour[1]) ** 2
bd = (b_c - requested_colour[2]) ** 2
min_colours[(rd + gd + bd)] = name
return min_colours[min(min_colours.keys())]
def get_colour_name(requested_colour):
try:
closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
except ValueError:
closest_name = closest_colour(requested_colour)
actual_name = None
return actual_name, closest_name
requested_colour = (119, 172, 152)
actual_name, closest_name = get_colour_name(requested_colour)
print ("Actual colour name:", actual_name, ", closest colour name:", closest_name)
Solution 1:[1]
try this webcolors.CSS3_HEX_TO_NAMES
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Robin XW |
