Spaces:
Running
Running
Download render.js from eredo-house/luce: direct link, hf CLI and curl.
- Browser
- Download file 16.4 kB
-
https://huggingface.co/spaces/eredo-house/luce/resolve/main/render.js
- Command line
-
hf download hf://spaces/eredo-house/luce/render.js
-
curl -L -o render.js https://huggingface.co/spaces/eredo-house/luce/resolve/main/render.js
16.4 kB
| /* LUCE spec renderer: draws the piece exactly as configured (shape, carat to scale, metal, setting, add-ons). */ | |
| (function () { | |
| var uid = 0; | |
| var METAL = { | |
| "14k yellow": ["#f3d79a", "#c9a25a", "#8a6a2f"], "18k yellow": ["#f6d58a", "#d1a246", "#8c6420"], | |
| "14k white": ["#f4f5f7", "#c4c8ce", "#80858d"], "18k white": ["#f7f8fa", "#cbd0d6", "#868b93"], | |
| "14k rose": ["#f3c9b4", "#cf9277", "#8f5a45"], "18k rose": ["#f5c6ad", "#d18d6e", "#8d5440"], | |
| "Platinum": ["#eef0f3", "#b9bec6", "#767c85"] | |
| }; | |
| function mm(ct) { return 6.5 * Math.cbrt(Math.max(ct, 0.01)); } | |
| // parametric outline: n points, half-width a | |
| function outline(shape, a, n) { | |
| var b = a * ({ Oval: 1.4, Pear: 1.5, Marquise: 1.9, Emerald: 1.35, Radiant: 1.2, Cushion: 1.04 }[shape] || 1), pts = [], i, t, x, y, c, s, e; | |
| for (i = 0; i < n; i++) { | |
| t = (i / n) * Math.PI * 2; c = Math.cos(t); s = Math.sin(t); | |
| if (shape === "Pear") { x = s * Math.pow(Math.abs(Math.sin(t / 2)), 1.4) * 1.22; y = -c; } | |
| else if (shape === "Marquise") { x = (s < 0 ? -1 : 1) * Math.pow(Math.abs(s), 1.5); y = -c; } | |
| else if (shape === "Round" || shape === "Oval" || !shape) { x = s; y = -c; } | |
| else { | |
| e = { Cushion: 4, Princess: 14, Asscher: 7, Emerald: 9, Radiant: 7 }[shape] || 4; | |
| x = (s < 0 ? -1 : 1) * Math.pow(Math.abs(s), 2 / e); y = -(c < 0 ? -1 : 1) * Math.pow(Math.abs(c), 2 / e); | |
| } | |
| pts.push([x * a, y * b]); | |
| } | |
| return pts; | |
| } | |
| function halfH(shape, w) { return w / 2 * ({ Oval: 1.4, Pear: 1.5, Marquise: 1.9, Emerald: 1.35, Radiant: 1.2, Cushion: 1.04 }[shape] || 1); } | |
| function poly(p, k) { return p.map(function (q) { return (q[0] * k).toFixed(1) + "," + (q[1] * k).toFixed(1); }).join(" "); } | |
| function shapePath(shape, w) { var p = outline(shape, w / 2, 64); return "M" + poly(p, 1).replace(/ /g, " L") + "Z"; } | |
| var SH0 = ["#ffffff", "#dfe7ef", "#9aa9b9", "#4c5866", "#c9d4df", "#2f3842", "#eef3f8", "#7d8a98"], SHB = ["#cfe3ff", "#6fa3e8", "#2f63b8", "#14336e", "#5b8fd6", "#0d2552", "#a9ccff", "#24509a"], SH = SH0; | |
| function stone(x, y, w, shape, id, rot) { | |
| var g = '<g transform="translate(' + x.toFixed(1) + "," + y.toFixed(1) + ")" + (rot ? " rotate(" + rot + ")" : "") + '">', N = 16, P = outline(shape, w / 2, N * 2), i, k; | |
| var step = shape === "Emerald" || shape === "Asscher"; | |
| if (w < 16) { | |
| g += '<polygon points="' + poly(outline(shape, w / 2, 24), 1) + '" fill="url(#st' + id + ')" stroke="#fff" stroke-opacity=".7" stroke-width=".6"/>'; | |
| g += '<polygon points="' + poly(outline(shape, w / 2, 12), 0.5) + '" fill="#fff" opacity=".85"/>'; | |
| return g + "</g>"; | |
| } | |
| if (step) { | |
| [1, 0.8, 0.6, 0.42].forEach(function (k2, j) { g += '<polygon points="' + poly(P, k2) + '" fill="' + (SH === SHB ? ["#163a7a", "#6fa3e8", "#2a5cae", "#a9ccff"] : ["#5d6977", "#cfd8e2", "#7f8c9a", "#e9eff5"])[j] + '" stroke="#fff" stroke-opacity=".5" stroke-width=".7"/>'; }); | |
| [0.125, 0.375, 0.625, 0.875].forEach(function (f) { var q = P[Math.round(f * P.length) % P.length]; g += '<line x1="' + q[0].toFixed(1) + '" y1="' + q[1].toFixed(1) + '" x2="' + (q[0] * 0.42).toFixed(1) + '" y2="' + (q[1] * 0.42).toFixed(1) + '" stroke="#fff" stroke-opacity=".55" stroke-width=".8"/>'; }); | |
| } else { | |
| g += '<polygon points="' + poly(P, 1) + '" fill="#3a4450"/>'; | |
| for (i = 0; i < N * 2; i++) { | |
| var a = P[i], b = P[(i + 1) % (N * 2)], ta = [a[0] * 0.56, a[1] * 0.56], tb = [b[0] * 0.56, b[1] * 0.56]; | |
| k = (i * 5 + 3) % SH.length; | |
| g += '<polygon points="' + poly([a, b, tb], 1) + '" fill="' + SH[k] + '" stroke="#0e1114" stroke-opacity=".25" stroke-width=".4"/>'; | |
| g += '<polygon points="' + poly([a, tb, ta], 1) + '" fill="' + SH[(k + 3) % SH.length] + '" stroke="#0e1114" stroke-opacity=".25" stroke-width=".4"/>'; | |
| } | |
| g += '<polygon points="' + poly(P, 0.56) + '" fill="url(#st' + id + ')" stroke="#fff" stroke-opacity=".6" stroke-width=".6"/>'; | |
| for (i = 0; i < N * 2; i += 4) { var q2 = P[i]; g += '<line x1="0" y1="0" x2="' + (q2[0] * 0.56).toFixed(1) + '" y2="' + (q2[1] * 0.56).toFixed(1) + '" stroke="#8796a6" stroke-opacity=".35" stroke-width=".5"/>'; } | |
| } | |
| g += '<polygon points="' + poly(P, 1) + '" fill="none" stroke="#fff" stroke-opacity=".9" stroke-width="' + Math.max(0.8, w / 90) + '"/>'; | |
| var f = w * 0.09; g += '<path d="M' + (-w * 0.18) + "," + (-w * 0.2 - f) + " l" + f * 0.3 + "," + f * 0.7 + " l" + f * 0.7 + "," + f * 0.3 + " l" + -f * 0.7 + "," + f * 0.3 + " l" + -f * 0.3 + "," + f * 0.7 + " l" + -f * 0.3 + "," + -f * 0.7 + " l" + -f * 0.7 + "," + -f * 0.3 + " l" + f * 0.7 + "," + -f * 0.3 + 'Z" fill="#fff"/>'; | |
| return g + "</g>"; | |
| } | |
| function dots(points, d, id) { var keep = SH; SH = SH0; var o = points.map(function (p) { return stone(p[0], p[1], d, "Round", id); }).join(""); SH = keep; return o; } | |
| function arc(cx, cy, rx, ry, a0, a1, n) { var o = []; for (var i = 0; i < n; i++) { var a = a0 + (a1 - a0) * (n === 1 ? 0.5 : i / (n - 1)); o.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); } return o; } | |
| function outlinePts(shape, w, off, n) { var hw = w / 2 + off; return outline(shape, hw, n).map(function (p) { return [p[0], p[1] * (halfH(shape, w) + off) / (halfH(shape, w) * (hw / (w / 2)) || 1)]; }); } | |
| window.lucePieceSVG = function (spec) { | |
| SH = spec.colour === "Blue" ? SHB : SH0; | |
| var id = ++uid, piece = spec.piece || "Solitaire", shape = spec.shape || "Round", ct = +spec.ct || 1; | |
| var M = METAL[spec.metal] || METAL["14k yellow"], add = spec.addons || [], has = function (k) { return add.indexOf(k) > -1; }; | |
| var matte = has("finish"), K = 13; // px per mm | |
| var defs = '<defs><linearGradient id="m' + id + '" x1="0" y1="0" x2="1" y2="0"><stop offset="0" stop-color="' + M[2] + '"/><stop offset=".22" stop-color="' + (matte ? M[1] : M[0]) + '"/><stop offset=".45" stop-color="' + M[1] + '"/><stop offset=".62" stop-color="' + (matte ? M[1] : M[0]) + '"/><stop offset="1" stop-color="' + M[2] + '"/></linearGradient>' + | |
| '<linearGradient id="mb' + id + '" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="' + M[2] + '"/><stop offset="1" stop-color="' + M[1] + '" stop-opacity="0.7"/></linearGradient>' + | |
| '<radialGradient id="st' + id + '" cx="0.35" cy="0.3" r="0.9"><stop offset="0" stop-color="#ffffff"/><stop offset="0.5" stop-color="' + (spec.colour === "Blue" ? "#7fb0f0" : "#cdd8e3") + '"/><stop offset="1" stop-color="' + (spec.colour === "Blue" ? "#1d3f80" : "#6f7f90") + '"/></radialGradient><radialGradient id="rb' + id + '" cx="0.35" cy="0.3" r="0.8"><stop offset="0" stop-color="#ff9aa8"/><stop offset="0.45" stop-color="#c0142e"/><stop offset="1" stop-color="#4a0510"/></radialGradient>' + | |
| '<radialGradient id="bg' + id + '" cx="0.5" cy="0.42" r="0.7"><stop offset="0" stop-color="#2a231d"/><stop offset="1" stop-color="#14110f"/></radialGradient></defs>'; | |
| var out = '<rect width="400" height="500" fill="url(#bg' + id + ')"/>'; | |
| var mg = "url(#m" + id + ")", mb = "url(#mb" + id + ")"; | |
| var cx = 200, w = mm(ct) * K; | |
| if (/Tennis/.test(piece)) { | |
| var chain = piece === "Tennis chain", total = chain ? 140 : 52, per = ct / total, d = Math.max(9, Math.min(30, mm(per) * K * 0.9)); | |
| var n = Math.round((chain ? 640 : 520) / (d * 1.12)), pts = []; | |
| for (var i = 0; i < n; i++) { var t = i / (n - 1), a = Math.PI * (0.05 + 0.9 * t); pts.push([cx - 150 * Math.cos(a), 120 + (chain ? 300 : 250) * Math.sin(a)]); } | |
| out += pts.map(function (p) { return '<circle cx="' + p[0] + '" cy="' + p[1] + '" r="' + d * 0.62 + '" fill="' + mg + '"/>'; }).join(""); | |
| out += dots(pts, d, id); | |
| if (!chain) out += '<rect x="' + (cx - 150 * Math.cos(Math.PI * 0.05) - 10) + '" y="96" width="20" height="26" rx="3" fill="' + mg + '"/>'; | |
| } else if (piece === "Studs") { | |
| [130, 270].forEach(function (x) { | |
| out += '<rect x="' + (x - 3) + '" y="250" width="6" height="90" fill="' + mb + '"/>'; | |
| if (has("bezel")) out += '<path d="' + shapePath(shape, w + 10) + '" transform="translate(' + x + ',230)" fill="' + mg + '"/>'; | |
| else [[-1, -1], [1, -1], [1, 1], [-1, 1]].forEach(function (q) { out += '<circle cx="' + (x + q[0] * w * 0.36) + '" cy="' + (230 + q[1] * halfH(shape, w) * 0.72) + '" r="' + Math.max(4, w * 0.09) + '" fill="' + mg + '"/>'; }); | |
| out += stone(x, 230, w, shape, id); | |
| }); | |
| } else { | |
| // ring in three-quarter view: back of shank, head, front of shank | |
| out += '<g transform="translate(200,250) scale(1.18) translate(-200,-292)">'; | |
| var cigar = piece === "Cigar band", heavy = piece === "Signet" || piece === "Pinky ring", bw = cigar ? 46 : heavy ? 24 : 15, cy = 340, rx = 128, ry = 48; | |
| out += '<path d="M' + (cx - rx) + "," + cy + " A" + rx + "," + ry + " 0 0,1 " + (cx + rx) + "," + cy + '" fill="none" stroke="' + mb + '" stroke-width="' + bw + '"/>'; | |
| var hy = cy - ry - 34; | |
| if (cigar) { | |
| // wide domed band: no head | |
| } else if (piece === "Signet") { | |
| var fw = 140, fh = 112; | |
| out += '<path d="M' + (cx - 46) + "," + (cy - ry + 6) + " L" + (cx - fw / 2 + 6) + "," + (hy + 30) + " L" + (cx + fw / 2 - 6) + "," + (hy + 30) + " L" + (cx + 46) + "," + (cy - ry + 6) + 'Z" fill="' + mb + '"/>'; | |
| out += '<rect x="' + (cx - fw / 2) + '" y="' + (hy - fh / 2) + '" width="' + fw + '" height="' + fh + '" rx="34" fill="' + mg + '" stroke="' + M[2] + '" stroke-width="2"/>'; | |
| if (has("coin")) { | |
| out += '<circle cx="' + cx + '" cy="' + hy + '" r="48" fill="' + mg + '" stroke="' + M[2] + '" stroke-width="5" stroke-dasharray="2 3"/><circle cx="' + cx + '" cy="' + hy + '" r="38" fill="none" stroke="' + M[2] + '" stroke-width="1.2"/>'; | |
| out += '<text x="' + cx + '" y="' + (hy + 4) + '" text-anchor="middle" font-family="Georgia,serif" font-size="30" font-weight="700" fill="' + M[2] + '">3</text><text x="' + cx + '" y="' + (hy + 20) + '" text-anchor="middle" font-family="Georgia,serif" font-size="9" letter-spacing="1" fill="' + M[2] + '">DOLLARS</text><text x="' + cx + '" y="' + (hy - 24) + '" text-anchor="middle" font-family="Georgia,serif" font-size="8" letter-spacing="1" fill="' + M[2] + '">1854</text>'; | |
| } else if (has("handeng")) { | |
| for (var e = -3; e <= 3; e++) out += '<path d="M' + (cx - 58) + "," + (hy + e * 12) + " q29," + (e % 2 ? 8 : -8) + " 58,0 t58,0" + '" fill="none" stroke="' + M[2] + '" stroke-width="1.2" opacity="0.8"/>'; | |
| } | |
| if (has("rim")) out += dots(outlinePts("Cushion", fw - 20, 0, 22).map(function (p) { return [cx + p[0] * 1.02, hy + p[1] * 0.8]; }), 9, id); | |
| if (!has("coin")) { | |
| var sw = Math.min(w, 70); | |
| out += '<path d="' + shapePath(shape, sw + 8) + '" transform="translate(' + cx + "," + hy + ')" fill="' + M[2] + '"/>'; | |
| out += stone(cx, hy, sw, shape, id); | |
| } | |
| } else { | |
| var hh = halfH(shape, w); | |
| // gallery under the head | |
| out += '<path d="M' + (cx - 26) + "," + (cy - ry + 4) + " L" + (cx - w * 0.3) + "," + (hy + hh * 0.4) + " L" + (cx + w * 0.3) + "," + (hy + hh * 0.4) + " L" + (cx + 26) + "," + (cy - ry + 4) + 'Z" fill="' + mb + '"/>'; | |
| if (has("halo") && piece !== "Halo") out += dots(arc(cx, hy + hh * 0.55, w * 0.42, 10, 0.15, Math.PI - 0.15, 9), 6, id); | |
| if (has("side")) { var sw2 = mm(0.25) * K; out += stone(cx - w / 2 - sw2 / 2 - 4, hy + 6, sw2, shape, id) + stone(cx + w / 2 + sw2 / 2 + 4, hy + 6, sw2, shape, id); } | |
| if (piece === "Halo") { var hd = Math.max(7, w * 0.11); out += '<path d="' + shapePath(shape, w + hd * 2.6) + '" transform="translate(' + cx + "," + hy + ')" fill="' + mg + '"/>'; out += dots(outlinePts(shape, w, hd * 0.75, Math.round((w + hd * 3) * 3.2 / (hd * 1.15))).map(function (p) { return [cx + p[0], hy + p[1]]; }), hd, id); } | |
| if (has("bezel") || piece === "Pinky ring" && has("bezel")) out += '<path d="' + shapePath(shape, w + 12) + '" transform="translate(' + cx + "," + hy + ')" fill="' + mg + '" stroke="' + M[2] + '" stroke-width="1.5"/>'; | |
| if (has("ruby")) { | |
| var rw = Math.max(26, w * 0.95), rh = rw * 1.45, rxp = cx + w * 0.42 + rw * 0.3; | |
| out += '<ellipse cx="' + rxp + '" cy="' + (hy + 4) + '" rx="' + (rw / 2 + 5) + '" ry="' + (rh / 2 + 5) + '" fill="' + mg + '" transform="rotate(-28 ' + rxp + " " + (hy + 4) + ')"/>'; | |
| out += '<path d="' + shapePath("Pear", rw) + '" transform="translate(' + rxp + "," + (hy + 4) + ') rotate(152)" fill="url(#rb' + id + ')" stroke="#ffb3be" stroke-opacity=".5"/>'; | |
| out += '<ellipse cx="' + (rxp - rw * 0.15) + '" cy="' + (hy - rh * 0.12) + '" rx="' + rw * 0.12 + '" ry="' + rw * 0.2 + '" fill="#fff" opacity=".55" transform="rotate(-28 ' + rxp + " " + hy + ')"/>'; | |
| cx -= w * 0.3; | |
| } | |
| out += stone(cx, hy, w, shape, id); | |
| if (!has("bezel")) [[-1, -1], [1, -1], [1, 1], [-1, 1]].forEach(function (q) { out += '<circle cx="' + (cx + q[0] * w * 0.38) + '" cy="' + (hy + q[1] * hh * 0.78) + '" r="' + Math.max(4, w * 0.075) + '" fill="' + mg + '"/>'; }); | |
| } | |
| if (has("ruby")) cx = 200; | |
| // front of shank | |
| out += '<path d="M' + (cx - rx) + "," + cy + " A" + rx + "," + ry + " 0 0,0 " + (cx + rx) + "," + cy + '" fill="none" stroke="' + mg + '" stroke-width="' + bw + '"/>'; | |
| if (cigar && has("flush")) { out += '<circle cx="' + cx + '" cy="' + (cy + ry) + '" r="' + (Math.min(w, 40) / 2 + 4) + '" fill="' + M[2] + '"/>' + stone(cx, cy + ry, Math.min(w, 40), shape, id); } | |
| if (!matte) out += '<path d="M' + (cx - rx * 0.6) + "," + (cy + ry * 0.82) + " A" + rx + "," + ry + " 0 0,0 " + (cx + rx * 0.2) + "," + (cy + ry * 0.98) + '" fill="none" stroke="#fff" stroke-opacity="0.55" stroke-width="2.5"/>'; | |
| if (piece === "Band") out += dots(arc(cx, cy, rx, ry, Math.PI * 0.08, Math.PI * 0.92, 15), 13, id); | |
| if (has("pave") && piece !== "Band") out += dots(arc(cx, cy, rx, ry, Math.PI * 1.08, Math.PI * 1.3, 4).concat(arc(cx, cy, rx, ry, Math.PI * 1.7, Math.PI * 1.92, 4)), 7, id); | |
| if (piece === "Halo") out += dots(arc(cx, cy, rx, ry, Math.PI * 1.06, Math.PI * 1.32, 4).concat(arc(cx, cy, rx, ry, Math.PI * 1.68, Math.PI * 1.94, 4)), 7, id); | |
| out += "</g>"; | |
| } | |
| return '<svg viewBox="0 0 400 500" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="' + piece + ", " + ct + " ct " + shape + ", " + (spec.metal || "") + '" preserveAspectRatio="xMidYMid slice">' + defs + out + "</svg>"; | |
| }; | |
| /* Photoreal studio render for a spec: piece + shape + metal tone, film variants for coin, ruby and blue. */ | |
| window.lucePhotoSrc = function (spec) { | |
| var piece = spec.piece || "Solitaire", shape = String(spec.shape || "Round").toLowerCase(), a = spec.addons || [], m = String(spec.metal || "14k yellow").toLowerCase(); | |
| var t = m.indexOf("rose") > -1 ? "r" : (m.indexOf("white") > -1 || m.indexOf("platinum") > -1) ? "w" : "y"; | |
| var SH = ["round", "oval", "emerald", "cushion", "pear", "princess", "radiant", "marquise", "asscher"]; | |
| if (SH.indexOf(shape) < 0) shape = "round"; | |
| var b; | |
| if (piece === "Signet") b = a.indexOf("coin") > -1 ? "coin" : "signet"; | |
| else if (piece === "Cigar band") b = a.indexOf("flush") > -1 ? "cigar-flush" : "cigar"; | |
| else if (piece === "Pinky ring") { if (a.indexOf("ruby") > -1) { b = "jersey"; t = "y"; } else if (spec.colour === "Blue") { b = "vegas"; t = "y"; } else b = "pinky-" + shape; } | |
| else if (piece === "Halo") b = "halo-" + shape; | |
| else if (piece === "Band") b = "band"; | |
| else if (piece === "Studs") b = "studs"; | |
| else if (piece === "Tennis chain") b = "chain"; | |
| else if (piece === "Tennis bracelet") b = "bracelet"; | |
| else b = "sol-" + shape; | |
| return "ph/" + b + "-" + t + ".jpg"; | |
| }; | |
| window.lucePhotoHTML = function (spec) { | |
| var alt = [spec.piece, spec.shape, spec.metal].filter(Boolean).join(", "); | |
| return '<img src="' + window.lucePhotoSrc(spec) + '" alt="' + alt + ' studio render" loading="lazy" decoding="async" />'; | |
| }; | |
| window.luceRenderAll = function () { | |
| document.querySelectorAll("[data-spec]").forEach(function (el) { try { el.innerHTML = window.lucePhotoHTML(JSON.parse(el.getAttribute("data-spec"))); } catch (e) {} }); | |
| }; | |
| if (document.readyState !== "loading") window.luceRenderAll(); else document.addEventListener("DOMContentLoaded", window.luceRenderAll); | |
| })(); | |