Add support for color names

see https://pillow.readthedocs.io/en/stable/_modules/PIL/ImageColor.html for supported colors
This commit is contained in:
missionfloyd 2023-04-08 21:01:44 -06:00 committed by GitHub
parent b6d1bfdc8b
commit 23a93b40f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -186,7 +186,7 @@ class QuantizePalette:
raise ValueError("Palette is empty")
def parse_palette(color_str):
if re.match(r'#[a-fA-F0-9]{6}', color_str):
if re.match(r'#[a-fA-F0-9]{6}', color_str) or color_str.lower() in ImageColor.colormap:
return ImageColor.getrgb(color_str)
color_rgb = re.match(r'\(?(\d{1,3}),(\d{1,3}),(\d{1,3})\)?', color_str)
@ -197,7 +197,7 @@ class QuantizePalette:
pal_img = Image.new('P', (1, 1))
pal_colors = palette.replace(" ", "")
pal_colors = re.findall(r'#[a-fA-F0-9]{6}|\(?\d{1,3},\d{1,3},\d{1,3}\)?', pal_colors)
pal_colors = re.findall(fr'#[a-fA-F0-9]{{6}}|\(?\d{{1,3}},\d{{1,3}},\d{{1,3}}\)?|{"|".join(ImageColor.colormap.keys())}', pal_colors)
pal_colors = list(itertools.chain.from_iterable(map(parse_palette, pal_colors)))
pal_img.putpalette(pal_colors)