From e14c796888733ab3c9caf43019663b8a06cb11ba Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Fri, 24 May 2024 11:45:23 -0700 Subject: [PATCH] Split out color palettes to their own file Signed-off-by: Alek Ratzloff --- colorhash/__main__.py | 16 +++---------- colorhash/palettes.py | 56 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 colorhash/palettes.py diff --git a/colorhash/__main__.py b/colorhash/__main__.py index 0fa6437..06b05c2 100644 --- a/colorhash/__main__.py +++ b/colorhash/__main__.py @@ -7,24 +7,14 @@ import sys from .colorizer import PaletteColorizer from .matricizer import Matricizer, NibbleMatricizer, RandomartMatricizer +from .palettes import DEFAULT_PALETTES, PALETTES from .svg import gensvg # TODO - WASM compile for embedding directly in HTML # TODO - option to add a caption based on the filename -# TODO - palettes defined by JSON - - -PALETTES = { - "red": [f"#{0x110000 * i:06x}" for i in range(0x10)], - "green": [f"#{0x001100 * i:06x}" for i in range(0x10)], - "blue": [f"#{0x000011 * i:06x}" for i in range(0x10)], - "black": [f"#{0x111111 * i:06x}" for i in range(0x10)], - "cyan": [f"#{0x001111 * i:06x}" for i in range(0x10)], - "yellow": [f"#{0x111100 * i:06x}" for i in range(0x10)], - "magenta": [f"#{0x110011 * i:06x}" for i in range(0x10)], - "white": [f"#{0x111111 * (0xF - i):06x}" for i in range(0x10)], -} +# TODO - load palettes from a file +# TODO - HSV color def main() -> None: diff --git a/colorhash/palettes.py b/colorhash/palettes.py new file mode 100644 index 0000000..e364ed5 --- /dev/null +++ b/colorhash/palettes.py @@ -0,0 +1,56 @@ +"Base color palette definitions." +import abc +from typing import Sequence, Self + + +class Palette(metaclass=abc.ABCMeta): + """ + A 16-color palette. + + All colors must be HTML color strings. + """ + + @abc.abstractmethod + def choose(self, color: int) -> str: + """ + Chooses the given color in this palette. + """ + + def __getitem__(self, color: int) -> str: + return self.choose(color) + + +class StaticPalette(Palette): + """ + A static color palette with discrete colors. + """ + + def __init__(self, colors: Sequence[str]) -> 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) -> str: + if not isinstance(color, int): + raise KeyError("palette color indices must be an integer") + return self.colors[color] + + +DEFAULT_PALETTES = { + "red": StaticPalette([f"#{0x110000 * i:06x}" for i in range(0x10)]), + "green": StaticPalette([f"#{0x001100 * i:06x}" for i in range(0x10)]), + "blue": StaticPalette([f"#{0x000011 * i:06x}" for i in range(0x10)]), + "black": StaticPalette([f"#{0x111111 * i:06x}" for i in range(0x10)]), + "cyan": StaticPalette([f"#{0x001111 * i:06x}" for i in range(0x10)]), + "yellow": StaticPalette([f"#{0x111100 * i:06x}" for i in range(0x10)]), + "magenta": StaticPalette([f"#{0x110011 * i:06x}" for i in range(0x10)]), + "white": StaticPalette([f"#{0x111111 * (0xF - i):06x}" for i in range(0x10)]), +} + + +PALETTES = {**DEFAULT_PALETTES}