Convert getcolor input to RGBA if that is the palette mode

This commit is contained in:
Andrew Murray 2021-06-23 19:23:34 +10:00
parent 4d36feefc7
commit 3b9792ab74
2 changed files with 7 additions and 1 deletions

View File

@ -23,8 +23,12 @@ def test_getcolor():
test_map = {}
for i in range(256):
test_map[palette.getcolor((i, i, i))] = i
assert len(test_map) == 256
# Colors can be converted between RGB and RGBA
rgba_palette = ImagePalette.ImagePalette("RGBA")
assert rgba_palette.getcolor((0, 0, 0)) == rgba_palette.getcolor((0, 0, 0, 255))
with pytest.raises(ValueError):
palette.getcolor((1, 2, 3))

View File

@ -105,6 +105,8 @@ 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,)
try:
return self.colors[color]
except KeyError as e: