Set colors

This commit is contained in:
Andrew Murray 2021-06-23 19:16:04 +10:00
parent 70ef50cf72
commit dc89f0eaa9
2 changed files with 21 additions and 7 deletions

View File

@ -7,7 +7,9 @@ from .helper import assert_image_equal_tofile
def test_sanity():
ImagePalette.ImagePalette("RGB", list(range(256)) * 3)
palette = ImagePalette.ImagePalette("RGB", list(range(256)) * 3)
assert len(palette.colors) == 256
with pytest.raises(ValueError):
ImagePalette.ImagePalette("RGB", list(range(256)) * 2)

View File

@ -40,13 +40,26 @@ class ImagePalette:
self.mode = mode
self.rawmode = None # if set, palette contains raw data
self.palette = palette or bytearray(range(256)) * len(self.mode)
self.colors = {}
self.dirty = None
if (size == 0 and len(self.mode) * 256 != len(self.palette)) or (
size != 0 and size != len(self.palette)
):
raise ValueError("wrong palette size")
@property
def palette(self):
return self._palette
@palette.setter
def palette(self, palette):
self._palette = palette
mode_len = len(self.mode)
self.colors = {
tuple(self.palette[i : i + mode_len]): i // mode_len
for i in range(0, len(self.palette), mode_len)
}
def copy(self):
new = ImagePalette()
@ -54,7 +67,6 @@ class ImagePalette:
new.rawmode = self.rawmode
if self.palette is not None:
new.palette = self.palette[:]
new.colors = self.colors.copy()
new.dirty = self.dirty
return new
@ -100,14 +112,14 @@ class ImagePalette:
except KeyError as e:
# allocate new color slot
if isinstance(self.palette, bytes):
self.palette = bytearray(self.palette)
self._palette = bytearray(self.palette)
index = len(self.colors)
if index >= 256:
raise ValueError("cannot allocate more than 256 colors") from e
self.colors[color] = index
self.palette[index] = color[0]
self.palette[index + 256] = color[1]
self.palette[index + 512] = color[2]
self._palette[index] = color[0]
self._palette[index + 256] = color[1]
self._palette[index + 512] = color[2]
self.dirty = 1
return index
else: