{"version":3,"file":"palette.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/VoiceVisualization/palette.ts"],"sourcesContent":["import type { Hsv } from './types';\n\nexport const MAXIMUM_COLORS = 5;\n\ntype Rgb = [number, number, number];\n\nlet normalizer: CanvasRenderingContext2D | null | undefined;\n\n/**\n * The browser's own reading of a CSS color, as `#rrggbb` or `rgba(...)`, so any syntax it\n * understands (names, `hsl()`, `color-mix()`, ...) reaches the parser below in one of two forms.\n * Null for a color the browser rejects.\n */\nconst normalize = (color: string) => {\n  if (normalizer === undefined) {\n    normalizer =\n      typeof document === 'undefined'\n        ? null\n        : document.createElement('canvas').getContext('2d');\n  }\n  if (!normalizer) return color;\n  // A rejected color leaves the previous value in place, so it reads back as whichever of two\n  // different starting values was there; an accepted one reads the same both times.\n  normalizer.fillStyle = '#000000';\n  normalizer.fillStyle = color;\n  const read = String(normalizer.fillStyle);\n  normalizer.fillStyle = '#ffffff';\n  normalizer.fillStyle = color;\n  return String(normalizer.fillStyle) === read ? read : null;\n};\n\nconst channel = (value: string) =>\n  value.trim().endsWith('%') ? parseFloat(value) / 100 : parseFloat(value) / 255;\n\n/** Reads `#rgb`, `#rrggbb` (with optional alpha) and `rgb()`/`rgba()` into 0-1 channels. */\nexport const parseColor = (color: string): Rgb | null => {\n  const normalized = normalize(color.trim());\n  if (normalized === null) return null;\n  const value = normalized.trim().toLowerCase();\n  const hex = /^#([0-9a-f]{3,8})$/.exec(value)?.[1];\n  if (hex) {\n    const full =\n      hex.length <= 4\n        ? hex\n            .slice(0, 3)\n            .split('')\n            .map((c) => c + c)\n            .join('')\n        : hex.slice(0, 6);\n    if (full.length !== 6) return null;\n    return [0, 2, 4].map((i) => parseInt(full.slice(i, i + 2), 16) / 255) as Rgb;\n  }\n  const rgb = /^rgba?\\(([^)]+)\\)$/.exec(value)?.[1];\n  if (rgb) {\n    const parts = rgb.split(/[\\s,/]+/).filter(Boolean);\n    if (parts.length < 3) return null;\n    const channels = parts.slice(0, 3).map(channel);\n    if (channels.some((c) => !Number.isFinite(c))) return null;\n    return channels.map((c) => Math.min(Math.max(c, 0), 1)) as Rgb;\n  }\n  return null;\n};\n\n/** RGB channels from 0 to 1 as HSV, keeping full saturation and value so muted colors render true. */\nexport const rgbToHsv = ([r, g, b]: Rgb): Hsv => {\n  const high = Math.max(r, g, b);\n  const low = Math.min(r, g, b);\n  const delta = high - low;\n  let hue = 0;\n  if (delta !== 0) {\n    if (high === r) hue = ((((g - b) / delta) % 6) + 6) % 6;\n    else if (high === g) hue = (b - r) / delta + 2;\n    else hue = (r - g) / delta + 4;\n  }\n  return {\n    hue: (((hue / 6) % 1) + 1) % 1,\n    saturation: high === 0 ? 0 : Math.min(Math.max(delta / high, 0), 1),\n    value: Math.min(Math.max(high, 0), 1),\n  };\n};\n\n/** CSS colors as the HSV the renderers read, at most five; colors that cannot be read are skipped. */\nexport const toHsvPalette = (colors: readonly string[]): Hsv[] =>\n  colors\n    .map(parseColor)\n    .filter((rgb): rgb is Rgb => rgb !== null)\n    .slice(0, MAXIMUM_COLORS)\n    .map(rgbToHsv);\n\n/** Relative luminance of a CSS color, or null when it cannot be read. */\nexport const luminance = (color: string) => {\n  const rgb = parseColor(color);\n  if (!rgb) return null;\n  const [r, g, b] = rgb.map((c) =>\n    c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4),\n  );\n  return 0.2126 * r + 0.7152 * g + 0.0722 * b;\n};\n\n/** Used when neither the `colors` prop nor the theme gives a readable color. */\nexport const FALLBACK_COLOR = '#005fff';\n\n/**\n * The theme a voice visualization reads from its root: the accent color (the root's `color`) as\n * a palette, empty when unreadable, and whether it sits on a dark backdrop (the root's\n * `text-decoration-color`, which carries the backdrop token and never paints).\n */\nexport const readVoiceTheme = (root: Element) => {\n  const style = getComputedStyle(root);\n  const backdrop = luminance(style.textDecorationColor || '');\n  return {\n    dark: backdrop !== null && backdrop < 0.5 ? 1 : 0,\n    palette: toHsvPalette(style.color ? [style.color] : []),\n  };\n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