figures / runtime /hand.js
sergiopaniego's picture
sergiopaniego HF Staff
Brand teal accent, readable text colors, U4 nudge redesign, U7 figures
39824c1 verified
Raw History Blame Contribute Delete
8.01 kB
// Shared drawing kit for the course figures. Every figure uses it, so the whole course has one look.
// The default is the clean look of the HF blog figures: flat translucent fills, thin borders, sans text.
// ?style=hand brings back the earlier hand-drawn look (rough.js + Excalifont), kept only for comparison.
//
// const h = Hand.create(svg, { seed: 1 }); // svg: an <svg> element inside ctx.root
// const C = Hand.colors(ctx.root); // theme-aware colors, call inside render
// h.rect(x, y, w, h, { fill: C.blueSoft, stroke: C.blue });
// h.text(x, y, 'label', { size: 14, anchor: 'middle', color: C.muted });
// h.arrow([[x1, y1], [x2, y2]], { stroke: C.muted });
//
// Colors: C.blue etc. are for strokes and marks, C.blueSoft for fills, and C.blueText for TEXT drawn in a
// palette color (at least 4.5:1 on the card and on the Soft fill, in light and dark).
(function () {
const NS = 'http://www.w3.org/2000/svg';
const CLEAN = new URLSearchParams(location.search).get('style') !== 'hand';
const FONT = CLEAN ? "'Source Sans 3', 'Source Sans Pro', ui-sans-serif, system-ui, sans-serif" : "'Excalifont', 'Comic Sans MS', cursive";
// Fixed hues, picked to work on white and on the dark page (#0b0f19-ish). Soft variants are fills.
const LIGHT = {
blue: '#1971c2', blueSoft: '#a5d8ff', purple: '#7048e8', purpleSoft: '#d0bfff',
green: '#2f9e44', greenSoft: '#b2f2bb', red: '#e03131', redSoft: '#ffc9c9',
yellow: '#f08c00', yellowSoft: '#ffec99', grey: '#868e96', greySoft: '#e9ecef',
teal: '#0c8599', tealSoft: '#99e9f2', orange: '#e8590c', orangeSoft: '#ffd8a8',
};
const DARK = {
blue: '#74c0fc', blueSoft: '#1c3d5a', purple: '#b197fc', purpleSoft: '#3b2f63',
green: '#69db7c', greenSoft: '#1f4227', red: '#ff8787', redSoft: '#5a2328',
yellow: '#ffd43b', yellowSoft: '#5c4a10', grey: '#adb5bd', greySoft: '#2a2f3a',
teal: '#66d9e8', tealSoft: '#0f3d44', orange: '#ff922b', orangeSoft: '#5c3a14',
};
// Clean mode uses the research-article template's categorical palette (the HF blog figures):
// flat translucent fills with thin borders in the same hue, instead of Excalidraw's pastel set.
const TEMPLATE = { blue: '#49BDE9', purple: '#A47BD1', teal: '#26B9A7', yellow: '#C9AB4B', green: '#7FBF63', red: '#E5484D', grey: '#8B95A1', orange: '#DE895E' };
function rgba(hex, a) {
const n = parseInt(hex.slice(1), 16);
return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${a})`;
}
// Text variants: darker hues on the light card, and on the dark card the base hue where it already passes.
const TEXT_LIGHT = { blue: '#0b6e99', purple: '#7442b0', teal: '#0b7366', yellow: '#7a600b', green: '#367520', red: '#bf262c', grey: '#5b6470', orange: '#a84a1c' };
const TEXT_DARK = { ...TEMPLATE, purple: '#bb9be0', red: '#ff7a7e', grey: '#a3acb7' };
function cleanPalette(dark) {
const out = {};
for (const [k, v] of Object.entries(TEMPLATE)) {
out[k] = v; out[k + 'Soft'] = rgba(v, dark ? 0.26 : 0.16); out[k + 'Text'] = (dark ? TEXT_DARK : TEXT_LIGHT)[k];
}
return out;
}
function colors(root) {
const dark = document.documentElement.getAttribute('data-theme') === 'dark';
if (CLEAN) {
const css = (n, fb) => (getComputedStyle(root || document.documentElement).getPropertyValue(n).trim() || fb);
return { ...cleanPalette(dark), dark, ink: dark ? '#e5e7eb' : '#1f2937', muted: css('--muted-color', dark ? '#9ca3af' : '#6b7280'),
border: dark ? 'rgba(255,255,255,0.14)' : 'rgba(0,0,0,0.12)', bg: css('--page-bg', dark ? '#0f1115' : '#ffffff') };
}
const css = (n, fb) => (getComputedStyle(root || document.documentElement).getPropertyValue(n).trim() || fb);
const pal = dark ? DARK : LIGHT;
const text = Object.fromEntries(Object.keys(pal).filter((k) => !k.endsWith('Soft')).map((k) => [k + 'Text', pal[k]]));
return {
...pal,
...text,
dark,
ink: dark ? '#e9ecef' : '#1e1e1e',
muted: css('--muted-color', dark ? '#adb5bd' : '#495057'),
border: dark ? '#495057' : '#ced4da',
bg: css('--page-bg', dark ? '#0f1115' : '#ffffff'),
};
}
function create(svg, { seed = 1, roughness = 1.1 } = {}) {
const rc = rough.svg(svg);
let n = 0;
const s = () => seed * 1000 + (n++); // deterministic: same drawing on every render
const INK = new Set(['#1e1e1e', '#e9ecef', '#1f2937', '#e5e7eb', 'currentColor']);
const lightBorder = () => (document.documentElement.getAttribute('data-theme') === 'dark' ? 'rgba(255,255,255,0.22)' : 'rgba(0,0,0,0.22)');
const opt = (o = {}) => ({
roughness: CLEAN ? 0 : (o.roughness ?? roughness), bowing: CLEAN ? 0 : (o.bowing ?? 0.8),
disableMultiStroke: CLEAN, preserveVertices: CLEAN,
stroke: (CLEAN && o.fill && o.fill !== 'none' && INK.has(o.stroke ?? 'currentColor')) ? lightBorder() : (o.stroke ?? 'currentColor'), strokeWidth: CLEAN ? Math.min(o.width ?? 1, 1.2) : (o.width ?? 1.4),
fill: o.fill, fillStyle: o.fillStyle || 'solid', hachureGap: o.hachureGap ?? 5,
strokeLineDash: o.dashed ? [6, 5] : undefined, seed: o.seed ?? s(),
});
const add = (node) => (svg.appendChild(node), node);
return {
reset() { n = 0; },
rect: (x, y, w, h, o = {}) => add(o.round === false
? rc.rectangle(x, y, w, h, opt(o))
: rc.path(roundedPath(x, y, w, h, o.radius ?? (CLEAN ? Math.min(5, h / 4, w / 4) : Math.min(10, h / 3, w / 3))), opt(o))),
line: (x1, y1, x2, y2, o = {}) => add(rc.line(x1, y1, x2, y2, opt(o))),
ellipse: (cx, cy, w, h, o = {}) => add(rc.ellipse(cx, cy, w, h, opt(o))),
circle: (cx, cy, d, o = {}) => add(rc.circle(cx, cy, d, opt(o))),
path: (d, o = {}) => add(rc.path(d, opt(o))),
arrow(points, o = {}) {
const pts = points.map((p) => [p[0], p[1]]);
const g = document.createElementNS(NS, 'g');
g.appendChild(pts.length === 2 ? rc.line(...pts[0], ...pts[1], opt(o)) : rc.curve(pts, opt(o)));
const [ax, ay] = pts[pts.length - 2], [bx, by] = pts[pts.length - 1];
const a = Math.atan2(by - ay, bx - ax), L = o.head ?? 10;
[-0.45, 0.45].forEach((d) => g.appendChild(rc.line(bx, by, bx - L * Math.cos(a + d), by - L * Math.sin(a + d), opt(o))));
return add(g);
},
text(x, y, str, o = {}) {
const lines = String(str).split('\n');
const t = document.createElementNS(NS, 'text');
t.setAttribute('x', x); t.setAttribute('y', y);
t.setAttribute('fill', o.color || 'currentColor');
t.setAttribute('font-size', o.size || 14);
t.setAttribute('font-family', o.font || FONT);
if (o.anchor) t.setAttribute('text-anchor', o.anchor);
if (o.weight) t.setAttribute('font-weight', o.weight);
if (o.baseline) t.setAttribute('dominant-baseline', o.baseline);
lines.forEach((l, i) => {
const sp = document.createElementNS(NS, 'tspan');
sp.setAttribute('x', x);
if (i) sp.setAttribute('dy', (o.size || 14) * 1.25);
sp.textContent = l;
t.appendChild(sp);
});
return add(t);
},
};
}
function roundedPath(x, y, w, h, r) {
return `M${x + r},${y} H${x + w - r} Q${x + w},${y} ${x + w},${y + r} V${y + h - r} Q${x + w},${y + h} ${x + w - r},${y + h}` +
` H${x + r} Q${x},${y + h} ${x},${y + h - r} V${y + r} Q${x},${y} ${x + r},${y} Z`;
}
// One color per message role, the same in every figure of the course:
// system grey, user (the task) blue, assistant (the model being trained, the student) purple,
// tool (the environment) yellow, teacher teal. Red is for errors and failures, green for passes and rewards.
function role(C, name) {
const r = { system: 'grey', user: 'blue', assistant: 'purple', student: 'purple', tool: 'yellow', teacher: 'teal' }[name] || 'grey';
return { stroke: C[r], fill: C[r + 'Soft'] };
}
window.Hand = { create, colors, role, FONT, CLEAN };
})();