#!/usr/bin/python3 """Render a 64-frame lunar phase spritesheet (8x8 grid, 300x300 cells) for Plymouth.""" import math import os import numpy as np from PIL import Image COLS = 8 ROWS = 8 FRAMES = COLS * ROWS CELL = 300 RADIUS = 142.5 CENTER = (CELL - 1) / 2.0 # 149.5 def make_base_textures(): """Precompute shared geometry, surface gradients, maria mask, and earthshine.""" coords = np.arange(CELL, dtype=np.float64) yy, xx = np.meshgrid(coords, coords, indexing="ij") u = (xx - CENTER) / RADIUS v = (yy - CENTER) / RADIUS r2 = u * u + v * v r = np.sqrt(r2) disc_alpha = np.clip((1.0 - r) * RADIUS + 0.5, 0.0, 1.0) nz = np.sqrt(np.maximum(0.0, 1.0 - r2)) # Diagonal gradient aligned with Chandra's moon.svg: (0.15, 0.1) -> (0.85, 0.9) gx = (u + 1.0) * 0.5 gy = (v + 1.0) * 0.5 dx, dy = 0.70, 0.80 grad_t = np.clip(((gx - 0.15) * dx + (gy - 0.10) * dy) / (dx * dx + dy * dy), 0.0, 1.0) c0 = np.array([238.0, 244.0, 255.0]) # #eef4ff c1 = np.array([165.0, 189.0, 255.0]) # #a5bdff c2 = np.array([124.0, 156.0, 255.0]) # #7c9cff lit_grad = np.empty((CELL, CELL, 3), dtype=np.float64) low = grad_t <= 0.48 t_low = np.where(low, grad_t / 0.48, 0.0)[..., None] t_high = np.where(~low, (grad_t - 0.48) / 0.52, 0.0)[..., None] lit_grad = np.where(low[..., None], c0 * (1.0 - t_low) + c1 * t_low, c1 * (1.0 - t_high) + c2 * t_high) # Subtle 3D limb darkening and gentle upper-left specular sheen limb_shade = 0.88 + 0.12 * (nz ** 0.45) spec_dist2 = (u + 0.32) ** 2 + (v + 0.34) ** 2 spec = 0.05 * np.exp(-spec_dist2 / 0.22) * nz lit_rgb = np.clip(lit_grad * limb_shade[..., None] + spec[..., None] * 255.0, 0.0, 255.0) # Minimalist, soft translucent lunar maria (overlapping Gaussian basins) maria_specs = [ # (u0, v0, su, sv, angle_rad, weight) (-0.22, -0.32, 0.24, 0.20, -0.3, 0.65), # Mare Imbrium (-0.45, -0.02, 0.22, 0.34, 0.2, 0.55), # Oceanus Procellarum (0.14, -0.30, 0.17, 0.15, 0.2, 0.55), # Mare Serenitatis (0.32, -0.06, 0.18, 0.15, -0.4, 0.52), # Mare Tranquillitatis (0.58, -0.22, 0.10, 0.13, 0.1, 0.48), # Mare Crisium (-0.16, 0.26, 0.20, 0.16, 0.3, 0.42), # Mare Nubium (0.46, 0.18, 0.13, 0.17, -0.2, 0.40), # Mare Fecunditatis ] maria = np.zeros((CELL, CELL), dtype=np.float64) for u0, v0, su, sv, ang, weight in maria_specs: du = u - u0 dv = v - v0 ca, sa = math.cos(ang), math.sin(ang) ru = (du * ca + dv * sa) / su rv = (-du * sa + dv * ca) / sv maria += weight * np.exp(-(ru * ru + rv * rv)) maria_blend = np.clip(maria, 0.0, 1.0) * 0.15 # ~15% max darkening toward #5a77d4 maria_tint = np.array([90.0, 119.0, 212.0]) # #5a77d4 lit_rgb = lit_rgb * (1.0 - maria_blend[..., None]) + maria_tint * maria_blend[..., None] # Earthshine dark side: #1d2c52 (upper-left) -> #111a33 (lower-right) + faint silver-blue rim d0 = np.array([29.0, 44.0, 82.0]) # #1d2c52 d1 = np.array([17.0, 26.0, 51.0]) # #111a33 dark_rgb = d0 * (1.0 - grad_t[..., None]) + d1 * grad_t[..., None] rim_glow = 0.20 * np.exp(-((np.maximum(0.0, 1.0 - r)) / 0.025) ** 2) dark_rgb = dark_rgb * (1.0 - rim_glow[..., None]) + c2 * rim_glow[..., None] return u, nz, disc_alpha, lit_rgb, dark_rgb def main(): here = os.path.dirname(os.path.abspath(__file__)) out_path = os.path.join(here, "chandra", "moon-phases.png") nx, nz, disc_alpha, lit_rgb, dark_rgb = make_base_textures() alpha_u8 = np.round(disc_alpha * 255.0).astype(np.uint8) inside = disc_alpha > 0.0 sheet = np.zeros((ROWS * CELL, COLS * CELL, 4), dtype=np.uint8) for idx in range(FRAMES): p = idx / float(FRAMES) ang = 2.0 * math.pi * p sx = math.sin(ang) sz = -math.cos(ang) ndotl = nx * sx + nz * sz t = np.clip(ndotl / 0.11, 0.0, 1.0) lit = t * t * (3.0 - 2.0 * t) rgb = dark_rgb * (1.0 - lit[..., None]) + lit_rgb * lit[..., None] rgb[~inside] = 0.0 row = idx // COLS col = idx % COLS y0, y1 = row * CELL, (row + 1) * CELL x0, x1 = col * CELL, (col + 1) * CELL sheet[y0:y1, x0:x1, :3] = np.round(np.clip(rgb, 0.0, 255.0)).astype(np.uint8) sheet[y0:y1, x0:x1, 3] = alpha_u8 Image.fromarray(sheet, "RGBA").save(out_path, optimize=True) print(f"wrote {out_path} ({os.path.getsize(out_path)} bytes)") if __name__ == "__main__": main()