Remove StaticPalette class, redefine Palette type

StaticPalette was not really being used besides as a list wrapper, so
that is removed. Palette type is getting some use in the rest of the
codebase, so I have redefined that type as a Sequence[str], becauase
that's basically what it is.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-05-31 10:10:48 -07:00
parent 7249ab3cff
commit 28eca5e2de
2 changed files with 84 additions and 120 deletions

View File

@@ -5,45 +5,8 @@ from typing import Sequence
from .color import Color, HSLColor from .color import Color, HSLColor
class Palette(metaclass=abc.ABCMeta):
"""
A 16-color palette.
All colors must be a `colorhash.Color`.
"""
@abc.abstractmethod
def choose(self, color: int) -> Color:
"""
Chooses the given color in this palette.
"""
def __getitem__(self, color: int) -> Color:
return self.choose(color)
class StaticPalette(Palette):
"""
A static color palette with discrete colors.
"""
def __init__(self, colors: Sequence[Color]) -> None:
"""
Creates a new static color palette.
:param colors: the colors for this palette. Must be exactly 16 colors.
"""
if len(colors) != 16:
raise ValueError(f"palette must have exactly 16 colors (got {len(colors)})")
self.colors = colors
def choose(self, color: int) -> Color:
if not isinstance(color, int):
raise KeyError("palette color indices must be an integer")
return self.colors[color]
HSLRange = range | float | int | list[float | int] HSLRange = range | float | int | list[float | int]
Palette = Sequence[str]
def quantize(r: range, steps: int = 16) -> list[float]: def quantize(r: range, steps: int = 16) -> list[float]:
@@ -106,55 +69,56 @@ GRADIENT_PALETTES = {
# red, orange, yellow, green, cyan, blue, purple, magenta, pink, gray, rainbow # red, orange, yellow, green, cyan, blue, purple, magenta, pink, gray, rainbow
# #
# Also disabling yellow-light, that one just gives me a headache. It's hard to look at. # Also disabling yellow-light, that one just gives me a headache. It's hard to look at.
#
"red-light": StaticPalette(hsl_colors(0, 100, range(50, 100))), "red-light": hsl_colors(0, 100, range(50, 100)),
"red-dark": StaticPalette(hsl_colors(0, 100, range(0, 50))), "red-dark": hsl_colors(0, 100, range(0, 50)),
#
"orange-light": StaticPalette(hsl_colors(30, 100, range(50, 100))), "orange-light": hsl_colors(30, 100, range(50, 100)),
"orange-dark": StaticPalette(hsl_colors(30, 100, range(0, 50))), "orange-dark": hsl_colors(30, 100, range(0, 50)),
#
#"yellow-light": StaticPalette(hsl_colors(60, 100, range(50, 100))), # "yellow-light": hsl_colors(60, 100, range(50, 100)),
"yellow-dark": StaticPalette(hsl_colors(60, 100, range(0, 50))), "yellow-dark": hsl_colors(60, 100, range(0, 50)),
#
#"lime-light": StaticPalette(hsl_colors(90, 100, range(50, 100))), # "lime-light": hsl_colors(90, 100, range(50, 100)),
#"lime-dark": StaticPalette(hsl_colors(90, 100, range(0, 50))), # "lime-dark": hsl_colors(90, 100, range(0, 50)),
#
"green-light": StaticPalette(hsl_colors(120, 100, range(50, 100))), "green-light": hsl_colors(120, 100, range(50, 100)),
"green-dark": StaticPalette(hsl_colors(120, 100, range(0, 50))), "green-dark": hsl_colors(120, 100, range(0, 50)),
#
#"seafoam-light": StaticPalette(hsl_colors(150, 100, range(50, 100))), # "seafoam-light": hsl_colors(150, 100, range(50, 100)),
#"seafoam-dark": StaticPalette(hsl_colors(150, 100, range(0, 50))), # "seafoam-dark": hsl_colors(150, 100, range(0, 50)),
#
"cyan-light": StaticPalette(hsl_colors(180, 100, range(50, 100))), "cyan-light": hsl_colors(180, 100, range(50, 100)),
"cyan-dark": StaticPalette(hsl_colors(180, 100, range(0, 50))), "cyan-dark": hsl_colors(180, 100, range(0, 50)),
#
#"teal-light": StaticPalette(hsl_colors(210, 100, range(50, 100))), # "teal-light": hsl_colors(210, 100, range(50, 100)),
#"teal-dark": StaticPalette(hsl_colors(210, 100, range(0, 50))), # "teal-dark": hsl_colors(210, 100, range(0, 50)),
#
"blue-light": StaticPalette(hsl_colors(240, 100, range(50, 100))), "blue-light": hsl_colors(240, 100, range(50, 100)),
"blue-dark": StaticPalette(hsl_colors(240, 100, range(0, 50))), "blue-dark": hsl_colors(240, 100, range(0, 50)),
#
"purple-light": StaticPalette(hsl_colors(270, 100, range(50, 100))), "purple-light": hsl_colors(270, 100, range(50, 100)),
"purple-dark": StaticPalette(hsl_colors(270, 100, range(0, 50))), "purple-dark": hsl_colors(270, 100, range(0, 50)),
#
"magenta-light": StaticPalette(hsl_colors(300, 100, range(50, 100))), "magenta-light": hsl_colors(300, 100, range(50, 100)),
"magenta-dark": StaticPalette(hsl_colors(300, 100, range(0, 50))), "magenta-dark": hsl_colors(300, 100, range(0, 50)),
#
"pink-light": StaticPalette(hsl_colors(330, 100, range(50, 100))), "pink-light": hsl_colors(330, 100, range(50, 100)),
"pink-dark": StaticPalette(hsl_colors(330, 100, range(0, 50))), "pink-dark": hsl_colors(330, 100, range(0, 50)),
#
"gray-light": StaticPalette(hsl_colors(0, 0, range(50, 100))), "gray-light": hsl_colors(0, 0, range(50, 100)),
"gray-dark": StaticPalette(hsl_colors(0, 0, range(0, 50))), "gray-dark": hsl_colors(0, 0, range(0, 50)),
} }
MULTICOLOR_PALETTES = { MULTICOLOR_PALETTES = {
"rainbow": StaticPalette(hsl_colors(range(0, 360), 100, 50)), "rainbow": hsl_colors(range(0, 360), 100, 50),
"rainbow-reverse": StaticPalette(list(reversed(hsl_colors(range(0, 360), 100, 50)))), "rainbow-reverse": list(reversed(hsl_colors(range(0, 360), 100, 50))),
} }
DEFAULT_PALETTES = { DEFAULT_PALETTES = {
**GRADIENT_PALETTES, **MULTICOLOR_PALETTES, **GRADIENT_PALETTES,
**MULTICOLOR_PALETTES,
} }

View File

@@ -1,42 +1,42 @@
<svg width="256" height="160" xmlns="http://www.w3.org/2000/svg"> <svg width="256" height="160" xmlns="http://www.w3.org/2000/svg">
<rect x="0" y="0" width="32" height="32" fill="hsl(0.00,0.00%,23.00%)" /> <rect x="0" y="0" width="32" height="32" fill="hsl(330.00,100.00%,23.00%)" />
<rect x="32" y="0" width="32" height="32" fill="hsl(0.00,0.00%,30.00%)" /> <rect x="32" y="0" width="32" height="32" fill="hsl(330.00,100.00%,7.00%)" />
<rect x="64" y="0" width="32" height="32" fill="hsl(0.00,0.00%,30.00%)" /> <rect x="64" y="0" width="32" height="32" fill="hsl(330.00,100.00%,13.00%)" />
<rect x="96" y="0" width="32" height="32" fill="hsl(0.00,0.00%,0.00%)" /> <rect x="96" y="0" width="32" height="32" fill="hsl(330.00,100.00%,30.00%)" />
<rect x="128" y="0" width="32" height="32" fill="hsl(0.00,0.00%,40.00%)" /> <rect x="128" y="0" width="32" height="32" fill="hsl(330.00,100.00%,33.00%)" />
<rect x="160" y="0" width="32" height="32" fill="hsl(0.00,0.00%,30.00%)" /> <rect x="160" y="0" width="32" height="32" fill="hsl(330.00,100.00%,37.00%)" />
<rect x="192" y="0" width="32" height="32" fill="hsl(0.00,0.00%,33.00%)" /> <rect x="192" y="0" width="32" height="32" fill="hsl(330.00,100.00%,10.00%)" />
<rect x="224" y="0" width="32" height="32" fill="hsl(0.00,0.00%,43.00%)" /> <rect x="224" y="0" width="32" height="32" fill="hsl(330.00,100.00%,40.00%)" />
<rect x="0" y="32" width="32" height="32" fill="hsl(0.00,0.00%,20.00%)" /> <rect x="0" y="32" width="32" height="32" fill="hsl(330.00,100.00%,50.00%)" />
<rect x="32" y="32" width="32" height="32" fill="hsl(0.00,0.00%,33.00%)" /> <rect x="32" y="32" width="32" height="32" fill="hsl(330.00,100.00%,50.00%)" />
<rect x="64" y="32" width="32" height="32" fill="hsl(0.00,0.00%,33.00%)" /> <rect x="64" y="32" width="32" height="32" fill="hsl(330.00,100.00%,0.00%)" />
<rect x="96" y="32" width="32" height="32" fill="hsl(0.00,0.00%,10.00%)" /> <rect x="96" y="32" width="32" height="32" fill="hsl(330.00,100.00%,23.00%)" />
<rect x="128" y="32" width="32" height="32" fill="hsl(0.00,0.00%,50.00%)" /> <rect x="128" y="32" width="32" height="32" fill="hsl(330.00,100.00%,0.00%)" />
<rect x="160" y="32" width="32" height="32" fill="hsl(0.00,0.00%,30.00%)" /> <rect x="160" y="32" width="32" height="32" fill="hsl(330.00,100.00%,27.00%)" />
<rect x="192" y="32" width="32" height="32" fill="hsl(0.00,0.00%,30.00%)" /> <rect x="192" y="32" width="32" height="32" fill="hsl(330.00,100.00%,7.00%)" />
<rect x="224" y="32" width="32" height="32" fill="hsl(0.00,0.00%,20.00%)" /> <rect x="224" y="32" width="32" height="32" fill="hsl(330.00,100.00%,47.00%)" />
<rect x="0" y="64" width="32" height="32" fill="hsl(0.00,0.00%,40.00%)" /> <rect x="0" y="64" width="32" height="32" fill="hsl(330.00,100.00%,0.00%)" />
<rect x="32" y="64" width="32" height="32" fill="hsl(0.00,0.00%,37.00%)" /> <rect x="32" y="64" width="32" height="32" fill="hsl(330.00,100.00%,27.00%)" />
<rect x="64" y="64" width="32" height="32" fill="hsl(0.00,0.00%,43.00%)" /> <rect x="64" y="64" width="32" height="32" fill="hsl(330.00,100.00%,3.00%)" />
<rect x="96" y="64" width="32" height="32" fill="hsl(0.00,0.00%,37.00%)" /> <rect x="96" y="64" width="32" height="32" fill="hsl(330.00,100.00%,37.00%)" />
<rect x="128" y="64" width="32" height="32" fill="hsl(0.00,0.00%,13.00%)" /> <rect x="128" y="64" width="32" height="32" fill="hsl(330.00,100.00%,43.00%)" />
<rect x="160" y="64" width="32" height="32" fill="hsl(0.00,0.00%,20.00%)" /> <rect x="160" y="64" width="32" height="32" fill="hsl(330.00,100.00%,23.00%)" />
<rect x="192" y="64" width="32" height="32" fill="hsl(0.00,0.00%,17.00%)" /> <rect x="192" y="64" width="32" height="32" fill="hsl(330.00,100.00%,23.00%)" />
<rect x="224" y="64" width="32" height="32" fill="hsl(0.00,0.00%,13.00%)" /> <rect x="224" y="64" width="32" height="32" fill="hsl(330.00,100.00%,47.00%)" />
<rect x="0" y="96" width="32" height="32" fill="hsl(0.00,0.00%,3.00%)" /> <rect x="0" y="96" width="32" height="32" fill="hsl(330.00,100.00%,40.00%)" />
<rect x="32" y="96" width="32" height="32" fill="hsl(0.00,0.00%,3.00%)" /> <rect x="32" y="96" width="32" height="32" fill="hsl(330.00,100.00%,50.00%)" />
<rect x="64" y="96" width="32" height="32" fill="hsl(0.00,0.00%,40.00%)" /> <rect x="64" y="96" width="32" height="32" fill="hsl(330.00,100.00%,7.00%)" />
<rect x="96" y="96" width="32" height="32" fill="hsl(0.00,0.00%,40.00%)" /> <rect x="96" y="96" width="32" height="32" fill="hsl(330.00,100.00%,37.00%)" />
<rect x="128" y="96" width="32" height="32" fill="hsl(0.00,0.00%,30.00%)" /> <rect x="128" y="96" width="32" height="32" fill="hsl(330.00,100.00%,47.00%)" />
<rect x="160" y="96" width="32" height="32" fill="hsl(0.00,0.00%,23.00%)" /> <rect x="160" y="96" width="32" height="32" fill="hsl(330.00,100.00%,33.00%)" />
<rect x="192" y="96" width="32" height="32" fill="hsl(0.00,0.00%,20.00%)" /> <rect x="192" y="96" width="32" height="32" fill="hsl(330.00,100.00%,50.00%)" />
<rect x="224" y="96" width="32" height="32" fill="hsl(0.00,0.00%,3.00%)" /> <rect x="224" y="96" width="32" height="32" fill="hsl(330.00,100.00%,3.00%)" />
<rect x="0" y="128" width="32" height="32" fill="hsl(0.00,0.00%,0.00%)" /> <rect x="0" y="128" width="32" height="32" fill="hsl(330.00,100.00%,3.00%)" />
<rect x="32" y="128" width="32" height="32" fill="hsl(0.00,0.00%,0.00%)" /> <rect x="32" y="128" width="32" height="32" fill="hsl(330.00,100.00%,0.00%)" />
<rect x="64" y="128" width="32" height="32" fill="hsl(0.00,0.00%,27.00%)" /> <rect x="64" y="128" width="32" height="32" fill="hsl(330.00,100.00%,27.00%)" />
<rect x="96" y="128" width="32" height="32" fill="hsl(0.00,0.00%,3.00%)" /> <rect x="96" y="128" width="32" height="32" fill="hsl(330.00,100.00%,37.00%)" />
<rect x="128" y="128" width="32" height="32" fill="hsl(0.00,0.00%,37.00%)" /> <rect x="128" y="128" width="32" height="32" fill="hsl(330.00,100.00%,43.00%)" />
<rect x="160" y="128" width="32" height="32" fill="hsl(0.00,0.00%,37.00%)" /> <rect x="160" y="128" width="32" height="32" fill="hsl(330.00,100.00%,50.00%)" />
<rect x="192" y="128" width="32" height="32" fill="hsl(0.00,0.00%,33.00%)" /> <rect x="192" y="128" width="32" height="32" fill="hsl(330.00,100.00%,23.00%)" />
<rect x="224" y="128" width="32" height="32" fill="hsl(0.00,0.00%,7.00%)" /> <rect x="224" y="128" width="32" height="32" fill="hsl(330.00,100.00%,30.00%)" />
</svg> </svg>

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB