diff --git a/colorhash/__main__.py b/colorhash/__main__.py index 81b8a82..56dee97 100644 --- a/colorhash/__main__.py +++ b/colorhash/__main__.py @@ -154,13 +154,6 @@ def main() -> None: case _: assert False, f"unknown input type {args.input_type}" - # Choose the palette - palette: list[str] - if args.palette == "auto": - palette = list(DEFAULT_PALETTES.values())[sum(hashdata) % len(DEFAULT_PALETTES)] - else: - palette = PALETTES[args.palette] - # Choose the dimensions and the matricizer matricizer: Matricizer match args.matrix: @@ -170,10 +163,17 @@ def main() -> None: case "randomart": # 17x9 is what openssh uses # TODO - allow configuring dimensions, maybe - matricizer = RandomartMatricizer(17, 9) + matricizer = RandomartMatricizer(11, 6) case _: assert False, f"invalid args.matrix: {args.matrix}" + # Choose the palette + palette: list[str] + if args.palette == "auto": + palette = matricizer.choose_palette(hashdata, PALETTES) + else: + palette = PALETTES[args.palette] + # Choose the colorizer colorizer = PaletteColorizer(palette) diff --git a/colorhash/matricizer.py b/colorhash/matricizer.py index 657503c..111e758 100644 --- a/colorhash/matricizer.py +++ b/colorhash/matricizer.py @@ -1,6 +1,8 @@ "All things that turn a hash into a matrix." import abc -from typing import Sequence +from typing import Mapping, Sequence + +from .palettes import Palette, DEFAULT_PALETTES Matrix = Sequence[Sequence[int]] @@ -33,6 +35,12 @@ class Matricizer(metaclass=abc.ABCMeta): :returns: the matrix converted from the hash data. """ + @abc.abstractmethod + def choose_palette(self, data: bytes, palettes: Mapping[str, Palette]) -> Palette: + """ + Choose a palette based on the give data and palettes. + """ + class NibbleMatricizer(Matricizer): """ @@ -81,6 +89,9 @@ class NibbleMatricizer(Matricizer): return cols + def choose_palette(self, data: bytes, palettes: Mapping[str, Palette]) -> Palette: + return list(palettes.values())[sum(data) % len(palettes)] + class RandomartMatricizer(Matricizer): """ @@ -127,3 +138,6 @@ class RandomartMatricizer(Matricizer): if rows[r][c] < 0xF: rows[r][c] += 1 return rows + + def choose_palette(self, _data: bytes, _palettes: Mapping[str, Palette]) -> Palette: + return DEFAULT_PALETTES['rainbow']