Skip to content
Verified contractExactMIT

ArgonautsPictureV6B

Address0xcbc05445a5afd922e4cc5fbe319daf2e346056c8
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.24

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.24+commit.e11b9ed9
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,328,086
Creation transaction
0x618509cc26…ab8b8881ae

2 files

src/PictureV6B.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Ink, Pen, Fixed} from "./Strokes.sol";

/// @title ArgonautsPictureV6B
contract ArgonautsPictureV6B {
    using Pen for Ink;

    int256 internal constant W = 1e18;
    int256 internal constant HALF = 5e17;
    int256 internal constant LOOP = 200e18;
    int256 internal constant PH = 4e16;
    int256 internal constant T1 = 25e17;
    int256 internal constant T2 = 22e17;
    int256 internal constant T3 = 3e17;
    int256 internal constant T4 = 9e17;
    int256 internal constant T5 = 12e17;
    int256 internal constant T6 = 3e18;

    struct G {
        uint256 tok;
        uint8[576] own;
        uint256[576] rgb;
        int256[576] dist;
        int256 max;
        int256 end;
        int256 s0;
        int256 lt0;
        bool keepA;
        bool keepB;
        uint256[] held;
    }

    function layer(uint256 tok, uint8[576] calldata own, uint256[576] calldata rgb, int256 s0, int256 end)
        external
        pure
        returns (bytes memory)
    {
        unchecked {
            G memory g;
            g.tok = tok;
            g.keepB = true;
            for (uint256 p = 0; p < 576; p++) {
                g.own[p] = own[p];
                g.rgb[p] = rgb[p];
            }
            _dist(g);
            g.end = end;
            g.s0 = s0;
            g.lt0 = 6e18 + _f(_h(g.tok, 1, 2), 4e18) + _f(_h(g.tok, 0, 7), 500e18);
            return _body(g);
        }
    }

    function _h(uint256 a, uint256 b, uint256 c) internal pure returns (uint256 h) {
        unchecked {
            h = ((a * 0x9E3779B1) & 0xFFFFFFFF) ^ (((b + 0x632BE5AB) * 0x85EBCA77) & 0xFFFFFFFF)
                ^ (((c + 0x27D4EB2F) * 0xC2B2AE3D) & 0xFFFFFFFF);
            h ^= h >> 16;
            h = (h * 0x7FEB352D) & 0xFFFFFFFF;
            h ^= h >> 15;
            h = (h * 0x846CA68B) & 0xFFFFFFFF;
            h ^= h >> 16;
        }
    }

    function _f(uint256 h, int256 x) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return int256((h * uint256(x)) >> 32);
        }
    }

    function _mod(int256 a, int256 m) internal pure returns (int256 r) {
        unchecked {
            r = a % m;
            if (r < 0) r += m;
        }
    }

    function _at(int256 s0, int256 t) internal pure returns (int256) {
        unchecked {
            return _mod(LOOP - _mod(s0 + t, LOOP), LOOP);
        }
    }

    function _cl(int256 v) internal pure returns (int256) {
        unchecked {
            return v < 0 ? int256(0) : v > W ? W : v;
        }
    }

    function _all(G memory g, int256 u) internal pure returns (int256) {
        unchecked {
            if (u < T1) return 0;
            if (u < g.end) return _cl(W / 10 + (u - T1) / 6);
            return W - _cl((u - g.end) * 10 / 45);
        }
    }

    function _roundI(int256 x) internal pure returns (int256 q) {
        unchecked {
            int256 v = x + HALF;
            q = v / W;
            if (v < 0 && v % W != 0) q -= 1;
        }
    }

    function _floorI(int256 x) internal pure returns (int256 q) {
        unchecked {
            q = x / W;
            if (x < 0 && x % W != 0) q -= 1;
        }
    }

    function _fig(G memory g, int256 p) internal pure returns (bool) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return p >= 0 && p < 576 && g.own[uint256(p)] != 0;
        }
    }

    function _core(G memory g, uint256 p) internal pure returns (bool) {
        unchecked {
            return g.own[p] == 1 || g.own[p] == 6;
        }
    }

    function _dist(G memory g) internal pure {
        unchecked {
            uint256[] memory q = new uint256[](576);
            uint256 n;
            for (uint256 p = 0; p < 576; p++) {
                g.dist[p] = -1;
                if (g.own[p] == 6) {
                    g.dist[p] = 0;
                    q[n++] = p;
                }
            }
            int256[8] memory ks = [int256(-1), 1, -24, 24, -25, -23, 23, 25];
            for (uint256 i = 0; i < n; i++) {
                uint256 p0 = q[i];
                for (uint256 j = 0; j < 8; j++) {
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 m = int256(p0) + ks[j];
                    if (m < 0 || m > 575) continue;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    uint256 um = uint256(m);
                    if (g.own[um] == 0 || g.dist[um] != -1) continue;
                    if ((um % 24 > p0 % 24 ? um % 24 - p0 % 24 : p0 % 24 - um % 24) > 1) continue;
                    g.dist[um] = g.dist[p0] + 1;
                    if (g.dist[um] > g.max) g.max = g.dist[um];
                    q[n++] = um;
                }
            }
            for (uint256 p = 0; p < 576; p++) {
                if (g.own[p] != 0 && g.dist[p] == -1) g.dist[p] = g.max;
            }
        }
    }

    function _dec(Ink memory k, int256 v, uint256 places) internal pure {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            uint256 p = 10 ** places;
            // forge-lint: disable-next-line(unsafe-typecast)
            uint256 q = uint256((v * int256(p) + HALF) / W);
            uint256 whole = q / p;
            uint256 fr = q % p;
            if (fr == 0) {
                k.num(whole);
                return;
            }
            if (whole != 0) k.num(whole);
            k.ch(0x2e);
            uint256 d = p / 10;
            while (fr != 0) {
                k.ch(48 + fr / d);
                fr %= d;
                d /= 10;
            }
        }
    }

    function _pct(Ink memory k, int256 u) internal pure {
        unchecked {
            _dec(k, u * 100 / 200, 4);
            k.ch(0x25);
        }
    }

    function _hex(Ink memory k, int256[3] memory c) internal pure {
        unchecked {
            uint256 rgb;
            for (uint256 i = 0; i < 3; i++) {
                int256 q = _roundI(c[i]);
                if (q < 0) q = 0;
                if (q > 255) q = 255;
                // forge-lint: disable-next-line(unsafe-typecast)
                rgb = (rgb << 8) | uint256(q);
            }
            k.hex6(rgb);
        }
    }

    function _px(Ink memory k, uint256 p) internal pure {
        unchecked {
            k.ch(0x4d);
            k.num(p % 24);
            k.ch(0x20);
            k.num(p / 24);
            k.w("h1v1h-1z", 8);
        }
    }

    function _lerp(int256[3] memory a, int256[3] memory b, int256 f) internal pure returns (int256[3] memory o) {
        unchecked {
            for (uint256 i = 0; i < 3; i++) {
                o[i] = a[i] + (b[i] - a[i]) * f / W;
            }
        }
    }

    function _c(uint256 r, uint256 gg, uint256 b) internal pure returns (int256[3] memory o) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            o = [int256(r) * W, int256(gg) * W, int256(b) * W];
        }
    }

    function _keep(G memory g, uint256 p) internal pure returns (uint256) {
        unchecked {
            if (g.keepB && g.own[p] == 1) {
                uint256 v = g.rgb[p];
                uint256 r = v >> 16;
                uint256 gg = (v >> 8) & 255;
                uint256 b = v & 255;
                uint256 mx = r > gg ? (r > b ? r : b) : (gg > b ? gg : b);
                uint256 mn = r < gg ? (r < b ? r : b) : (gg < b ? gg : b);
                uint256 d = mx - mn;
                if (d >= 40) {
                    bool tan = mx == r && gg >= b && 14 * d <= 60 * (gg - b) && 60 * (gg - b) <= 52 * d;
                    if (!tan) return 2;
                }
            }
            if (g.keepA) {
                for (uint256 i = 0; i < g.held.length; i++) {
                    if (g.held[i] == p) return 1;
                }
            }
            return 0;
        }
    }

    function _colour(G memory g, uint256 p) internal pure returns (int256[3] memory col) {
        unchecked {
            uint256 v = g.rgb[p];
            int256[3] memory src = _c(v >> 16, (v >> 8) & 255, v & 255);
            // forge-lint: disable-next-line(unsafe-typecast)
            int256 s = int256((v >> 16) + ((v >> 8) & 255) + (v & 255));
            if (2 * s < 765) col = _lerp(_c(16, 64, 128), _c(44, 130, 196), 2 * s * W / 765);
            else col = _lerp(_c(44, 130, 196), _c(214, 244, 255), (2 * s - 765) * W / 765);
            uint256 kp = _keep(g, p);
            col = _lerp(col, src, kp == 2 ? int256(45e16) : kp == 1 ? int256(62e16) : int256(16e16));
            uint256 gv = g.rgb[0];
            col = _lerp(
                col,
                _c(gv >> 16, (gv >> 8) & 255, gv & 255),
                35e16 * (765 - s) / 765 * (kp != 0 ? int256(3) : int256(10)) / 10
            );
            // forge-lint: disable-next-line(unsafe-typecast)
            if (!_fig(g, int256(p) - 24) || !_fig(g, int256(p) - 1)) col = _lerp(col, _c(214, 244, 255), 4e17);
        }
    }

    function _seen(uint256[8] memory bits, uint256 v) internal pure returns (bool) {
        unchecked {
            // forge-lint: disable-next-line(incorrect-shift)
            bool was = bits[v >> 8] & (1 << (v & 255)) != 0;
            // forge-lint: disable-next-line(incorrect-shift)
            bits[v >> 8] |= 1 << (v & 255);
            return was;
        }
    }

    function _body(G memory g) internal pure returns (bytes memory) {
        unchecked {
            Ink memory st = Pen.open(20_000);
            Ink memory k = Pen.open(80_000);
            st.w("<style>", 7);
            k.w("<g>", 3);
            uint256[8] memory sn;
            uint256[8] memory sf;
            uint256[] memory ln = new uint256[](576);
            uint256[] memory lf = new uint256[](576);
            uint256 nn;
            uint256 nf;
            for (uint256 p = 0; p < 576; p++) {
                if (!_core(g, p)) continue;
                int256 on;
                int256 off;
                if (g.own[p] == 6) {
                    off = g.end + T5;
                } else {
                    on = T2 + g.dist[p] * T3 + _f(_h(g.tok, 0, 1000 + p), T4);
                    off = g.end + (g.max - g.dist[p]) * 6e16 + _f(_h(g.tok, 0, 2000 + p), 6e17);
                }
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 a = uint256((_at(g.s0, on) * 10 + HALF) / W) % 2000;
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 b = uint256((_at(g.s0, off) * 10 + HALF) / W) % 2000;
                if (!_seen(sn, a)) ln[nn++] = a;
                if (!_seen(sf, b)) lf[nf++] = b;
                k.w('<path class="w', 14);
                if (g.own[p] == 6) k.w(" wb", 3);
                k.w(" n", 2);
                k.num(a);
                k.w(" f", 2);
                k.num(b);
                k.w('" fill="', 8);
                _hex(k, _colour(g, p));
                k.w('" d="', 5);
                _px(k, p);
                k.w('"/>', 3);
            }
            k.w("</g>", 4);
            for (uint256 i = 0; i < nn; i++) {
                st.w(".n", 2);
                st.num(ln[i]);
                st.w("{--n:-", 6);
                // forge-lint: disable-next-line(unsafe-typecast)
                _dec(st, int256(ln[i]) * W / 10, 1);
                st.w("s}", 2);
            }
            for (uint256 i = 0; i < nf; i++) {
                st.w(".f", 2);
                st.num(lf[i]);
                st.w("{--f:-", 6);
                // forge-lint: disable-next-line(unsafe-typecast)
                _dec(st, int256(lf[i]) * W / 10, 1);
                st.w("s}", 2);
            }
            Ink memory e = Pen.open(2_000);
            _edge(g, st, e);
            Ink memory dr = Pen.open(10_000);
            _runs(g, dr);
            _cycles(g, st, k);
            k.put(e.close());
            bytes memory d = dr.close();
            if (d.length != 0) {
                k.w('<g fill="#ecfaff">', 18);
                k.put(d);
                k.w("</g>", 4);
            }
            _rise(g, st, k);
            _fall(g, k);
            st.w("</style>", 8);
            return bytes.concat(st.close(), k.close());
        }
    }

    function _edge(G memory g, Ink memory st, Ink memory e) internal pure {
        unchecked {
            Ink memory d = Pen.open(1_000);
            for (uint256 p = 0; p < 576; p++) {
                uint8 o = g.own[p];
                // forge-lint: disable-next-line(unsafe-typecast)
                if (o == 0 || o == 1 || o == 6 || o == 4 || o == 7 || _fig(g, int256(p) - 24)) continue;
                _px(d, p);
            }
            bytes memory path = d.close();
            if (path.length == 0) return;
            int256[5] memory ks = [int256(0), 18e15, 18e16, 18e16, 0];
            int256[5] memory ts = [T1, T1, T1 + 54e17, g.end, g.end + T6 + 15e17];
            st.w("@keyframes we", 13);
            st.num(g.tok);
            st.ch(0x7b);
            for (uint256 i = 0; i < 5; i++) {
                _pct(st, ts[i]);
                st.w("{opacity:", 9);
                _dec(st, ks[i], 3);
                st.ch(0x7d);
            }
            st.ch(0x7d);
            e.put(bytes('<path style="opacity:0;animation:we'));
            e.num(g.tok);
            e.put(bytes(" 200s linear infinite;animation-de"));
            e.w("lay:-", 5);
            _dec(e, _at(g.s0, 0), 2);
            e.w('s" fill="#d6f4ff" d="', 21);
            e.put(path);
            e.w('"/>', 3);
        }
    }

    function _runs(G memory g, Ink memory k) internal pure {
        unchecked {
            uint256[] memory starts = new uint256[](576);
            uint256 ns;
            for (uint256 p = 0; p < 552; p++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                if (g.own[p] == 6 && _fig(g, int256(p) + 24) && g.own[p + 24] != 6) starts[ns++] = p + 24;
            }
            if (ns == 0) return;
            uint256[] memory path = new uint256[](23);
            for (uint256 j = 0; j < 7; j++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 cur = starts[uint256(_f(_h(g.tok, 0, 300 + j), int256(ns)))];
                uint256 len = 1;
                path[0] = cur;
                for (uint256 s = 0; s < 22; s++) {
                    uint256[3] memory dn;
                    uint256 nd;
                    for (uint256 q = 0; q < 3; q++) {
                        uint256 m = q == 0 ? cur + 24 : q == 1 ? cur + 23 : cur + 25;
                        if (m >= 576 || g.own[m] == 0 || g.own[m] == 4) continue;
                        if ((m % 24 > cur % 24 ? m % 24 - cur % 24 : cur % 24 - m % 24) > 1) continue;
                        dn[nd++] = m;
                    }
                    if (nd == 0) break;
                    cur = _h(g.tok, 0, 400 + j * 31 + s) * 10 < 7 * 4294967296 ? dn[0] : dn[nd - 1];
                    path[len++] = cur;
                }
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 st = T5 + int256(j) * 55e16 + _f(_h(g.tok, 0, 500 + j), 4e17);
                for (uint256 a = 0; a < len; a++) {
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 t = st + int256(a) * W / 5;
                    if (t >= 9e18) continue;
                    k.w('<path class="wd" style="--t:-', 29);
                    _dec(k, _at(g.s0, t), 2);
                    k.w('s" d="', 6);
                    _px(k, path[a]);
                    k.w('"/>', 3);
                }
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 at = st + int256(len) * W / 5;
                uint256 last = path[len - 1];
                if (at < 9e18 && last / 24 < 23) {
                    k.w('<g style="--t:-', 15);
                    _dec(k, _at(g.s0, at), 2);
                    k.put(bytes('s" fill="#e2f6ff"><path class="va" d="'));
                    _px(k, last + 24);
                    k.w('"/><path class="vb" d="', 23);
                    _px(k, last + 24);
                    k.w('"/></g>', 7);
                }
            }
        }
    }

    function _wd(Ink memory k, uint256 i, int256 x, int256 y, int256 sh) internal pure {
        unchecked {
            int256[4] memory a = [int256(9), 0, 7, 6];
            int256[4] memory b = [int256(0), 10, 7, -6];
            int256[4] memory w = [int256(9), -7, 11, -6];
            int256 per = Fixed.TWO_PI * 10 / (w[i] < 0 ? -w[i] : w[i]);
            _dec(k, _mod((a[i] * x + b[i] * y) * W / w[i] + sh, per), 2);
        }
    }

    struct L {
        uint256[8] sa;
        uint256[8] ss;
        uint256[8] sd;
        uint256[] la;
        uint256[] ls;
        uint256[] ld;
        uint256 na;
        uint256 ns;
        uint256 nd;
    }

    function _row(G memory g, L memory m, Ink memory l, uint256 y, int256 sh) internal pure {
        unchecked {
            bool any;
            for (uint256 x = 0; x < 24; x++) {
                uint256 p = y * 24 + x;
                if (!_core(g, p)) continue;
                if (!any) {
                    any = true;
                    l.w('<g class="lr" style="--y:-', 26);
                    // forge-lint: disable-next-line(unsafe-typecast)
                    _wd(l, 1, 0, int256(y), sh);
                    l.w('s">', 3);
                }
                if (!_seen(m.sa, x)) m.la[m.na++] = x;
                if (!_seen(m.ss, x + y)) m.ls[m.ns++] = x + y;
                if (!_seen(m.sd, x + 23 - y)) m.ld[m.nd++] = x + 23 - y;
                l.w('<path class="lp a', 17);
                l.num(x);
                l.w(" s", 2);
                l.num(x + y);
                l.w(" d", 2);
                l.num(x + 23 - y);
                l.w('" d="', 5);
                _px(l, p);
                l.w('"/>', 3);
            }
            if (any) l.w("</g>", 4);
        }
    }

    function _classes(Ink memory st, bytes1 c, uint256[] memory list, uint256 n, uint256 which, int256 sh)
        internal
        pure
    {
        unchecked {
            for (uint256 i = 0; i < n; i++) {
                st.ch(0x2e);
                st.ch(uint8(c));
                st.num(list[i]);
                st.w("{--", 3);
                st.ch(uint8(c));
                st.w(":-", 2);
                // forge-lint: disable-next-line(unsafe-typecast)
                _wd(st, which, int256(list[i]) - (which == 3 ? int256(23) : int256(0)), 0, sh);
                st.w("s}", 2);
            }
        }
    }

    function _cycles(G memory g, Ink memory st, Ink memory k) internal pure {
        unchecked {
            int256 sh = g.lt0 - g.s0;
            Ink memory l = Pen.open(20_000);
            L memory m;
            m.la = new uint256[](48);
            m.ls = new uint256[](48);
            m.ld = new uint256[](48);
            for (uint256 y = 0; y < 24; y++) {
                _row(g, m, l, y, sh);
            }
            _classes(st, "a", m.la, m.na, 0, sh);
            _classes(st, "s", m.ls, m.ns, 2, sh);
            _classes(st, "d", m.ld, m.nd, 3, sh);
            int256 spread = T2 + g.max * T3 + T4 + T5;
            st.w("@keyframes le", 13);
            st.num(g.tok);
            st.w("{0%{opacity:0}", 14);
            _pct(st, spread * 6 / 10);
            st.w("{opacity:.6}", 12);
            _pct(st, g.end);
            st.w("{opacity:.6}", 12);
            _pct(st, g.end + T6);
            st.w("{opacity:0}}", 12);
            k.put(bytes('<g fill="#d6f4ff" style="opacity:0;animation:le'));
            k.num(g.tok);
            k.put(bytes(" 200s linear infinite;animation-de"));
            k.w("lay:-", 5);
            _dec(k, _at(g.s0, 0), 2);
            k.w('s">', 3);
            k.put(l.close());
            k.w("</g>", 4);
        }
    }

    function _soft(G memory g, Ink memory st, uint256 b2, int256 per, uint256 top, uint256 bot) internal pure {
        unchecked {
            for (uint256 side = 0; side < 2; side++) {
                st.w("@keyframes wb", 13);
                st.num(g.tok);
                st.ch(0x5f);
                st.num(b2);
                st.ch(side == 0 ? 0x61 : 0x62);
                st.ch(0x7b);
                int256 ly = type(int256).min;
                int256 lq = -1;
                int256 j;
                for (; j * W / 10 < per; j++) {
                    int256 v = (j * W / 10 + PH) * W / per;
                    if (v >= W) break;
                    // forge-lint: disable-next-line(unsafe-typecast)
                    int256 dy = -v * int256(bot - top);
                    int256 a = 55 * Fixed.sin(v * Fixed.PI / W) / 100;
                    int256 y0 = _floorI(dy);
                    int256 fy = dy - y0 * W;
                    int256 al = side == 0 ? (W - fy) * a / W * 115 / 100 : fy * a / W * 115 / 100;
                    if (al > W) al = W;
                    if (al < 0) al = 0;
                    int256 yy = side == 0 ? y0 : y0 + 1;
                    int256 q = (al * 1000 + HALF) / W;
                    if (yy == ly && q == lq) continue;
                    ly = yy;
                    lq = q;
                    _pct(st, j * W / 10);
                    st.w("{transform:translateY(", 22);
                    st.sint(yy);
                    st.w("px);opacity:", 12);
                    _dec(st, al, 3);
                    st.ch(0x7d);
                }
                _pct(st, j * W / 10);
                st.w("{opacity:0}}", 12);
            }
            for (uint256 side = 0; side < 2; side++) {
                st.w(".q", 2);
                st.num(b2);
                st.ch(side == 0 ? 0x61 : 0x62);
                st.w("{animation-name:wb", 18);
                st.num(g.tok);
                st.ch(0x5f);
                st.num(b2);
                st.ch(side == 0 ? 0x61 : 0x62);
                st.ch(0x7d);
            }
        }
    }

    function _rise(G memory g, Ink memory st, Ink memory k) internal pure {
        unchecked {
            uint256 top = 24;
            uint256 bot;
            Ink memory clip = Pen.open(6_000);
            for (uint256 p = 0; p < 576; p++) {
                if (!_core(g, p)) continue;
                if (p / 24 < top) top = p / 24;
                if (p / 24 > bot) bot = p / 24;
                _px(clip, p);
            }
            Ink memory r = Pen.open(16_000);
            int256 stop = g.end + T6 + 15e17;
            for (uint256 b2 = 0; b2 < 3; b2++) {
                int256 per = T2 + _f(_h(g.tok, b2, 600), 14e17);
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 off = int256((_h(g.tok, b2, 601) * uint256(per)) >> 32);
                _soft(g, st, b2, per, top, bot);
                for (int256 u = 0; u < stop;) {
                    int256 c2 = (g.lt0 + u + off) / per;
                    int256 rise = c2 * per - off - g.lt0;
                    int256 mid = _all(g, rise + per / 2);
                    if (rise >= 0 && mid > T3) {
                        // forge-lint: disable-next-line(unsafe-typecast)
                        int256 bx = _roundI(8e18 + _f(_h(g.tok, uint256(c2), 602 + b2), 9e18));
                        r.w('<g style="--t:-', 15);
                        _dec(r, _at(g.s0, rise), 2);
                        r.w('s" fill-opacity="', 17);
                        _dec(r, (mid - T3) * 10 / 7, 3);
                        for (uint256 side = 0; side < 2; side++) {
                            if (side == 0) r.w('"><path class="q q', 18);
                            else r.w('"/><path class="q q', 19);
                            r.num(b2);
                            r.ch(side == 0 ? 0x61 : 0x62);
                            r.w('" d="M', 6);
                            r.sint(bx);
                            r.ch(0x20);
                            r.num(bot);
                            r.w("h1v1h-1z", 8);
                        }
                        r.w('"/></g>', 7);
                    }
                    u = (c2 + 1) * per - off - g.lt0 + 1;
                }
            }
            bytes memory body = r.close();
            if (body.length == 0) return;
            k.w('<clipPath id="wc', 16);
            k.num(g.tok);
            k.w('"><path d="', 11);
            k.put(clip.close());
            k.put(bytes('"/></clipPath><g clip-path="url(#wc'));
            k.num(g.tok);
            k.w(')" fill="#ecfaff">', 18);
            k.put(body);
            k.w("</g>", 4);
        }
    }

    function _fall(G memory g, Ink memory k) internal pure {
        unchecked {
            uint256[] memory under = new uint256[](576);
            uint256 nu;
            for (uint256 p = 0; p < 552; p++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                if (g.own[p] != 0 && !_fig(g, int256(p) + 24) && p / 24 < 21) under[nu++] = p + 24;
            }
            if (nu == 0) return;
            Ink memory r = Pen.open(14_000);
            int256 stop = g.end + T6 + 15e17;
            for (int256 jj = (g.lt0 + 6e17 - 1) / 6e17;; jj++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 u = jj * 6e17 + _f(_h(g.tok, uint256(jj), 700), 4e17) - g.lt0;
                if (u > stop) break;
                int256 a = _all(g, u);
                if (a <= T3 || u < 0) continue;
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 e = under[uint256(_f(_h(g.tok, uint256(jj), 701), int256(nu)))];
                r.w('<g style="--t:-', 15);
                _dec(r, _at(g.s0, u), 2);
                r.w('s" fill-opacity="', 17);
                _dec(r, (a - T3) * 10 / 7, 3);
                r.w('"><path class="ua" d="', 22);
                _px(r, e);
                r.w('"/><path class="ub" d="', 23);
                _px(r, e);
                r.w('"/></g>', 7);
            }
            bytes memory body = r.close();
            if (body.length == 0) return;
            k.w('<g fill="#d6f4ff">', 18);
            k.put(body);
            k.w("</g>", 4);
        }
    }
}

src/Strokes.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

struct Ink {
    uint256 at;
    uint256 from;
    uint256 cap;
}

library Pen {
    error TooLong();

    function open(uint256 cap) internal pure returns (Ink memory k) {
        bytes memory b = new bytes(cap + 32);
        uint256 at;
        assembly ("memory-safe") {
            at := add(b, 32)
        }
        k.at = at;
        k.from = at;
        k.cap = cap;
    }

    function put(Ink memory k, bytes memory s) internal pure {
        assembly ("memory-safe") {
            let n := mload(s)
            let at := mload(k)
            mcopy(at, add(s, 32), n)
            mstore(k, add(at, n))
        }
    }

    function w(Ink memory k, bytes32 s, uint256 n) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore(at, s)
            mstore(k, add(at, n))
        }
    }

    function ch(Ink memory k, uint256 c) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore8(at, c)
            mstore(k, add(at, 1))
        }
    }

    function num(Ink memory k, uint256 v) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            switch lt(v, 10)
            case 1 {
                mstore8(at, add(48, v))
                mstore(k, add(at, 1))
            }
            default {
                switch lt(v, 100)
                case 1 {
                    mstore8(at, add(48, div(v, 10)))
                    mstore8(add(at, 1), add(48, mod(v, 10)))
                    mstore(k, add(at, 2))
                }
                default {
                    let n := 1
                    for { let t := v } gt(t, 9) {} {
                        t := div(t, 10)
                        n := add(n, 1)
                    }
                    let p := add(at, n)
                    mstore(k, p)
                    for {} 1 {} {
                        p := sub(p, 1)
                        mstore8(p, add(48, mod(v, 10)))
                        v := div(v, 10)
                        if iszero(v) { break }
                    }
                }
            }
        }
    }

    function sint(Ink memory k, int256 v) internal pure {
        if (v < 0) {
            ch(k, 0x2d);
            // forge-lint: disable-next-line(unsafe-typecast)
            num(k, uint256(-v));
        } else {
            // forge-lint: disable-next-line(unsafe-typecast)
            num(k, uint256(v));
        }
    }

    function hex6(Ink memory k, uint256 rgb) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            mstore8(at, 0x23)
            let n := and(shr(20, rgb), 15)
            mstore8(add(at, 1), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(16, rgb), 15)
            mstore8(add(at, 2), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(12, rgb), 15)
            mstore8(add(at, 3), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(8, rgb), 15)
            mstore8(add(at, 4), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(shr(4, rgb), 15)
            mstore8(add(at, 5), add(add(n, 48), mul(39, gt(n, 9))))
            n := and(rgb, 15)
            mstore8(add(at, 6), add(add(n, 48), mul(39, gt(n, 9))))
            mstore(k, add(at, 7))
        }
    }

    function op(Ink memory k, uint256 v) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            switch iszero(v)
            case 1 {
                mstore8(at, 48)
                at := add(at, 1)
            }
            default {
                switch lt(v, 1000)
                case 0 {
                    mstore8(at, 49)
                    at := add(at, 1)
                }
                default {
                    mstore8(at, 46)
                    mstore8(add(at, 1), add(48, div(v, 100)))
                    at := add(at, 2)
                    let r := mod(v, 100)
                    if r {
                        mstore8(at, add(48, div(r, 10)))
                        at := add(at, 1)
                        r := mod(r, 10)
                        if r {
                            mstore8(at, add(48, r))
                            at := add(at, 1)
                        }
                    }
                }
            }
            mstore(k, at)
        }
    }

    function secs(Ink memory k, uint256 steps) internal pure {
        num(k, steps / 10);
        uint256 r = steps % 10;
        if (r != 0) {
            ch(k, 0x2e);
            ch(k, 48 + r);
        }
        ch(k, 0x73);
    }

    function close(Ink memory k) internal pure returns (bytes memory b) {
        uint256 used = k.at - k.from;
        if (used > k.cap) revert TooLong();
        uint256 from = k.from;
        assembly ("memory-safe") {
            b := sub(from, 32)
            mstore(b, used)
        }
    }
}

library Fixed {
    int256 internal constant WAD = 1e18;
    int256 internal constant HALF = 5e17;
    int256 internal constant PI = 3141592653589793238;
    int256 internal constant TWO_PI = 6283185307179586476;
    int256 internal constant HALF_PI = 1570796326794896619;

    function sin(int256 x) internal pure returns (int256) {
        unchecked {
            x = x % TWO_PI;
            if (x < 0) x += TWO_PI;
            bool neg;
            if (x >= PI) {
                x -= PI;
                neg = true;
            }
            if (x > HALF_PI) x = PI - x;
            int256 x2 = x * x / WAD;
            int256 term = x;
            int256 sum = x;
            term = term * x2 / WAD / 6;
            sum -= term;
            term = term * x2 / WAD / 20;
            sum += term;
            term = term * x2 / WAD / 42;
            sum -= term;
            term = term * x2 / WAD / 72;
            sum += term;
            term = term * x2 / WAD / 110;
            sum -= term;
            term = term * x2 / WAD / 156;
            sum += term;
            return neg ? -sum : sum;
        }
    }

    function cos(int256 x) internal pure returns (int256) {
        return sin(x + HALF_PI);
    }

    function round(int256 x) internal pure returns (int256 q) {
        unchecked {
            int256 v = x + HALF;
            q = v / WAD;
            if (v < 0 && v % WAD != 0) q -= 1;
        }
    }

    function op(int256 x) internal pure returns (uint256 k) {
        unchecked {
            if (x <= 0) return 0;
            // forge-lint: disable-next-line(unsafe-typecast)
            k = uint256((x * 1000 + HALF) / WAD);
            if (k > 1000) k = 1000;
        }
    }
}

struct Card {
    uint256 tok;
    uint8[7] t;
    uint256[576] px;
    uint256[128] x;
    uint256[] eyes;
    uint256[] lens;
    uint256[] red;
    uint256[] blue;
    uint256[] screen;
    uint256[] body;
    uint256[] head;
    uint256[] halo;
    uint256[] led;
    uint256[] smoke;
    uint256[] petals;
    uint256[] polyps;
    uint256[] glints;
    uint256[] crown;
    uint256 smokeRGB;
    bool dragon;
    int256 tempo;
    int256 tb0;
    int256 pv;
}

struct Wisps {
    uint256 nw;
    uint256[] full;
    uint256[] half;
    int256[] dx;
    uint256[] dy;
    uint256 any1;
    uint256 any2;
}

struct Light {
    uint256[] mask;
    uint256 count;
    uint256 cycle;
    bool own;
    uint256 loop;
    uint256 colour;
    int256 amount;
    int256 rise;
    int256 fall;
    uint256 mulC;
    uint256 mulS;
    uint256 add;
    uint256 mod;
}

/// @title Strokes
abstract contract Strokes {
    using Pen for Ink;

    int256 internal constant WAD = 1e18;

    uint256 internal constant RGB = 0xFFFFFF;
    uint256 internal constant SMK = 1 << 27;
    uint256 internal constant RED = 1 << 28;
    uint256 internal constant BLUE = 1 << 29;

    uint256 internal constant GROUND = 0;
    uint256 internal constant BODY = 1;
    uint256 internal constant CLOAK = 2;
    uint256 internal constant EYES = 4;
    uint256 internal constant MOUTH = 5;
    uint256 internal constant HEAD = 6;
    uint256 internal constant SMOKE = 7;

    function _mark() internal pure returns (uint256 m) {
        assembly ("memory-safe") {
            m := mload(0x40)
        }
    }

    function _free(uint256 m) internal pure {
        assembly {
            mstore(0x40, m)
        }
    }

    function _own(uint256 v) internal pure returns (uint256) {
        return (v >> 24) & 7;
    }

    function _chroma(uint256 c) internal pure returns (uint256) {
        unchecked {
            uint256 r = (c >> 16) & 255;
            uint256 g = (c >> 8) & 255;
            uint256 b = c & 255;
            uint256 mx = r > g ? r : g;
            if (b > mx) mx = b;
            uint256 mn = r < g ? r : g;
            if (b < mn) mn = b;
            return mx - mn;
        }
    }

    function _top(uint256 c) internal pure returns (uint256 mx) {
        uint256 r = (c >> 16) & 255;
        uint256 g = (c >> 8) & 255;
        uint256 b = c & 255;
        mx = r > g ? r : g;
        if (b > mx) mx = b;
    }

    function _lum(uint256 c) internal pure returns (uint256) {
        unchecked {
            return 299 * ((c >> 16) & 255) + 587 * ((c >> 8) & 255) + 114 * (c & 255);
        }
    }

    function _trim(uint256[] memory a, uint256 n) internal pure {
        assembly ("memory-safe") {
            mstore(a, n)
        }
    }

    function _sw(Card memory c, uint256 i) internal pure returns (int256) {
        // forge-lint: disable-next-line(unsafe-typecast)
        return int256((c.x[i] * 1e18) >> 32);
    }

    function _steps(Card memory c, uint256 p10) internal pure returns (uint256 n) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            n = uint256((int256(p10) * WAD * 2 + c.tempo) / (2 * c.tempo));
            if (n < 2) n = 2;
        }
    }

    function _from(Card memory c, uint256 p10) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return (c.tb0 * 10 / int256(p10)) % WAD;
        }
    }

    function _at(int256 from, uint256 i, uint256 n) internal pure returns (int256) {
        unchecked {
            // forge-lint: disable-next-line(unsafe-typecast)
            return (from + WAD * int256(i) / int256(n)) % WAD;
        }
    }

    function _ch(uint256 v, uint256 a, uint256 b) internal pure returns (uint256 o) {
        unchecked {
            o = (v * a + b * 100 + 50) / 100;
            if (o > 255) o = 255;
        }
    }

    function _scaled(uint256 rgb, uint256 a, uint256 b) internal pure returns (uint256) {
        return (_ch(rgb >> 16, a, b) << 16) | (_ch((rgb >> 8) & 255, a, b) << 8) | _ch(rgb & 255, a, b);
    }

    function _flat(uint256 n, uint256 colour) internal pure returns (uint256[] memory out) {
        unchecked {
            out = new uint256[](n);
            for (uint256 i = 0; i < n; i++) {
                out[i] = colour;
            }
        }
    }

    function _tint(Card memory c, uint256[] memory pxs, uint256 a, uint256 b)
        internal
        pure
        returns (uint256[] memory out)
    {
        unchecked {
            out = new uint256[](pxs.length);
            for (uint256 i = 0; i < pxs.length; i++) {
                out[i] = _scaled(c.px[pxs[i]] & RGB, a, b);
            }
        }
    }

    function _any(uint256[] memory v) internal pure returns (bool) {
        unchecked {
            for (uint256 i = 0; i < v.length; i++) {
                if (v[i] != 0) return true;
            }
            return false;
        }
    }

    function _anim(
        Ink memory k,
        bytes32 attr,
        uint256 attrLen,
        uint256[] memory v,
        uint256 steps,
        uint256 shift,
        bool raw
    ) internal pure {
        unchecked {
            k.w('<animate attributeName="', 24);
            k.w(attr, attrLen);
            k.w('" values="', 10);
            _vals(k, v, raw);
            k.w('" dur="', 7);
            k.secs(steps);
            k.ch(0x22);
            if (shift != 0) {
                k.w(' begin="-', 9);
                k.secs(shift);
                k.ch(0x22);
            }
            k.w(' calcMode="discrete" repeatCount', 32);
            k.w('="indefinite"/>', 15);
        }
    }

    function _vals(Ink memory k, uint256[] memory v, bool raw) internal pure {
        assembly ("memory-safe") {
            let at := mload(k)
            let n := mload(v)
            let p := add(v, 32)
            for { let i := 0 } lt(i, n) { i := add(i, 1) } {
                if i {
                    mstore8(at, 0x3b)
                    at := add(at, 1)
                }
                let x := mload(add(p, shl(5, i)))
                switch raw
                case 0 {
                    switch iszero(x)
                    case 1 {
                        mstore8(at, 48)
                        at := add(at, 1)
                    }
                    default {
                        switch lt(x, 1000)
                        case 0 {
                            mstore8(at, 49)
                            at := add(at, 1)
                        }
                        default {
                            mstore8(at, 46)
                            mstore8(add(at, 1), add(48, div(x, 100)))
                            at := add(at, 2)
                            let r := mod(x, 100)
                            if r {
                                mstore8(at, add(48, div(r, 10)))
                                at := add(at, 1)
                                r := mod(r, 10)
                                if r {
                                    mstore8(at, add(48, r))
                                    at := add(at, 1)
                                }
                            }
                        }
                    }
                }
                default {
                    if gt(x, 99) {
                        mstore8(at, add(48, div(x, 100)))
                        at := add(at, 1)
                        x := mod(x, 100)
                        mstore8(at, add(48, div(x, 10)))
                        at := add(at, 1)
                        x := mod(x, 10)
                    }
                    if gt(x, 9) {
                        mstore8(at, add(48, div(x, 10)))
                        at := add(at, 1)
                        x := mod(x, 10)
                    }
                    mstore8(at, add(48, x))
                    at := add(at, 1)
                }
            }
            mstore(k, at)
        }
    }

    function _run(Ink memory k, uint256 x, uint256 y, uint256 n) internal pure {
        assembly ("memory-safe") {
            function d(at, v) -> to {
                to := at
                if gt(v, 9) {
                    mstore8(to, add(48, div(v, 10)))
                    to := add(to, 1)
                }
                mstore8(to, add(48, mod(v, 10)))
                to := add(to, 1)
            }
            let at := mload(k)
            mstore8(at, 0x4d)
            at := d(add(at, 1), x)
            mstore8(at, 0x20)
            at := d(add(at, 1), y)
            mstore8(at, 0x68)
            at := d(add(at, 1), n)
            mstore(at, "v1h-")
            at := d(add(at, 4), n)
            mstore8(at, 0x7a)
            mstore(k, add(at, 1))
        }
    }

    function _px(uint256[576] memory px, uint256 p) internal pure returns (uint256 v) {
        assembly ("memory-safe") {
            v := mload(add(px, shl(5, p)))
        }
    }

    function _paths(Ink memory k, uint256[] memory pxs, uint256[] memory cols) internal pure {
        unchecked {
            uint256 len = pxs.length;
            uint256[] memory runs = new uint256[](len);
            uint256 nr;
            uint256 i;
            while (i < len) {
                uint256 p = pxs[i];
                uint256 col = cols[i];
                uint256 n = 1;
                while (i + n < len && pxs[i + n] == p + n && (p + n) / 24 == p / 24 && cols[i + n] == col) n++;
                runs[nr++] = p | (n << 16) | (col << 32);
                i += n;
            }
            for (i = 0; i < nr; i++) {
                uint256 r = runs[i];
                if (r >> 255 != 0) continue;
                uint256 col = (r >> 32) & RGB;
                k.w('<path fill="', 12);
                k.hex6(col);
                k.w('" d="', 5);
                for (uint256 j = i; j < nr; j++) {
                    uint256 q = runs[j];
                    if (q >> 255 != 0 || ((q >> 32) & RGB) != col) continue;
                    _run(k, (q & 0xFFFF) % 24, (q & 0xFFFF) / 24, (q >> 16) & 0xFFFF);
                    runs[j] = q | (1 << 255);
                }
                k.w('"/>', 3);
            }
        }
    }

    function _shape(Ink memory k, uint256[] memory pxs, uint256 parity) internal pure {
        unchecked {
            uint256 len = pxs.length;
            uint256 i;
            while (i < len) {
                uint256 p = pxs[i];
                if (parity < 2 && p % 2 != parity) {
                    i++;
                    continue;
                }
                uint256 n = 1;
                if (parity == 2) {
                    while (i + n < len && pxs[i + n] == p + n && (p + n) / 24 == p / 24) n++;
                }
                _run(k, p % 24, p / 24, n);
                i += n;
            }
        }
    }

    function _fade(Ink memory k, uint256[] memory pxs, uint256[] memory cols, uint256[] memory v, uint256 steps)
        internal
        pure
    {
        if (pxs.length == 0 || !_any(v)) return;
        k.w('<g opacity="0">', 15);
        _anim(k, "opacity", 7, v, steps, 0, false);
        _paths(k, pxs, cols);
        k.w("</g>", 4);
    }

    function _lights(Ink memory k, Card memory c, Light memory o) internal pure {
        unchecked {
            uint256 nc = o.own ? _steps(c, o.cycle) : o.cycle;
            uint256[] memory xs = new uint256[](o.loop);
            uint256[] memory ys = new uint256[](o.loop);
            uint256[] memory env = new uint256[](nc);
            for (uint256 s = 0; s < o.count; s++) {
                // forge-lint: disable-next-line(unsafe-typecast)
                int256 c0 = (o.own ? c.tb0 * 10 / int256(o.cycle) : int256(0)) + WAD * int256(s) / int256(o.count);
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 first = uint256(c0 / WAD);
                int256 fr = c0 % WAD;
                // forge-lint: disable-next-line(unsafe-typecast)
                uint256 sh = uint256(fr * int256(nc) / WAD);
                for (uint256 j = 0; j < o.loop; j++) {
                    uint256 p = o.mask[(c.x[((first + j) * o.mulC + s * o.mulS + o.add) % o.mod] * o.mask.length) >> 32];
                    xs[j] = p % 24;
                    ys[j] = p / 24;
                }
                for (uint256 m = 0; m < nc; m++) {
                    int256 f = _at(fr, m, nc);
                    int256 e = f < o.rise ? f * WAD / o.rise : WAD - (f - o.rise) * WAD / o.fall;
                    env[m] = Fixed.op(e * o.amount / 1000);
                }
                k.w('<rect width="1" height="1" fill=', 32);
                k.w('"', 1);
                k.hex6(o.colour);
                k.w('" opacity="0">', 14);
                _anim(k, "x", 1, xs, nc * o.loop, sh, true);
                _anim(k, "y", 1, ys, nc * o.loop, sh, true);
                _anim(k, "opacity", 7, env, nc, 0, false);
                k.w("</rect>", 7);
            }
        }
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "forge-std/=lib/forge-std/src/",
    "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.