Palettes are selected by matricizer

Matricizers select color palettes now. This is motivated by the fact
that the gradient palettes don't really look good for openssh randomart
based art, and rainbows do look good.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-05-24 22:18:50 -07:00
parent 6c370237ea
commit 0baf1f7688
2 changed files with 23 additions and 9 deletions

View File

@@ -154,13 +154,6 @@ def main() -> None:
case _: case _:
assert False, f"unknown input type {args.input_type}" 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 # Choose the dimensions and the matricizer
matricizer: Matricizer matricizer: Matricizer
match args.matrix: match args.matrix:
@@ -170,10 +163,17 @@ def main() -> None:
case "randomart": case "randomart":
# 17x9 is what openssh uses # 17x9 is what openssh uses
# TODO - allow configuring dimensions, maybe # TODO - allow configuring dimensions, maybe
matricizer = RandomartMatricizer(17, 9) matricizer = RandomartMatricizer(11, 6)
case _: case _:
assert False, f"invalid args.matrix: {args.matrix}" 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 # Choose the colorizer
colorizer = PaletteColorizer(palette) colorizer = PaletteColorizer(palette)

View File

@@ -1,6 +1,8 @@
"All things that turn a hash into a matrix." "All things that turn a hash into a matrix."
import abc import abc
from typing import Sequence from typing import Mapping, Sequence
from .palettes import Palette, DEFAULT_PALETTES
Matrix = Sequence[Sequence[int]] Matrix = Sequence[Sequence[int]]
@@ -33,6 +35,12 @@ class Matricizer(metaclass=abc.ABCMeta):
:returns: the matrix converted from the hash data. :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): class NibbleMatricizer(Matricizer):
""" """
@@ -81,6 +89,9 @@ class NibbleMatricizer(Matricizer):
return cols return cols
def choose_palette(self, data: bytes, palettes: Mapping[str, Palette]) -> Palette:
return list(palettes.values())[sum(data) % len(palettes)]
class RandomartMatricizer(Matricizer): class RandomartMatricizer(Matricizer):
""" """
@@ -127,3 +138,6 @@ class RandomartMatricizer(Matricizer):
if rows[r][c] < 0xF: if rows[r][c] < 0xF:
rows[r][c] += 1 rows[r][c] += 1
return rows return rows
def choose_palette(self, _data: bytes, _palettes: Mapping[str, Palette]) -> Palette:
return DEFAULT_PALETTES['rainbow']