Convert opaque RGBA color to RGB if that is the palette mode

This commit is contained in:
Andrew Murray 2021-06-23 19:26:53 +10:00
parent 3b9792ab74
commit f3451aefc6
2 changed files with 8 additions and 2 deletions

View File

@ -29,6 +29,8 @@ def test_getcolor():
rgba_palette = ImagePalette.ImagePalette("RGBA")
assert rgba_palette.getcolor((0, 0, 0)) == rgba_palette.getcolor((0, 0, 0, 255))
assert palette.getcolor((0, 0, 0)) == palette.getcolor((0, 0, 0, 255))
with pytest.raises(ValueError):
palette.getcolor((1, 2, 3))

View File

@ -105,8 +105,12 @@ class ImagePalette:
if self.rawmode:
raise ValueError("palette contains raw palette data")
if isinstance(color, tuple):
if self.mode == "RGBA" and len(color) == 3:
color += (255,)
if self.mode == "RGB":
if len(color) == 4 and color[3] == 255:
color = color[:3]
elif self.mode == "RGBA":
if len(color) == 3:
color += (255,)
try:
return self.colors[color]
except KeyError as e: