| 1 | """Shared procedural art toolkit for Reasonix official theme backgrounds. |
| 2 | |
| 3 | All artwork is generated from scratch with numpy + PIL. No reference pixels, |
| 4 | no third-party assets, no text, no UI mockery. Fixed seeds make every render |
| 5 | reproducible; the SHA-256 of each output is recorded in PROVENANCE. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import math |
| 10 | import os |
| 11 | import random |
| 12 | |
| 13 | import numpy as np |
| 14 | from PIL import Image, ImageDraw, ImageFilter |
| 15 | |
| 16 | W, H = 2560, 1440 |
| 17 | |
| 18 | # Layout contract (fractions of W/H) from the theme plan: |
| 19 | # low-info zone : x 0% - 52% |
| 20 | # visual centre : x 68% - 76% |
| 21 | # key content box: x 62% - 88%, y 16% - 72% |
| 22 | KEY_X0, KEY_X1 = 0.62 * W, 0.88 * W |
| 23 | KEY_Y0, KEY_Y1 = 0.16 * H, 0.72 * H |
| 24 | FOCUS_X = 0.72 * W |
| 25 | |
| 26 | |
| 27 | def hex2rgb(s: str) -> tuple[int, int, int]: |
| 28 | s = s.lstrip("#") |
| 29 | return int(s[0:2], 16), int(s[2:4], 16), int(s[4:6], 16) |
| 30 | |
| 31 | |
| 32 | def mix(c1, c2, t: float): |
| 33 | a, b = hex2rgb(c1) if isinstance(c1, str) else c1, hex2rgb(c2) if isinstance(c2, str) else c2 |
| 34 | return tuple(int(round(a[i] + (b[i] - a[i]) * t)) for i in range(3)) |
| 35 | |
| 36 | |
| 37 | def rgba(c, a: int): |
| 38 | return (c[0], c[1], c[2], max(0, min(255, int(a)))) |
| 39 | |
| 40 | |
| 41 | def _stops_arrays(stops): |
| 42 | pos = np.array([p for p, _ in stops], dtype=np.float64) |
| 43 | cols = np.array([hex2rgb(c) for _, c in stops], dtype=np.float64) |
| 44 | return pos, cols |
| 45 | |
| 46 | |
| 47 | def _interp_channel(pos, cols, t): |
| 48 | out = np.zeros((*t.shape, 3), dtype=np.float64) |
| 49 | for ch in range(3): |
| 50 | out[..., ch] = np.interp(t, pos, cols[:, ch]) |
| 51 | return out |
| 52 | |
| 53 | |
| 54 | def gradient(w: int, h: int, stops, direction: str = "v") -> Image.Image: |
| 55 | """Multi-stop gradient. direction: v | h | d1 (tl->br) | d2 (bl->tr) | r (radial from stops centre).""" |
| 56 | pos, cols = _stops_arrays(stops) |
| 57 | if direction == "v": |
| 58 | t = np.linspace(0.0, 1.0, h)[:, None] * np.ones((1, w)) |
| 59 | elif direction == "h": |
| 60 | t = np.ones((h, 1)) * np.linspace(0.0, 1.0, w)[None, :] |
| 61 | elif direction == "d1": |
| 62 | t = (np.linspace(0.0, 1.0, h)[:, None] + np.linspace(0.0, 1.0, w)[None, :]) / 2.0 |
| 63 | elif direction == "d2": |
| 64 | t = (np.linspace(1.0, 0.0, h)[:, None] + np.linspace(0.0, 1.0, w)[None, :]) / 2.0 |
| 65 | else: |
| 66 | raise ValueError(direction) |
| 67 | arr = _interp_channel(pos, cols, t).astype(np.uint8) |
| 68 | return Image.fromarray(arr, "RGB").convert("RGBA") |
| 69 | |
| 70 | |
| 71 | def new_layer() -> Image.Image: |
| 72 | return Image.new("RGBA", (W, H), (0, 0, 0, 0)) |
| 73 | |
| 74 | |
| 75 | def comp(base: Image.Image, layer: Image.Image, blur: float = 0.0) -> Image.Image: |
| 76 | if blur > 0: |
| 77 | layer = layer.filter(ImageFilter.GaussianBlur(blur)) |
| 78 | base.alpha_composite(layer) |
| 79 | return base |
| 80 | |
| 81 | |
| 82 | def glow(base, cx, cy, r, color, alpha, squash=1.0): |
| 83 | """Soft radial light blob (alpha peaks at centre).""" |
| 84 | lay = new_layer() |
| 85 | d = ImageDraw.Draw(lay) |
| 86 | rx, ry = r, r * squash |
| 87 | steps = 28 |
| 88 | for i in range(steps, 0, -1): |
| 89 | t = i / steps |
| 90 | a = alpha * (1.0 - t) ** 1.6 |
| 91 | d.ellipse([cx - rx * t, cy - ry * t, cx + rx * t, cy + ry * t], fill=rgba(color, a)) |
| 92 | base.alpha_composite(lay.filter(ImageFilter.GaussianBlur(r * 0.10))) |
| 93 | |
| 94 | |
| 95 | def beam(base, apex, target, width0, width1, color, alpha, blur=24): |
| 96 | """Spotlight cone from apex towards target point.""" |
| 97 | lay = new_layer() |
| 98 | d = ImageDraw.Draw(lay) |
| 99 | ax, ay = apex |
| 100 | tx, ty = target |
| 101 | dx, dy = tx - ax, ty - ay |
| 102 | ln = math.hypot(dx, dy) or 1.0 |
| 103 | nx, ny = -dy / ln, dx / ln |
| 104 | pts = [ |
| 105 | (ax + nx * width0 / 2, ay + ny * width0 / 2), |
| 106 | (tx + nx * width1 / 2, ty + ny * width1 / 2), |
| 107 | (tx - nx * width1 / 2, ty - ny * width1 / 2), |
| 108 | (ax - nx * width0 / 2, ay - ny * width0 / 2), |
| 109 | ] |
| 110 | d.polygon(pts, fill=rgba(color, alpha)) |
| 111 | base.alpha_composite(lay.filter(ImageFilter.GaussianBlur(blur))) |
| 112 | |
| 113 | |
| 114 | def cubic(p0, p1, p2, p3, n=48): |
| 115 | pts = [] |
| 116 | for i in range(n + 1): |
| 117 | t = i / n |
| 118 | mt = 1 - t |
| 119 | x = mt**3 * p0[0] + 3 * mt**2 * t * p1[0] + 3 * mt * t**2 * p2[0] + t**3 * p3[0] |
| 120 | y = mt**3 * p0[1] + 3 * mt**2 * t * p1[1] + 3 * mt * t**2 * p2[1] + t**3 * p3[1] |
| 121 | pts.append((x, y)) |
| 122 | return pts |
| 123 | |
| 124 | |
| 125 | def smooth_path(segments): |
| 126 | """segments: list of (p0,p1,p2,p3) cubic tuples -> concatenated point list.""" |
| 127 | pts = [] |
| 128 | for seg in segments: |
| 129 | part = cubic(*seg) |
| 130 | if pts: |
| 131 | part = part[1:] |
| 132 | pts.extend(part) |
| 133 | return pts |
| 134 | |
| 135 | |
| 136 | def ellipse_poly(cx, cy, rx, ry, n=72, a0=0.0, a1=2 * math.pi, rot=0.0): |
| 137 | pts = [] |
| 138 | for i in range(n + 1): |
| 139 | t = a0 + (a1 - a0) * i / n |
| 140 | x, y = rx * math.cos(t), ry * math.sin(t) |
| 141 | xr = x * math.cos(rot) - y * math.sin(rot) |
| 142 | yr = x * math.sin(rot) + y * math.cos(rot) |
| 143 | pts.append((cx + xr, cy + yr)) |
| 144 | return pts |
| 145 | |
| 146 | |
| 147 | def superellipse_poly(cx, cy, rx, ry, power=4.0, n=96, rot=0.0): |
| 148 | """Rounded-rect-like closed curve; power 2 = ellipse, higher = boxier.""" |
| 149 | pts = [] |
| 150 | e = 2.0 / power |
| 151 | for i in range(n): |
| 152 | t = 2 * math.pi * i / n |
| 153 | ct, st = math.cos(t), math.sin(t) |
| 154 | x = rx * math.copysign(abs(ct) ** e, ct) |
| 155 | y = ry * math.copysign(abs(st) ** e, st) |
| 156 | xr = x * math.cos(rot) - y * math.sin(rot) |
| 157 | yr = x * math.sin(rot) + y * math.cos(rot) |
| 158 | pts.append((cx + xr, cy + yr)) |
| 159 | return pts |
| 160 | |
| 161 | |
| 162 | def star4(draw, cx, cy, r, color, alpha, thin=0.18, rot=0.0): |
| 163 | """Four-point sparkle.""" |
| 164 | pts = [] |
| 165 | for i in range(8): |
| 166 | ang = rot + math.pi / 4 * i |
| 167 | rr = r if i % 2 == 0 else r * thin |
| 168 | pts.append((cx + rr * math.cos(ang), cy + rr * math.sin(ang))) |
| 169 | draw.polygon(pts, fill=rgba(color, alpha)) |
| 170 | |
| 171 | |
| 172 | def add_grain(img: Image.Image, amount=3.0, seed=7): |
| 173 | rng = np.random.default_rng(seed) |
| 174 | noise = rng.normal(0.0, amount, (H, W, 1)).repeat(3, axis=2) |
| 175 | arr = np.asarray(img.convert("RGB")).astype(np.int16) + noise.astype(np.int16) |
| 176 | arr = np.clip(arr, 0, 255).astype(np.uint8) |
| 177 | out = Image.fromarray(arr, "RGB").convert("RGBA") |
| 178 | out.putalpha(img.split()[3] if img.mode == "RGBA" else 255) |
| 179 | return out |
| 180 | |
| 181 | |
| 182 | def paper_texture(img, color="#000000", alpha=6, seed=3, scale=3): |
| 183 | """Fine fibrous speckle for paper-like fields.""" |
| 184 | rng = np.random.default_rng(seed) |
| 185 | small = rng.normal(0.0, 1.0, (H // scale, W // scale)) |
| 186 | t = Image.fromarray(((small - small.min()) / (small.ptp() + 1e-9) * 255).astype(np.uint8)) |
| 187 | t = t.resize((W, H), Image.BILINEAR).filter(ImageFilter.GaussianBlur(0.6)) |
| 188 | lay = Image.merge("RGBA", (t, t, t, t.point(lambda v: int(v / 255 * alpha)))) |
| 189 | tint = Image.new("RGBA", (W, H), rgba(hex2rgb(color), 255)) |
| 190 | lay = Image.composite(tint, new_layer(), lay.split()[3]) |
| 191 | img.alpha_composite(lay) |
| 192 | |
| 193 | |
| 194 | def petal_pts(cx, cy, size, angle): |
| 195 | """A single rose petal outline (teardrop with curled tip).""" |
| 196 | ca, sa = math.cos(angle), math.sin(angle) |
| 197 | |
| 198 | def tr(p): |
| 199 | x, y = p |
| 200 | return (cx + x * ca - y * sa, cy + x * sa + y * ca) |
| 201 | |
| 202 | segs = [ |
| 203 | ((0, 0), (0.55 * size, -0.42 * size), (1.05 * size, -0.28 * size), (1.18 * size, 0.10 * size)), |
| 204 | ((1.18 * size, 0.10 * size), (1.26 * size, 0.42 * size), (0.72 * size, 0.62 * size), (0.28 * size, 0.55 * size)), |
| 205 | ((0.28 * size, 0.55 * size), (-0.05 * size, 0.50 * size), (-0.10 * size, 0.18 * size), (0, 0)), |
| 206 | ] |
| 207 | return [tr(p) for p in smooth_path(segs)] |
| 208 | |
| 209 | |
| 210 | def leaf_pts(cx, cy, length, width, angle, curl=0.35): |
| 211 | ca, sa = math.cos(angle), math.sin(angle) |
| 212 | |
| 213 | def tr(p): |
| 214 | x, y = p |
| 215 | return (cx + x * ca - y * sa, cy + x * sa + y * ca) |
| 216 | |
| 217 | segs = [ |
| 218 | ((0, 0), (0.30 * length, -width), (0.75 * length, -width * 0.9), (length, -curl * width)), |
| 219 | ((length, -curl * width), (0.72 * length, width * 0.7), (0.32 * length, width), (0, 0)), |
| 220 | ] |
| 221 | return [tr(p) for p in smooth_path(segs)] |
| 222 | |
| 223 | |
| 224 | def butterfly_pts(cx, cy, size, angle, flap=1.0): |
| 225 | """Stylised butterfly: two upper + two lower wings + body, returns list of polys.""" |
| 226 | ca, sa = math.cos(angle), math.sin(angle) |
| 227 | |
| 228 | def tr(p): |
| 229 | x, y = p |
| 230 | return (cx + x * ca - y * sa, cy + x * sa + y * ca) |
| 231 | |
| 232 | polys = [] |
| 233 | for sgn in (-1, 1): |
| 234 | upper = smooth_path([ |
| 235 | ((0, 0), (sgn * 0.95 * size, -0.85 * size * flap), (sgn * 1.45 * size, -0.55 * size * flap), (sgn * 1.30 * size, -0.02 * size)), |
| 236 | ((sgn * 1.30 * size, -0.02 * size), (sgn * 1.05 * size, 0.28 * size), (sgn * 0.35 * size, 0.22 * size), (0, 0.10 * size)), |
| 237 | ]) |
| 238 | polys.append([tr(p) for p in upper]) |
| 239 | lower = smooth_path([ |
| 240 | ((0, 0.08 * size), (sgn * 0.72 * size, 0.28 * size), (sgn * 0.88 * size, 0.78 * size), (sgn * 0.42 * size, 1.02 * size)), |
| 241 | ((sgn * 0.42 * size, 1.02 * size), (sgn * 0.10 * size, 0.95 * size), (sgn * 0.02 * size, 0.42 * size), (0, 0.22 * size)), |
| 242 | ]) |
| 243 | polys.append([tr(p) for p in lower]) |
| 244 | body = ellipse_poly(cx, cy, 0.09 * size, 0.42 * size, rot=angle) |
| 245 | return polys, body |
| 246 | |
| 247 | |
| 248 | def cloud_curl_pts(cx, cy, size, color_flip=False): |
| 249 | """Auspicious-cloud (spiral scroll) outline, flat motif.""" |
| 250 | pts = [] |
| 251 | turns = 1.65 |
| 252 | for i in range(90): |
| 253 | t = i / 89 |
| 254 | ang = turns * 2 * math.pi * t + math.pi * 0.5 |
| 255 | r = size * (1.0 - 0.72 * t) |
| 256 | pts.append((cx + r * math.cos(ang), cy + 0.62 * r * math.sin(ang))) |
| 257 | # outer tail sweeping right |
| 258 | tail = smooth_path([ |
| 259 | (pts[0], (cx + 1.9 * size, cy - 0.9 * size), (cx + 2.9 * size, cy - 0.4 * size), (cx + 3.3 * size, cy + 0.35 * size)), |
| 260 | ]) |
| 261 | return pts, tail |
| 262 | |
| 263 | |
| 264 | def coin_pts(cx, cy, r, rot=0.0): |
| 265 | """Round coin with rounded-square hole (abstract lucky coin, no characters).""" |
| 266 | outer = ellipse_poly(cx, cy, r, r, rot=rot) |
| 267 | hole = superellipse_poly(cx, cy, r * 0.34, r * 0.34, power=4.5, rot=rot) |
| 268 | return outer, hole |
| 269 | |
| 270 | |
| 271 | def ring_pts(cx, cy, r, width, a0=0.0, a1=2 * math.pi, squash=1.0): |
| 272 | outer = ellipse_poly(cx, cy, r, r * squash, a0=a0, a1=a1) |
| 273 | inner = ellipse_poly(cx, cy, r - width, (r - width) * squash, a0=a1, a1=a0) |
| 274 | return outer + inner |
| 275 | |
| 276 | |
| 277 | def draw_poly(draw, pts, color, alpha=255, outline=None, outline_w=0): |
| 278 | draw.polygon(pts, fill=rgba(color, alpha)) |
| 279 | if outline and outline_w > 0: |
| 280 | draw.line(pts + [pts[0]], fill=outline, width=outline_w, joint="curve") |
| 281 | |
| 282 | |
| 283 | def soft_fill(base, pts, color, alpha, blur=0.0): |
| 284 | lay = new_layer() |
| 285 | d = ImageDraw.Draw(lay) |
| 286 | d.polygon(pts, fill=rgba(color, alpha)) |
| 287 | comp(base, lay, blur) |
| 288 | |
| 289 | |
| 290 | def save_webp(img: Image.Image, path: str, quality=82, target_bytes=None): |
| 291 | os.makedirs(os.path.dirname(path), exist_ok=True) |
| 292 | rgb = img.convert("RGB") |
| 293 | q = quality |
| 294 | while True: |
| 295 | rgb.save(path, "WEBP", quality=q, method=6, exact=True) |
| 296 | size = os.path.getsize(path) |
| 297 | if target_bytes is None or size <= target_bytes or q <= 40: |
| 298 | return size |
| 299 | q -= 6 |
| 300 | |
| 301 | |
| 302 | def make_thumb(src: Image.Image, path: str, quality=76, target_bytes=120 * 1024): |
| 303 | thumb = src.convert("RGB").resize((480, 270), Image.LANCZOS) |
| 304 | q = quality |
| 305 | while True: |
| 306 | thumb.save(path, "WEBP", quality=q, method=6, exact=True) |
| 307 | size = os.path.getsize(path) |
| 308 | if size <= target_bytes or q <= 30: |
| 309 | return size |
| 310 | q -= 8 |
| 311 | |
| 312 | |
| 313 | def sha256_file(path: str) -> str: |
| 314 | import hashlib |
| 315 | |
| 316 | h = hashlib.sha256() |
| 317 | with open(path, "rb") as f: |
| 318 | for chunk in iter(lambda: f.read(1 << 20), b""): |
| 319 | h.update(chunk) |
| 320 | return h.hexdigest() |
| 321 | |
| 322 | |
| 323 | def rng(seed: int) -> random.Random: |
| 324 | return random.Random(seed) |
| 325 |