{"version":3,"file":"cssGlow.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/VoiceVisualization/cssGlow.ts"],"sourcesContent":["import { clamp, mix, saturate, vivid } from './math';\nimport type { Hsv, VoiceAgentState } from './types';\n\n/*\n * The CSS glow is built the way the shader composes the glow: an opaque color field (a base and\n * the masses mixed over it in order, with smoothstep falloffs) seen through an alpha that the\n * first mass makes, 0.15 + 0.6 b0, as the shader's 0.15 + 0.6 (b0 + 0.8 b1) without its fainter\n * second term. The gradients below are what the stylesheet cannot know ahead of time: the\n * falloffs, the mask and the palette.\n */\n\n/** R as a share of the globe's width: the globe is the soft edge, 1.06 R across each side. */\nconst R = 100 / 2.12;\n\n/**\n * The four orbits, from the shader's angles: the period of a turn at playback rate 1 (2π over its\n * share of the orbit spin), its direction, starting angle and distance from the centre in R, and\n * how long a sway takes in place of the noise that wobbles it.\n */\nexport const ORBITS = [\n  { direction: 'normal', distance: 0.4, period: 15, start: 0, sway: 8.3 },\n  { direction: 'reverse', distance: 0.38, period: 18.5, start: 115, sway: 10.1 },\n  { direction: 'normal', distance: 0.36, period: 21, start: 57, sway: 9.1 },\n  { direction: 'reverse', distance: 0.34, period: 24, start: 200, sway: 11.3 },\n] as const;\n\ntype Mass = {\n  color: string;\n  orbit: number;\n  reach: number;\n  weight?: string;\n};\n\n/** The masses in the order the shader mixes them, each reaching `reach` R. */\nexport const MASSES: Mass[] = [\n  { color: 'var(--str-ai-glow-k0)', orbit: 0, reach: 1.15 },\n  { color: 'var(--str-ai-glow-col-b)', orbit: 1, reach: 1.3 },\n  { color: 'var(--str-ai-glow-col-c)', orbit: 0, reach: 1.15, weight: '0.5' },\n  {\n    color: 'var(--str-ai-glow-col-d)',\n    orbit: 2,\n    reach: 1.2,\n    weight: 'var(--str-ai-glow-w3, 0)',\n  },\n  {\n    color: 'var(--str-ai-glow-col-e)',\n    orbit: 3,\n    reach: 1.25,\n    weight: 'var(--str-ai-glow-w4, 0)',\n  },\n];\n\n/**\n * The mass that carries the glow's opacity along its orbit: its orbit, reach and peak alpha. The\n * shader's second, fainter one is left out, as it costs a full layer for little the eye sees.\n */\nexport const RIDING_BODY = { alpha: 0.6, orbit: 0, reach: 1.15 };\n\nconst smoothstep = (edge0: number, edge1: number, x: number) => {\n  const t = clamp((x - edge0) / (edge1 - edge0), 0, 1);\n  return t * t * (3 - 2 * t);\n};\n\n/** smoothstep(reach, 0.1, x) over 0..reach as gradient stops, each `paint(weight, offset)`. */\nconst falloff = (reach: number, paint: (weight: number, offset: string) => string) => {\n  const stops: string[] = [];\n  for (let k = 0; k <= 10; k++) {\n    const x = (reach * k) / 10;\n    stops.push(paint(smoothstep(reach, 0.1, x), `${((x / reach) * 100).toFixed(1)}%`));\n  }\n  return stops.join(', ');\n};\n\n/** A mass: its color weighted by the falloff, on its own circle. */\nexport const massBackground = ({ color, reach, weight = '1' }: Mass) =>\n  `radial-gradient(closest-side, ${falloff(\n    reach,\n    (w, at) =>\n      `color-mix(in srgb, ${color} calc(${weight} * ${(w * 100).toFixed(1)}%), transparent) ${at}`,\n  )})`;\n\n/** The opacity a mass carries along its orbit: its peak alpha times its falloff. */\nexport const orbitMask = (orbit: number, reach: number, alpha: number) => {\n  const size = `${(reach * R).toFixed(2)}%`;\n  const centre = `${(50 + ORBITS[orbit].distance * R).toFixed(2)}% 50%`;\n  return `radial-gradient(ellipse ${size} ${size} at ${centre}, ${falloff(\n    reach,\n    (w, at) => `rgb(0 0 0 / ${(alpha * w).toFixed(3)}) ${at}`,\n  )})`;\n};\n\n/** How far a mass sits from the centre, as a share of the globe. */\nexport const orbitDistance = (orbit: number) =>\n  `${(ORBITS[orbit].distance * R).toFixed(2)}%`;\n\ntype Rgb = [number, number, number];\n\nconst rgb = ([r, g, b]: Rgb) =>\n  `rgb(${Math.round(r * 255)} ${Math.round(g * 255)} ${Math.round(b * 255)})`;\n\nconst scaled = (c: Rgb, k: number): Rgb => [\n  clamp(c[0] * k, 0, 1),\n  clamp(c[1] * k, 0, 1),\n  clamp(c[2] * k, 0, 1),\n];\n\nconst saturated = (c: Rgb, amount: number): Rgb => {\n  const out = [...c];\n  saturate(out, amount);\n  return out as Rgb;\n};\n\nconst towardWhite = (c: Rgb, amount: number): Rgb => [\n  mix(c[0], 1, amount),\n  mix(c[1], 1, amount),\n  mix(c[2], 1, amount),\n];\n\n/** The field's last steps, saturate3(col, 1.15) and x1.12, folded into each color: both linear. */\nconst finish = (c: Rgb) => scaled(saturated(c, 1.15), 1.12);\n\n/**\n * The colors the CSS glow mixes, as the shader derives them from the palette: masses saturated\n * x1.7, the first color lightened or darkened where the palette has no color of its own, a\n * near-white tint of it for the base, the halo and a tap's ring. In custom properties for the\n * stylesheet.\n */\nexport const cssGlowPalette = (palette: Hsv[], dark: number): Record<string, string> => {\n  if (!palette.length) return {};\n  const colors = palette.map((color) => {\n    const out = [0, 0, 0];\n    vivid(color, dark, out);\n    return out as Rgb;\n  });\n  const [first] = colors;\n  const mass = (c: Rgb) => rgb(finish(saturated(c, 1.7)));\n  return {\n    '--str-ai-glow-base': rgb(finish(saturated(towardWhite(first, 0.06), 1.3))),\n    '--str-ai-glow-col-b': colors[1]\n      ? mass(colors[1])\n      : rgb(finish(saturated(scaled(first, 1.18), 1.3))),\n    '--str-ai-glow-col-c': colors[2]\n      ? mass(colors[2])\n      : rgb(finish(saturated(scaled(first, 0.85), 1.3))),\n    '--str-ai-glow-col-d': mass(colors[3] ?? first),\n    '--str-ai-glow-col-e': mass(colors[4] ?? first),\n    '--str-ai-glow-halo': rgb(towardWhite(first, dark ? 0.32 : 0.75)),\n    '--str-ai-glow-k0': mass(first),\n    '--str-ai-glow-ring': rgb(towardWhite(saturated(first, 1.7), 0.4)),\n    '--str-ai-glow-w3': colors.length > 3 ? '1' : '0',\n    '--str-ai-glow-w4': colors.length > 4 ? '1' : '0',\n  };\n};\n\n/**\n * Playback rates per state, from the driver's motion: `orbit` how fast the masses turn, `flow`\n * the slow turn of thinking, `tempo` everything else.\n */\nexport const CSS_GLOW_RATES: Record<\n  VoiceAgentState,\n  { flow: number; orbit: number; tempo: number }\n> = {\n  connecting: { flow: 0, orbit: 1, tempo: 0.8 },\n  idle: { flow: 0, orbit: 0.405, tempo: 0.32 },\n  listening: { flow: 0, orbit: 0.94, tempo: 0.72 },\n  speaking: { flow: 0, orbit: 1.375, tempo: 1 },\n  thinking: { flow: 1, orbit: 1.875, tempo: 1 },\n};\n\n/** Which rate an animation of the stylesheet plays at, or null for one-off animations. */\nexport const cssGlowRateKind = (animationName: string) => {\n  if (animationName.endsWith('-flow') || animationName.endsWith('-unflow')) return 'flow';\n  if (animationName.endsWith('-spin') || animationName.endsWith('-unspin'))\n    return 'orbit';\n  if (animationName.endsWith('-bounce') || animationName.endsWith('-ripple')) return null;\n  return 'tempo';\n};\n"],"mappings":";;;AAYA,IAAM,IAAI,MAAM;;;;;;AAOhB,IAAa,SAAS;CACpB;EAAE,WAAW;EAAU,UAAU;EAAK,QAAQ;EAAI,OAAO;EAAG,MAAM;CAAI;CACtE;EAAE,WAAW;EAAW,UAAU;EAAM,QAAQ;EAAM,OAAO;EAAK,MAAM;CAAK;CAC7E;EAAE,WAAW;EAAU,UAAU;EAAM,QAAQ;EAAI,OAAO;EAAI,MAAM;CAAI;CACxE;EAAE,WAAW;EAAW,UAAU;EAAM,QAAQ;EAAI,OAAO;EAAK,MAAM;CAAK;AAC7E;;AAUA,IAAa,SAAiB;CAC5B;EAAE,OAAO;EAAyB,OAAO;EAAG,OAAO;CAAK;CACxD;EAAE,OAAO;EAA4B,OAAO;EAAG,OAAO;CAAI;CAC1D;EAAE,OAAO;EAA4B,OAAO;EAAG,OAAO;EAAM,QAAQ;CAAM;CAC1E;EACE,OAAO;EACP,OAAO;EACP,OAAO;EACP,QAAQ;CACV;CACA;EACE,OAAO;EACP,OAAO;EACP,OAAO;EACP,QAAQ;CACV;AACF;;;;;AAMA,IAAa,cAAc;CAAE,OAAO;CAAK,OAAO;CAAG,OAAO;AAAK;AAE/D,IAAM,cAAc,OAAe,OAA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