"""Contact sheet of the generated theme art (needs ImageMagick 7 `magick`). Run: python3 -I tools/theme_art/preview.py For each theme it fakes a 300 x 220 card on its background, scene and tile, with the frame set and a thread strip recoloured by the app's gradient map. Then it joins all panels into contact_sheet.png. """ import json import os import subprocess import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from svg_common import THEMES_DIR # noqa: E402 from theme_colours import THEMES # noqa: E402 PANEL_WIDTH, PANEL_HEIGHT = 520, 420 CARD_LEFT, CARD_TOP, CARD_WIDTH, CARD_HEIGHT = 110, 50, 300, 220 META_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "meta.json") LOW, MID, HIGHLIGHT = 0.05, 0.5, 0.45 def magick(*arguments): subprocess.run(["magick", *[str(argument) for argument in arguments]], check=True) def hex_to_rgb(text): text = text.lstrip("#") return [int(text[index:index + 2], 16) for index in (0, 2, 4)] def rgb_to_hex(rgb): return "#" + "".join(f"{max(0, min(255, round(channel))):02x}" for channel in rgb) def make_clut(path, dark, trim): """A 256 x 1 lookup strip: dark up to LOW, dark -> trim up to MID, trim -> highlight up to 1.""" trim_rgb = hex_to_rgb(trim) highlight = rgb_to_hex([channel + (255 - channel) * HIGHLIGHT for channel in trim_rgb]) low_steps, mid_steps = round(LOW * 256), round((MID - LOW) * 256) high_steps = 256 - low_steps - mid_steps magick("-size", f"{low_steps}x1", f"xc:{dark}", "(", "-size", f"1x{mid_steps}", f"gradient:{dark}-{trim}", "-rotate", "-90", ")", "(", "-size", f"1x{high_steps}", f"gradient:{trim}-{highlight}", "-rotate", "-90", ")", "+append", "+repage", "-resize", "256x1!", path) def render_svg(svg_path, png_path, scale): magick("-background", "none", "-density", 96 * scale, svg_path, png_path) def recolour(source_png, clut_png, out_png, extra=()): magick(source_png, "-colorspace", "sRGB", "-type", "TrueColorAlpha", *extra, clut_png, "-channel", "RGB", "-clut", "+channel", out_png) def image_size(path): output = subprocess.run(["magick", "identify", "-format", "%w %h", path], check=True, capture_output=True, text=True).stdout width, height = output.split() return int(width), int(height) def build_frame_layers(theme_dir, work, frame, dark, trim): """Return a list of (png, x, y) layers for edges, corners and corner ornaments.""" clut = os.path.join(work, "frame_clut.png") make_clut(clut, dark, trim) scale = frame["scale"] for name in ("frame_corner", "frame_edge"): render_svg(os.path.join(theme_dir, f"{name}.svg"), os.path.join(work, f"{name}_grey.png"), scale) recolour(os.path.join(work, f"{name}_grey.png"), clut, os.path.join(work, f"{name}.png")) corner = os.path.join(work, "frame_corner.png") edge = os.path.join(work, "frame_edge.png") corner_width, corner_height = image_size(corner) edge_width, _ = image_size(edge) outset = frame["outset"] left, top = round(CARD_LEFT - outset), round(CARD_TOP - outset) right, bottom = round(CARD_LEFT + CARD_WIDTH + outset), round(CARD_TOP + CARD_HEIGHT + outset) side_length, top_length = bottom - top - 2 * corner_height, right - left - 2 * corner_width side_x = round((frame["side_mid"] - frame["edge_mid"]) * scale) layers = [] side = os.path.join(work, "side.png") magick("-size", f"{edge_width}x{side_length}", "-background", "none", f"tile:{edge}", side) flipped_side = os.path.join(work, "side_flop.png") magick(side, "-flop", flipped_side) layers += [(side, left + side_x, top + corner_height), (flipped_side, right - edge_width - side_x, top + corner_height)] rotated = os.path.join(work, "edge_rot.png") magick(edge, "-rotate", "-90", rotated) top_strip, bottom_strip = os.path.join(work, "top.png"), os.path.join(work, "bottom.png") magick("-size", f"{top_length}x{edge_width}", "-background", "none", f"tile:{rotated}", top_strip) magick(top_strip, "-flip", bottom_strip) layers += [(top_strip, left + corner_width, top + side_x), (bottom_strip, left + corner_width, bottom - edge_width - side_x)] corner_files = [corner] if frame["has_corner_fixed"]: fixed = os.path.join(work, "frame_corner_fixed.png") render_svg(os.path.join(theme_dir, "frame_corner_fixed.svg"), fixed, scale) corner_files.append(fixed) for index, corner_file in enumerate(corner_files): mirrored = {} for key, flags in (("tl", []), ("tr", ["-flop"]), ("bl", ["-flip"]), ("br", ["-flop", "-flip"])): mirrored[key] = os.path.join(work, f"corner{index}_{key}.png") magick(corner_file, *flags, mirrored[key]) layers += [(mirrored["tl"], left, top), (mirrored["tr"], right - corner_width, top), (mirrored["bl"], left, bottom - corner_height), (mirrored["br"], right - corner_width, bottom - corner_height)] return layers def build_thread_layers(theme_dir, work, thread, colours): clut = os.path.join(work, "thread_clut.png") make_clut(clut, colours["thread_dark"], colours["thread"]) strip_length, strip_height = thread["strip_size"] layers = [] for label, height_px, y in (("real", thread["width"], 300), ("zoom", strip_height, 330)): grey_png = os.path.join(work, f"thread_{label}_grey.png") render_svg(os.path.join(theme_dir, "thread_strip.svg"), grey_png, height_px / strip_height) coloured = os.path.join(work, f"thread_{label}_tile.png") recolour(grey_png, clut, coloured) long_strip = os.path.join(work, f"thread_{label}.png") tile_height = image_size(coloured)[1] magick("-size", f"480x{tile_height}", "-background", "none", f"tile:{coloured}", long_strip) layers.append((long_strip, 20, y)) return layers def build_panel(theme_id, theme, meta, out_dir): work = os.path.join(out_dir, "work", theme_id) os.makedirs(work, exist_ok=True) theme_dir = os.path.join(THEMES_DIR, theme_id) stops = theme["background"] panel = os.path.join(out_dir, f"{theme_id}.png") magick("-size", f"{PANEL_WIDTH}x{PANEL_HEIGHT}", f"gradient:{stops[0]}-{stops[-1]}", "-colorspace", "sRGB", "-type", "TrueColor", panel) if meta["background_scene"]: scene = os.path.join(work, "scene.png") render_svg(os.path.join(theme_dir, "background_scene.svg"), scene, 1) gravity = {"bottom": "south", "center": "center", "top": "north"}[meta["background_scene"]["anchor"]] magick(scene, "-background", "none", "-resize", f"{PANEL_WIDTH}x{PANEL_HEIGHT}^", "-gravity", gravity, "-extent", f"{PANEL_WIDTH}x{PANEL_HEIGHT}", scene) magick(panel, scene, "-composite", panel) if meta["background_tile"]: tile = os.path.join(work, "tile.png") render_svg(os.path.join(theme_dir, "background_tile.svg"), tile, meta["background_tile"]["scale"]) tiled = os.path.join(work, "tiled.png") magick("-size", f"{PANEL_WIDTH}x{PANEL_HEIGHT}", "-background", "none", f"tile:{tile}", "-channel", "A", "-evaluate", "multiply", meta["background_tile"]["opacity"], "+channel", tiled) magick(panel, tiled, "-composite", panel) colours = theme["colours"] magick(panel, "-fill", colours["card"], "-draw", f"roundrectangle {CARD_LEFT},{CARD_TOP} {CARD_LEFT + CARD_WIDTH - 1},{CARD_TOP + CARD_HEIGHT - 1} 10,10", panel) layers = build_frame_layers(theme_dir, work, meta["frame"], colours["trim_dark"], colours["trim"]) layers += build_thread_layers(theme_dir, work, meta["thread"], colours) arguments = [panel, "-colorspace", "sRGB"] for png, x, y in layers: arguments += [png, "-geometry", f"+{x}+{y}", "-composite"] magick(*arguments, "-gravity", "southeast", "-fill", "#808080", "-pointsize", "16", "-annotate", "+8+6", theme_id, panel) return panel def main(): out_dir = sys.argv[1] if len(sys.argv) > 1 else "art_preview" os.makedirs(out_dir, exist_ok=True) with open(META_PATH, encoding="utf-8") as handle: meta = json.load(handle) panels = [build_panel(theme_id, theme, meta[theme_id], out_dir) for theme_id, theme in THEMES.items()] sheet = os.path.join(out_dir, "contact_sheet.png") magick("montage", *panels, "-tile", "3x", "-geometry", "+6+6", "-background", "#333333", sheet) print(sheet) if __name__ == "__main__": main()