"""Renders the blob background: assets/background.png (static) and the looping GIFs. Shapes come from the desktop theme's SVG masks. Usage: python3 tools/background.py [hue] """ import math, re, subprocess, sys, tempfile, pathlib from PIL import Image sys.path.insert(0, str(pathlib.Path(__file__).parent)) from palette import oklch # noqa: E402 CSS = pathlib.Path(sys.argv[1]).read_text() HUE = float(sys.argv[2]) if len(sys.argv) > 2 else 328 W, H = 720, 1600 FRAMES, FPS = 90, 9 # 10 s loop GIF_W = 360 FROST_W, FROST = 180, 3 # frosted GIF: half size (blur hides it, keeps the file small) and blur radius in px OUT = pathlib.Path(__file__).resolve().parent.parent / 'themes/cookie-glass/assets' def path_of(name): m = re.search(rf"--sh-{name}: url\(\"data:image/svg\+xml,.*?d='([^']+)'", CSS) return m.group(1) P = {n: path_of(n) for n in ('cookie', 'flower', 'bloom', 'pent', 'sq', 'diamond')} P['circle'] = 'M100 8A92 92 0 1 1 99.99 8Z' A, B, C = oklch(.80, .09, HUE), oklch(.72, .07, HUE - 30), oklch(.55, .08, HUE + 14) BG = oklch(.13, .012, HUE) # (shape, style, cx, cy, size, rot, layer). style: fill | soft | line. layer 0 = far, 3 = near. SHAPES = [ ('cookie', 'fill', .20, .14, 300, 8, 1), ('flower', 'soft', .86, .22, 360, -12, 0), ('pent', 'line', .62, .08, 170, 20, 2), ('circle', 'soft', .10, .42, 220, 0, 0), ('bloom', 'fill', .78, .47, 260, 30, 2), ('sq', 'soft', .34, .36, 150, 18, 1), ('diamond', 'fill', .14, .66, 190, -8, 3), ('cookie', 'line', .66, .70, 330, 40, 1), ('flower', 'fill', .38, .86, 280, 15, 2), ('circle', 'line', .92, .86, 170, 0, 3), ('pent', 'fill', .88, .64, 120, -25, 3), ('sq', 'line', .50, .56, 110, 10, 3), ] LAYER = [ # opacity, drift amplitude (px), rotate amplitude (deg), loops per cycle, phase (.42, 10, 2, 1, 0.0), (.55, 15, 3, 1, 1.7), (.64, 20, 4, 1, 3.1), (.75, 26, 5, 1, 4.4), ] def svg(t): """t in [0,1): one loop. Every motion is sin/cos of whole loops, so the last frame meets the first.""" out = [f'', '' f'' '' f'' f'' '', f''] for i, (name, style, cx, cy, size, rot, layer) in enumerate(SHAPES): op, amp, ramp, loops, ph = LAYER[layer] a = 2 * math.pi * (t * loops) + ph + i * 0.9 dx, dy = amp * math.sin(a), amp * 0.75 * math.cos(a * 1 if loops == 1 else a) r = rot + ramp * math.sin(a + 1.3) s = size / 200 x, y = cx * W + dx - size / 2, cy * H + dy - size / 2 if style == 'fill': paint = 'fill="url(#page)"' elif style == 'soft': paint, op = 'fill="url(#page)"', op * .5 else: paint = f'fill="none" stroke="url(#page)" stroke-width="{3 / s * 1.4:.2f}" stroke-linejoin="round"' out.append(f'' f'') out.append('') return '\n'.join(out) def render(t, tmp): src = pathlib.Path(tmp) / 'f.svg' src.write_text(svg(t)) png = pathlib.Path(tmp) / 'f.png' subprocess.run(['rsvg-convert', '-o', str(png), str(src)], check=True) return Image.open(png).convert('RGB') OUT.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(dir=pathlib.Path.home() / '.cache') as tmp: render(0, tmp).save(OUT / 'background.png', optimize=True) frames = [render(f / FRAMES, tmp) for f in range(FRAMES)] # GIFs for iOS, whose image view only animates GIFs. One shared palette keeps colors steady between frames. from PIL import ImageFilter small = [fr.resize((GIF_W, round(H * GIF_W / W)), Image.LANCZOS) for fr in frames] for name, imgs in (('background-animated.gif', small), ('background-frosted.gif', [fr.resize((FROST_W, round(H * FROST_W / W)), Image.LANCZOS).filter(ImageFilter.GaussianBlur(FROST)) for fr in frames])): for i, im in enumerate(imgs): im.save(pathlib.Path(tmp) / f'{i:03d}.png') subprocess.run(['ffmpeg', '-v', 'error', '-y', '-framerate', str(FPS), '-i', str(pathlib.Path(tmp) / '%03d.png'), '-vf', 'split[a][b];[a]palettegen=max_colors=64:stats_mode=full[p];' '[b][p]paletteuse=dither=bayer:bayer_scale=5', '-loop', '0', str(OUT / name)], check=True) print('ok', *(f'{p.name} {p.stat().st_size // 1024}KB' for p in sorted(OUT.glob('background*'))))