Use set for O(1) palette color deduplication

This commit is contained in:
Jared Miller 2026-02-12 21:10:39 -05:00
parent 4619444ed1
commit 536dce6a98
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C

View file

@ -72,6 +72,7 @@ class Palette:
# scan image L->R T->B for unique colors, store em as tuples
# color 0 is always fully transparent
self.colors = [(0, 0, 0, 0)]
seen = {(0, 0, 0, 0)}
# determine lightest and darkest colors in palette for defaults
lightest = 0
darkest = 255 * 3 + 1
@ -82,7 +83,8 @@ class Palette:
if len(self.colors) >= MAX_COLORS:
break
color = src_img.getpixel((x, y))
if color not in self.colors:
if color not in seen:
seen.add(color)
self.colors.append(color)
# is this lightest/darkest unique color so far? save index
luminosity = color[0] * 0.21 + color[1] * 0.72 + color[2] * 0.07