Updated error message

This commit is contained in:
Andrew Murray 2022-10-10 12:02:10 +11:00
parent 982d7c49e3
commit 0b2cef5b03
2 changed files with 12 additions and 11 deletions

View File

@ -50,11 +50,12 @@ def test_getcolor():
palette.getcolor("unknown") palette.getcolor("unknown")
def test_getcolor_raises_on_incompatible_color(): def test_getcolor_rgba_color_rgb_palette():
palette = ImagePalette.ImagePalette(mode="RGB") palette = ImagePalette.ImagePalette("RGB")
# Opaque RGBA colors should work
palette.getcolor((0, 0, 0, 255)) # Opaque RGBA colors are converted
assert palette.getcolor((0, 0, 0)) == palette.getcolor((0, 0, 0, 255)) assert palette.getcolor((0, 0, 0, 255)) == palette.getcolor((0, 0, 0))
with pytest.raises(ValueError): with pytest.raises(ValueError):
palette.getcolor((0, 0, 0, 128)) palette.getcolor((0, 0, 0, 128))

View File

@ -114,13 +114,13 @@ class ImagePalette:
if self.rawmode: if self.rawmode:
raise ValueError("palette contains raw palette data") raise ValueError("palette contains raw palette data")
if isinstance(color, tuple): if isinstance(color, tuple):
if self.mode == "RGB" and len(color) == 4: if self.mode == "RGB":
if color[3] == 255: if len(color) == 4:
color = color[:3] if color[3] != 255:
else:
raise ValueError( raise ValueError(
"RGB ImagePalette can't handle non-opaque RGBA colors" "cannot add non-opaque RGBA color to RGB palette"
) )
color = color[:3]
elif self.mode == "RGBA": elif self.mode == "RGBA":
if len(color) == 3: if len(color) == 3:
color += (255,) color += (255,)