TableTower/tools/theme_art/tiles.py

112 lines
5.6 KiB
Python
Raw Permalink Normal View History

"""Background tiles (real colours, transparent ground) from the mockup's PATTERNS."""
from svg_common import LEAF_PATH, MAPLE_LEAF_PATH, MockupRandom, num, svg_document, wrapped_copies
def memphis_tile(pattern):
"""Squiggle, triangle, dot grid, zigzag, plus and square: the Memphis look."""
dots = "".join(f'<circle cx="{20 + column * 10}" cy="{120 + row * 10}" r="2.2"/>'
for column in range(4) for row in range(3))
body = (
f'<path d="M14 30 q8 -12 16 0 t16 0 t16 0" fill="none" stroke="{pattern["p1"]}" stroke-width="4" stroke-linecap="round"/>'
f'<path d="M120 20 l22 36 h-44z" fill="none" stroke="{pattern["p2"]}" stroke-width="3.5"/>'
f'<circle cx="140" cy="128" r="14" fill="{pattern["p3"]}"/>'
f'<g fill="{pattern["p4"]}">{dots}</g>'
f'<path d="M80 92 l8 8 l8 -8 l8 8 l8 -8" fill="none" stroke="{pattern["p4"]}" stroke-width="3"/>'
f'<path d="M70 150 v16 M62 158 h16" stroke="{pattern["p2"]}" stroke-width="3.5"/>'
f'<rect x="150" y="70" width="14" height="14" transform="rotate(20 157 77)" fill="none" '
f'stroke="{pattern["p1"]}" stroke-width="3"/>')
return 180, 180, body
def leaves_tile(pattern):
"""Nine drifting leaves (plain and maple) placed by the mockup's seed."""
random = MockupRandom(11)
colours = [pattern["p1"], pattern["p2"], pattern["p3"], pattern["p4"]]
leaves = ""
for index in range(9):
x, y = random.next() * 200, random.next() * 200
angle, size = random.next() * 360, 0.7 + random.next() * 0.8
shape = MAPLE_LEAF_PATH if index % 2 else LEAF_PATH
leaves += (f'<path d="{shape}" transform="translate({num(x)} {num(y)}) rotate({num(angle)}) '
f'scale({num(size)})" fill="{colours[index % 4]}" opacity=".5"/>')
return 200, 200, wrapped_copies(leaves, 200, 200)
def plank_tile(pattern):
"""Two vertical boards with seams, wavy grain and one knot. Opaque, so it replaces the plank fill."""
board_colours = ["#4e1c0d", "#5a2210"]
seam_colour = "#2c0f06"
width, height = 240, 220
body = ""
for board in range(2):
left = board * 120
body += f'<rect x="{left}" y="0" width="117" height="{height}" fill="{board_colours[board]}"/>'
body += f'<rect x="{left + 117}" y="0" width="3" height="{height}" fill="{seam_colour}"/>'
for line in range(8):
x = left + 8 + line * 14
stroke_width = 1.8 if line % 3 == 0 else 1
wobble = 5 if (line + board) % 2 else -4
body += (f'<path d="M{x} 0 C{x - wobble} 40 {x + wobble + 1} 70 {x} 110 S{x - wobble} 180 {x} {height}" '
f'fill="none" stroke="{pattern["p1"]}" stroke-width="{stroke_width}" opacity=".45"/>')
body += (f'<ellipse cx="60" cy="150" rx="6" ry="14" fill="none" stroke="{pattern["p1"]}" '
f'stroke-width="1.5" opacity=".5"/>')
body += f'<rect x="0" y="0" width="2" height="{height}" fill="{pattern["p2"]}" opacity=".7"/>'
return width, height, body
def _fern(pattern, x, y, angle, size):
frond = (f'<g transform="translate({x} {y}) rotate({angle}) scale({size})">'
f'<path d="M0 0 Q4 -40 0 -80" stroke="{pattern["p1"]}" stroke-width="2" fill="none"/>')
for step in range(1, 9):
leaf_y, length = -step * 9, 16 - step * 1.4
frond += (f'<path d="M1 {leaf_y} q{num(length)} -6 {num(length)} -2 q-{num(length * 0.6)} 6 -{num(length)} 2Z '
f'M-1 {leaf_y} q-{num(length)} -6 -{num(length)} -2 q{num(length * 0.6)} 6 {num(length)} 2Z" '
f'fill="{pattern["p1"]}"/>')
return frond + "</g>"
def ferns_tile(pattern):
"""Two ferns and two lily pads."""
body = _fern(pattern, 40, 170, -18, 1) + _fern(pattern, 170, 110, 24, 0.8)
body += (f'<g fill="{pattern["p2"]}" opacity=".9"><path d="M120 190 a22 16 0 1 1 1 0 l-1 -16z"/>'
f'<path d="M30 60 a16 12 0 1 1 1 0 l-1 -12z"/></g>')
return 220, 220, wrapped_copies(body, 220, 220)
def stone_tile(pattern):
"""Stone block courses with two cracks and two shaded blocks."""
body = (f'<g fill="none" stroke="{pattern["p1"]}" stroke-width="2">'
f'<path d="M0 1 H160 M0 41 H160 M0 81 H160 M1 1 V41 M81 1 V41 M41 41 V81 M121 41 V81"/></g>'
f'<path d="M95 1 l6 12 l-4 8 l9 10 l-3 10" fill="none" stroke="{pattern["p2"]}" stroke-width="1.4"/>'
f'<path d="M20 50 l10 6 l4 10 l12 4" fill="none" stroke="{pattern["p2"]}" stroke-width="1.2"/>'
f'<g fill="{pattern["p3"]}" opacity=".35"><rect x="6" y="6" width="70" height="30"/>'
f'<rect x="126" y="46" width="30" height="30"/></g>')
return 160, 80, body
def scan_tile(pattern):
"""A faint phosphor grid."""
return 28, 28, f'<path d="M0 .5 H28 M.5 0 V28" stroke="{pattern["p1"]}" stroke-width="1" opacity=".5"/>'
TILE_MAKERS = {"memphis": memphis_tile, "leaves": leaves_tile, "grain": plank_tile,
"ferns": ferns_tile, "stone": stone_tile, "scan": scan_tile}
from tiles_more import MORE_TILE_MAKERS # noqa: E402 (second batch)
TILE_MAKERS.update(MORE_TILE_MAKERS)
# Rustic's tile is the whole plank floor, so it is drawn nearly opaque.
OPACITY_OVERRIDES = {"grain": 0.92}
def make_background_tile(pattern):
"""Return (svg_text, meta) for a pattern, or (None, None) when the mockup has no tile."""
maker = TILE_MAKERS.get(pattern["kind"])
if maker is None:
return None, None
width, height, body = maker(pattern)
opacity = OPACITY_OVERRIDES.get(pattern["kind"], pattern.get("op", 1.0))
return svg_document(width, height, body), {"file": "background_tile.svg", "opacity": opacity, "scale": 1.0}