diff --git a/colorhash/cli.py b/colorhash/cli.py index b8c5f07..1958c67 100644 --- a/colorhash/cli.py +++ b/colorhash/cli.py @@ -5,7 +5,7 @@ from pathlib import Path import sys import textwrap -from .colorizer import PaletteColorizer +from .color import colorize from .matricizer import Matricizer, NibbleMatricizer, RandomartMatricizer from .palettes import Palette, DEFAULT_PALETTES, PALETTES from .writer import ANSIWriter, SVGWriter, Writer @@ -16,6 +16,7 @@ from .writer import ANSIWriter, SVGWriter, Writer # TODO - option to add a caption based on the filename (for SVG) # TODO - load palettes from a file # TODO - PNG output +# TODO - fix Matricizer.choose_dimensions - either get rid of it or use it def cli_main() -> None: @@ -191,12 +192,9 @@ def cli_main() -> None: else: palette = PALETTES[args.palette] - # Choose the colorizer - colorizer = PaletteColorizer(palette) - - # Print SVG + # Matricize and colorize matrix = matricizer.matricize(hashdata) - colors = colorizer.colorize(matrix) + colors = colorize(palette, matrix) # Choose the output writer writer: Writer diff --git a/colorhash/color.py b/colorhash/color.py index 4af4e11..07e1cd9 100644 --- a/colorhash/color.py +++ b/colorhash/color.py @@ -1,6 +1,12 @@ import abc import colorsys import dataclasses +from typing import Sequence, TYPE_CHECKING + + +if TYPE_CHECKING: + from .palettes import Palette + from .matricizer import Matrix class Color(metaclass=abc.ABCMeta): @@ -33,6 +39,7 @@ class RGBColor(Color): """ An RGB color. Colors are expected to be a floating point value from [0.0-255.0). """ + r: float g: float b: float @@ -68,6 +75,13 @@ class HSLColor(Color): r, g, b = colorsys.hls_to_rgb(h, l, s) return RGBColor(r * 255.0, g * 255.0, b * 255.0) - def to_hsl(self) -> "HSLColor": return self + + +ColorMatrix = Sequence[Sequence[Color]] + + +def colorize(palette: "Palette", matrix: "Matrix") -> ColorMatrix: + "Converts a matrix of values from [0x0..0xf] to a matrix of colors." + return [[palette[v] for v in row] for row in matrix] diff --git a/colorhash/colorizer.py b/colorhash/colorizer.py index 7355d57..94f6413 100644 --- a/colorhash/colorizer.py +++ b/colorhash/colorizer.py @@ -1,5 +1,4 @@ "All things that turn a numeric matrix into a colored matrix." -import abc from typing import Sequence from .color import Color @@ -10,43 +9,5 @@ from .palettes import Palette ColorMatrix = Sequence[Sequence[Color]] -class Colorizer(metaclass=abc.ABCMeta): - """ - The base Colorizer class. - - A colorizer turns a numeric matrix into a color matrix, colors being represented by strings of - HTML colors. - """ - - @abc.abstractmethod - def colorize(self, matrix: Matrix) -> ColorMatrix: - """ - Colorize a matrix. - - :param matrix: the matrix to colorize. - :returns: the colorized matrix. - """ - - -class PaletteColorizer(Colorizer): - """ - A palette colorizer. - - This colorizer will use a palette to colorize its inputs. A palette is 16 colors. - """ - def __init__(self, palette: Palette) -> None: - """ - Create a new palette colorizer for a given palette. - - :param palette: the palette to use for this colorizer. - """ - self.palette = palette - - def colorize(self, matrix: Matrix) -> ColorMatrix: - """ - Colorize the given matrix using this colorizer's palette. - - :param matrix: the matrix to colorize. - :returns: the colorized matrix. - """ - return [[self.palette[v] for v in row] for row in matrix] +def colorize(palette: Palette, matrix: Matrix) -> ColorMatrix: + return [[palette[v] for v in row] for row in matrix] diff --git a/colorhash/writer.py b/colorhash/writer.py index a6f83eb..8a59b2d 100644 --- a/colorhash/writer.py +++ b/colorhash/writer.py @@ -1,7 +1,6 @@ import abc -from .color import Color -from .colorizer import ColorMatrix +from .color import Color, ColorMatrix class Writer(metaclass=abc.ABCMeta): @@ -19,13 +18,15 @@ class Writer(metaclass=abc.ABCMeta): class ANSIWriter(Writer): def write(self, matrix: ColorMatrix) -> str: - ESC = '\x1b' + ESC = "\x1b" RESET = f"{ESC}[0m" C = "██" + def ansi_color(c: Color) -> str: c = c.to_rgb() return f"{ESC}[38;2;{round(c.r)};{round(c.g)};{round(c.b)}m" - out = '' + + out = "" for row in matrix: for col in row: out += ansi_color(col) @@ -68,4 +69,3 @@ class SVGWriter(Writer): # Close SVG string svg += "" return svg - diff --git a/examples/commithash.svg b/examples/commithash.svg index 4532a2f..4abdc34 100644 --- a/examples/commithash.svg +++ b/examples/commithash.svg @@ -1,42 +1,42 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file