Use set for O(1) palette color deduplication
This commit is contained in:
parent
55cfa7963c
commit
44e4219b04
1 changed files with 3 additions and 1 deletions
|
|
@ -72,6 +72,7 @@ class Palette:
|
||||||
# scan image L->R T->B for unique colors, store em as tuples
|
# scan image L->R T->B for unique colors, store em as tuples
|
||||||
# color 0 is always fully transparent
|
# color 0 is always fully transparent
|
||||||
self.colors = [(0, 0, 0, 0)]
|
self.colors = [(0, 0, 0, 0)]
|
||||||
|
seen = {(0, 0, 0, 0)}
|
||||||
# determine lightest and darkest colors in palette for defaults
|
# determine lightest and darkest colors in palette for defaults
|
||||||
lightest = 0
|
lightest = 0
|
||||||
darkest = 255 * 3 + 1
|
darkest = 255 * 3 + 1
|
||||||
|
|
@ -82,7 +83,8 @@ class Palette:
|
||||||
if len(self.colors) >= MAX_COLORS:
|
if len(self.colors) >= MAX_COLORS:
|
||||||
break
|
break
|
||||||
color = src_img.getpixel((x, y))
|
color = src_img.getpixel((x, y))
|
||||||
if color not in self.colors:
|
if color not in seen:
|
||||||
|
seen.add(color)
|
||||||
self.colors.append(color)
|
self.colors.append(color)
|
||||||
# is this lightest/darkest unique color so far? save index
|
# is this lightest/darkest unique color so far? save index
|
||||||
luminosity = color[0] * 0.21 + color[1] * 0.72 + color[2] * 0.07
|
luminosity = color[0] * 0.21 + color[1] * 0.72 + color[2] * 0.07
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue