painter.py

2.2 kB · python · 75 lines

1from typing import List2from mrlypy.core.colors import Color, gradient3from mrlypy.core.enums import Mode4from mrlypy.core.state import choice5from mrlypy.tile.models import Tile6from .colors import LEVELS, get_color7from .config import Config8from .enums import Edition, Ink, Scheme, Type9from .models import Paint10from .randomizer import random_primary, random_secondary, random_shades1112# SETUP1314def setup(p: Paint, config: Config = None) -> Paint:15    config = config or Config()16    colors, shades = (None, None)17    if p.is_simple:18        colors, shades = (1, 1)19    if p.edition == Edition.INDEX:20        colors, shades = (2, 2)21    p.scheme = choice(list(Scheme))22    p.target = config.target or choice(list(Type))23    p.primary = random_primary(config.primaries)24    match p.scheme:25        case Scheme.MULTICOLOR:26            p.secondary = random_secondary(colors, p.primary)27        case Scheme.MULTITONE:28            p.secondary = random_secondary(1, None)29            p.shades = random_shades(shades, p.primary)30    return p3132# HELPERS3334def get_primary(p: Paint) -> List[Color]:35    return [get_color(p.primary)]3637def get_secondary(p: Paint) -> List[Color]:38    colors = [get_color(c) for c in p.secondary]39    if p.scheme == Scheme.MULTITONE:40        c1 = colors[0].lightness(LEVELS[0])41        c2 = colors[0].lightness(LEVELS[1])42        colors = [c1, c2]43        steps = len(p.shades)44        if steps > 2:45            colors = gradient(colors, steps)46        colors = [colors[i] for i in p.shades]47    return colors4849# FACTORY5051MODE_FACTORY = {52    Edition.SIMPLE: Mode.TYPE,53    Edition.INDEX: Mode.INDEX,54    Edition.LAYERS: Mode.TAG,55    Edition.NEIGHBORS: Mode.TAG,56    Edition.ROWS: Mode.ROW,57    Edition.COLUMNS: Mode.COLUMN,58    Edition.RANDOM: Mode.RANDOM,59}6061# APPLY6263def apply(p: Paint, tile: Tile) -> Paint:64    primary = get_primary(p)65    secondary = get_secondary(p)66    mode = MODE_FACTORY[p.edition]67    match p.target:68        case Type.FILL:69            mapping = {0: secondary, 1: primary}70        case Type.VOID:71            mapping = {0: primary, 1: secondary}72        case _:73            raise ValueError(f"Invalid target: {p.target}")74    tile.cell.paint(mapping, mode)75    return p