'webcolors' has no attribute 'css3_hex_to_names'
Asked Answered
G

2

5

I am using this Function

def closest_colour(requested_colour):
    min_colours = {}
    for key, name in webcolors.css3_hex_to_names.items():
        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())]

I am using Webcolor 1.11.1 Python 3.8.8 and I am getting this Error in line

for key, name in webcolors.css21_hex_to_names.items():

AttributeError: module 'webcolors' has no attribute 'css21_hex_to_names'

Gallia answered 3/2, 2022 at 7:8 Comment(0)
G
10

Luckily, I found the solution to this. Instead of webcolors.css21_hex_to_names I used this webcolors.CSS3_HEX_TO_NAMES. CSS3_HEX_TO_NAMES in Capitals

Gallia answered 3/2, 2022 at 11:50 Comment(0)
P
2

I'm using webcolors version == '24.8.0' in Python 3.10 where it had changes and CSS3_HEX_TO_NAMES is not accessible anymore.

Here is an updated code where it replaced CSS3_HEX_TO_NAMES to webcolors.names("css3")

import webcolors

def closest_colour(requested_colour):
    min_colours = {}
    for name in webcolors.names("css3"):
        r_c, g_c, b_c = webcolors.name_to_rgb(name)
        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())]
Printmaker answered 14/8 at 7:0 Comment(1)
Same for Python 3.8 and Python 3.9Lilybelle

© 2022 - 2024 — McMap. All rights reserved.