TheBird

hexagons.py

4.7 kB · python · 174 lines

1import mrlypy.two as m22import mrlypy.three as m33import mrlypy.six as m64from mrlypy.core.enums import Mode5import numpy as np6import random7from colors import *8from config import DATA_DIR9from PIL import Image10from typing import Tuple1112# SETTINGS13SCALE = 514RESIZE = True15WIDTH = random.randint(3, 9)16HEIGHT = random.randint(3, 9)17TILE = "LINEAR"18CROP = True19FLIP = False2021# PAINT2223MODE = Mode.RANDOM24PRIMARY = random_primary()25LIGHTNESS = 33 if PRIMARY == black else 77 if PRIMARY == white else 5026VOID = [c.lightness(LIGHTNESS) for c in random_gradient()]27FILL = [PRIMARY]28GRID = [alpha]29UP = [random_secondary().lightness(LIGHTNESS)]30LEFT = [random_secondary().lightness(LIGHTNESS)]31RIGHT = [random_secondary().lightness(LIGHTNESS)]32PALETTE = {0: VOID, 1: FILL, 2: GRID, 3: UP, 4: LEFT, 5: RIGHT}3334# RESIZE3536def resize(image: Image.Image, orientation: str) -> Image.Image:37    import math38    ratio = math.sqrt(3) / 239    w, h = image.size40    if orientation == "horizontal":41        w = image.width42        h = int(image.height * ratio)43    if orientation == "vertical":44        w = int(image.width * ratio)45        h = image.height46    return image.resize((w, h), Image.Resampling.LANCZOS)4748# HELPERS4950def get_parity(value: int) -> str:51    return "EVEN" if value % 2 == 0 else "ODD"5253def get_start(cell: m2.Cell2d, name: str) -> int:54    width = get_parity(cell.width)55    value = 056    if TILE == "LINEAR":57        if "iso" in name:58            value = 0 if CROP else 159            if width == "EVEN":60                value = int(not value)61        if "pro" in name:62            value = 0 if CROP else 163            if width == "EVEN":64                value = int(not value)65        if "cut" in name:66            value = 1 if CROP else 067    if TILE == "RADIAL":68        radius = get_parity(WIDTH)69        if radius == "EVEN":70            value = 071        if radius == "ODD":72            value = 0 if CROP else 173        if "cut" in name:74            value = int(not value)75    return value if not FLIP else int(not value)7677def get_orientation(name: str) -> str:78    return {79        "iso": "vertical",80        "pro": "vertical",81        "cut": "horizontal",82    }[name]8384# SAVE8586def save_triangles_png(cell: m2.Cell2d, name: str):87    fp = f"{DATA_DIR}/{name}_triangles.png"88    image = m6.draw(cell, SCALE, get_orientation(name), get_start(cell, name))89    if RESIZE:90        image = resize(image, get_orientation(name))91    image.save(fp)92    print(f"Saved: {fp}")9394def save_triangles_svg(cell: m2.Cell2d, name: str):95    fp = f"{DATA_DIR}/{name}_triangles.svg"96    svg = m6.svg(cell, SCALE, get_orientation(name), get_start(cell, name))97    with open(fp, "w") as f:98        f.write(svg)99    print(f"Saved: {fp}")100101def save_squares_png(cell: m2.Cell2d, name: str):102    fp = f"{DATA_DIR}/{name}_squares.png"103    image = cell.to_image(SCALE)104    image.save(fp)105    print(f"Saved: {fp}")106107def save_squares_svg(cell: m2.Cell2d, name: str):108    fp = f"{DATA_DIR}/{name}_squares.svg"109    svg = cell.svg_square(SCALE)110    with open(fp, "w") as f:111        f.write(svg)112    print(f"Saved: {fp}")113114def print_txt(cell: m2.Cell2d, name: str):115    print(name)116    emoji_mode = True117    if emoji_mode:118        mapping = {0: "⬜️", 1: "⬛️", 2: "🟨", 3: "🟥", 4: "🟩", 5: "🟦"}119        for row in cell.text(mapping): print(row)120    else:121        for row in cell.text(): print(row)122    print()123124def save(cell: m2.Cell2d, name: str):125    save_triangles_png(cell, name)126    save_triangles_svg(cell, name)127    save_squares_png(cell, name)128    save_squares_svg(cell, name)129130# BUILD131132def magic():133    cell_1 = m3.random_3d(random.choice([3, 5]), 1)134    cell_2 = m3.random_3d(random.choice([3, 5]), 1)135    cell = m3.magic_3d([cell_1, cell_2])136    return cell137138def general():139    number = random.choice([3, 5])140    grid = random.choice([1, 3])141    level = 1142    cell = m3.random_3d(number, level)143    cell = cell.tile(grid, grid, grid)144    return cell145146def new_cell():147    return m3.carpet_3d(5, 2)148149# MAIN150151def main():152    factory = {153        "iso": m6.iso,154        "pro": m6.pro,155        "cut": m6.cut,156    }157    for name, func in factory.items():158        cell = new_cell()159        cell = func(cell)160        size = (cell.width, cell.height)161        if TILE == "LINEAR":162            cell = m6.tile(cell, WIDTH, HEIGHT)163            cell = m6.tile_crop(cell, size) if CROP else cell164        if TILE == "RADIAL":165            cell = m6.radial(cell, WIDTH)166            cell = m6.radial_crop(cell, size) if CROP else cell167        cell = cell.paint(PALETTE, mode=MODE)168        print(f"WidthxHeight: {cell.width}x{cell.height}")169        print(f"Width={get_parity(cell.width)}")170        print(f"Height={get_parity(cell.height)}")171        save(cell, name)172173if __name__ == "__main__":174    main()