{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "adaptive-theme",
  "title": "Adaptive Theme",
  "description": "Contrast helpers that lift muted type to WCAG AA against the canvas.",
  "files": [
    {
      "path": "registry/default/lib/adaptive-theme.ts",
      "content": "/** Contrast + OKLCH companion palettes for ThemeAdapt. */\n\nexport type Rgb = [number, number, number]\n\nexport type Oklch = { l: number; c: number; h: number }\n\nexport type ThemeSwatch = {\n  background: Rgb\n  foreground: Rgb\n  muted: Rgb\n  brand: Rgb\n}\n\nconst TARGET_AA = 4.5\n\nexport const COMPANION_VAR_KEYS = [\n  \"--background\",\n  \"--foreground\",\n  \"--muted-foreground\",\n  \"--muted\",\n  \"--card\",\n  \"--card-foreground\",\n  \"--popover\",\n  \"--popover-foreground\",\n  \"--border\",\n  \"--border-subtle\",\n  \"--brand\",\n  \"--primary\",\n  \"--input\",\n  \"--ring\",\n  \"--surface\",\n] as const\n\nexport function srgbToLinear(c: number): number {\n  const x = c / 255\n  return x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4\n}\n\nexport function linearToSrgb(c: number): number {\n  const x = c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055\n  return Math.max(0, Math.min(255, Math.round(x * 255)))\n}\n\nexport function relativeLuminance(rgb: Rgb): number {\n  const [r, g, b] = rgb\n  return (\n    0.2126 * srgbToLinear(r) +\n    0.7152 * srgbToLinear(g) +\n    0.0722 * srgbToLinear(b)\n  )\n}\n\nexport function contrastRatio(a: Rgb, b: Rgb): number {\n  const l1 = relativeLuminance(a)\n  const l2 = relativeLuminance(b)\n  const lighter = Math.max(l1, l2)\n  const darker = Math.min(l1, l2)\n  return (lighter + 0.05) / (darker + 0.05)\n}\n\nexport function parseRgb(css: string): Rgb | null {\n  const t = css.trim()\n  const m = t.match(\n    /^rgba?\\(\\s*([0-9.]+)\\s*[, ]\\s*([0-9.]+)\\s*[, ]\\s*([0-9.]+)/i\n  )\n  if (!m?.[1] || !m[2] || !m[3]) return null\n  const r = Number(m[1])\n  const g = Number(m[2])\n  const b = Number(m[3])\n  if (![r, g, b].every((n) => Number.isFinite(n))) return null\n  return [r, g, b]\n}\n\nexport function parseHex(hex: string): Rgb | null {\n  const m = hex.trim().match(/^#([0-9a-fA-F]{6})$/)\n  if (!m?.[1]) return null\n  const n = Number.parseInt(m[1], 16)\n  if (!Number.isFinite(n)) return null\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255]\n}\n\nexport function clampContrast(minRatio: number): number {\n  if (!Number.isFinite(minRatio)) return 4.5\n  return Math.min(21, Math.max(1, minRatio))\n}\n\nexport function rgbToCss(rgb: Rgb): string {\n  const r = Math.max(0, Math.min(255, Math.round(rgb[0])))\n  const g = Math.max(0, Math.min(255, Math.round(rgb[1])))\n  const b = Math.max(0, Math.min(255, Math.round(rgb[2])))\n  return `rgb(${r} ${g} ${b})`\n}\n\nexport function mixRgb(from: Rgb, to: Rgb, t: number): Rgb {\n  return [\n    from[0] + (to[0] - from[0]) * t,\n    from[1] + (to[1] - from[1]) * t,\n    from[2] + (to[2] - from[2]) * t,\n  ]\n}\n\nexport function rgbToOklch(rgb: Rgb): Oklch {\n  const r = srgbToLinear(rgb[0])\n  const g = srgbToLinear(rgb[1])\n  const b = srgbToLinear(rgb[2])\n  const l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b\n  const m = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b\n  const s = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b\n  const l_ = Math.cbrt(l)\n  const m_ = Math.cbrt(m)\n  const s_ = Math.cbrt(s)\n  const L = 0.2104542553 * l_ + 0.793617785 * m_ - 0.0040720468 * s_\n  const a = 1.9779984951 * l_ - 2.428592205 * m_ + 0.4505937099 * s_\n  const b2 = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.808675766 * s_\n  const C = Math.hypot(a, b2)\n  let h = (Math.atan2(b2, a) * 180) / Math.PI\n  if (h < 0) h += 360\n  return { l: L, c: C, h }\n}\n\nexport function oklchToRgb({ l: L, c: C, h }: Oklch): Rgb {\n  const hr = (h * Math.PI) / 180\n  const a = Math.cos(hr) * C\n  const b = Math.sin(hr) * C\n  const l_ = L + 0.3963377774 * a + 0.2158037573 * b\n  const m_ = L - 0.1055613458 * a - 0.0638541728 * b\n  const s_ = L - 0.0894841775 * a - 1.291485548 * b\n  const l = l_ * l_ * l_\n  const m = m_ * m_ * m_\n  const s = s_ * s_ * s_\n  const r = +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s\n  const g = -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s\n  const bl = -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s\n  return [linearToSrgb(r), linearToSrgb(g), linearToSrgb(bl)]\n}\n\nexport function hueDelta(a: number, b: number): number {\n  const d = Math.abs(a - b) % 360\n  return d > 180 ? 360 - d : d\n}\n\nfunction withHue(color: Oklch, fallback: Oklch): Oklch {\n  if (color.c >= 0.012) return color\n  return { l: color.l, c: Math.max(color.c, fallback.c * 0.3), h: fallback.h }\n}\n\n/** Raise or lower OKLCH lightness only until contrast passes. Keeps C and H. */\nexport function ensureContrastOklch(\n  foreground: Rgb,\n  background: Rgb,\n  minRatio = TARGET_AA\n): { rgb: Rgb; repaired: boolean; ratio: number } {\n  const start = contrastRatio(foreground, background)\n  if (start >= minRatio) {\n    return { rgb: foreground, repaired: false, ratio: start }\n  }\n  const fg = rgbToOklch(foreground)\n  const towardLight = relativeLuminance(foreground) >= relativeLuminance(background)\n  let lo = towardLight ? fg.l : 0\n  let hi = towardLight ? 1 : fg.l\n  let best = foreground\n  for (let i = 0; i < 20; i++) {\n    const mid = (lo + hi) / 2\n    const cand = oklchToRgb({ l: mid, c: fg.c, h: fg.h })\n    if (contrastRatio(cand, background) >= minRatio) {\n      best = cand\n      if (towardLight) hi = mid\n      else lo = mid\n    } else if (towardLight) {\n      lo = mid\n    } else {\n      hi = mid\n    }\n  }\n  return { rgb: best, repaired: true, ratio: contrastRatio(best, background) }\n}\n\nexport function ensureContrast(\n  foreground: Rgb,\n  background: Rgb,\n  minRatio = TARGET_AA\n): { rgb: Rgb; repaired: boolean; ratio: number } {\n  return ensureContrastOklch(foreground, background, minRatio)\n}\n\nfunction roundRgb(rgb: Rgb): Rgb {\n  return [Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2])]\n}\n\n/**\n * Dark companion for a designed light palette.\n * naive: crush + grey brand (the usual damage).\n * adaptive: keep hue, drop L on the canvas, lift type to AA in OKLCH.\n */\nexport function companionDark(\n  light: ThemeSwatch,\n  kind: \"naive\" | \"adaptive\",\n  minRatio = TARGET_AA\n): ThemeSwatch {\n  const floor = clampContrast(minRatio)\n  if (kind === \"naive\") {\n    const background = roundRgb(mixRgb(light.background, [10, 10, 12], 0.9))\n    return {\n      background,\n      foreground: roundRgb(mixRgb(light.foreground, [228, 228, 226], 0.5)),\n      muted: roundRgb(mixRgb(light.muted, background, 0.72)),\n      brand: roundRgb(mixRgb(light.brand, [118, 118, 118], 0.5)),\n    }\n  }\n\n  const paper = rgbToOklch(light.background)\n  const ink = withHue(rgbToOklch(light.foreground), paper)\n  const quiet = withHue(rgbToOklch(light.muted), paper)\n  const mark = withHue(rgbToOklch(light.brand), paper)\n\n  const background = oklchToRgb({\n    l: 0.17,\n    c: paper.c * 0.5,\n    h: paper.h,\n  })\n  const foreground = ensureContrastOklch(\n    oklchToRgb({ l: 0.93, c: Math.min(ink.c, 0.04), h: ink.h }),\n    background,\n    floor\n  ).rgb\n  const muted = ensureContrastOklch(\n    oklchToRgb({ l: 0.72, c: Math.max(quiet.c * 0.8, 0.02), h: quiet.h }),\n    background,\n    floor\n  ).rgb\n  const brand = oklchToRgb({\n    l: Math.max(mark.l, 0.62),\n    c: mark.c,\n    h: mark.h,\n  })\n\n  return { background, foreground, muted, brand }\n}\n\nexport function companionToCssVars(swatch: ThemeSwatch): Record<string, string> {\n  const paper = rgbToOklch(swatch.background)\n  const card = oklchToRgb({\n    l: Math.min(0.24, paper.l + 0.05),\n    c: paper.c * 0.85,\n    h: paper.h,\n  })\n  const border = oklchToRgb({\n    l: Math.min(0.34, paper.l + 0.12),\n    c: paper.c * 0.45,\n    h: paper.h,\n  })\n  const mutedBg = oklchToRgb({\n    l: Math.min(0.22, paper.l + 0.04),\n    c: paper.c * 0.65,\n    h: paper.h,\n  })\n  return {\n    \"--background\": rgbToCss(swatch.background),\n    \"--foreground\": rgbToCss(swatch.foreground),\n    \"--muted-foreground\": rgbToCss(swatch.muted),\n    \"--muted\": rgbToCss(mutedBg),\n    \"--card\": rgbToCss(card),\n    \"--card-foreground\": rgbToCss(swatch.foreground),\n    \"--popover\": rgbToCss(card),\n    \"--popover-foreground\": rgbToCss(swatch.foreground),\n    \"--border\": rgbToCss(border),\n    \"--border-subtle\": rgbToCss(border),\n    \"--brand\": rgbToCss(swatch.brand),\n    \"--primary\": rgbToCss(swatch.brand),\n    \"--input\": rgbToCss(border),\n    \"--ring\": rgbToCss(swatch.brand),\n    \"--surface\": rgbToCss(card),\n  }\n}\n\nexport function applyCompanionPalette(root: HTMLElement, swatch: ThemeSwatch) {\n  const vars = companionToCssVars(swatch)\n  for (const key of COMPANION_VAR_KEYS) {\n    const value = vars[key]\n    if (value) root.style.setProperty(key, value)\n  }\n  root.setAttribute(\"data-theme-adapt\", \"companion\")\n}\n\nexport function clearCompanionPalette(root: HTMLElement) {\n  for (const key of COMPANION_VAR_KEYS) {\n    root.style.removeProperty(key)\n  }\n  root.removeAttribute(\"data-theme-adapt\")\n}\n\nfunction isDarkSelector(selector: string): boolean {\n  return /(?:^|[\\s,.])dark\\b|\\[data-theme=[\"']dark[\"']\\]/.test(selector)\n}\n\nfunction isRootSelector(selector: string): boolean {\n  return /(^|,\\s*)(:root|html)(\\s|,|$|:)/.test(selector) && !isDarkSelector(selector)\n}\n\nfunction collectFromRules(\n  rules: CSSRuleList,\n  out: Record<string, string>\n) {\n  for (const rule of rules) {\n    if (\n      rule instanceof CSSMediaRule ||\n      rule instanceof CSSSupportsRule ||\n      rule instanceof CSSLayerBlockRule ||\n      (typeof CSSGroupingRule !== \"undefined\" && rule instanceof CSSGroupingRule)\n    ) {\n      if (\n        rule instanceof CSSMediaRule &&\n        /prefers-color-scheme:\\s*dark/.test(rule.conditionText)\n      ) {\n        continue\n      }\n      collectFromRules(rule.cssRules, out)\n      continue\n    }\n    if (!(rule instanceof CSSStyleRule)) continue\n    if (!isRootSelector(rule.selectorText)) continue\n    for (let i = 0; i < rule.style.length; i++) {\n      const name = rule.style.item(i)\n      if (name.startsWith(\"--\")) {\n        out[name] = rule.style.getPropertyValue(name).trim()\n      }\n    }\n  }\n}\n\nexport function collectLightRootProps(\n  sheets: ArrayLike<CSSStyleSheet>\n): Record<string, string> {\n  const out: Record<string, string> = {}\n  for (let i = 0; i < sheets.length; i++) {\n    const sheet = sheets[i]\n    if (!sheet) continue\n    let rules: CSSRuleList\n    try {\n      rules = sheet.cssRules\n    } catch {\n      continue\n    }\n    collectFromRules(rules, out)\n  }\n  return out\n}\n\nfunction readVarRgb(probe: HTMLElement, token: string): Rgb | null {\n  probe.style.backgroundColor = `var(${token})`\n  return parseRgb(getComputedStyle(probe).backgroundColor)\n}\n\nlet lightCache: ThemeSwatch | null = null\n\nfunction swatchFromProbe(\n  probe: HTMLElement\n): ThemeSwatch | null {\n  const background = readVarRgb(probe, \"--background\")\n  const foreground = readVarRgb(probe, \"--foreground\")\n  const muted =\n    readVarRgb(probe, \"--muted-foreground\") ?? readVarRgb(probe, \"--muted\")\n  const brand = readVarRgb(probe, \"--brand\") ?? readVarRgb(probe, \"--primary\")\n  if (!background || !foreground || !muted || !brand) return null\n  return { background, foreground, muted, brand }\n}\n\n/** Snapshot the authored light :root palette, even if html currently has .dark. */\nexport function sampleLightSwatch(): ThemeSwatch | null {\n  if (typeof document === \"undefined\") return null\n  const props = collectLightRootProps(document.styleSheets)\n  if (Object.keys(props).length > 0) {\n    const probe = document.createElement(\"div\")\n    probe.setAttribute(\"aria-hidden\", \"true\")\n    probe.style.cssText =\n      \"position:fixed;left:-9999px;top:0;width:2px;height:2px;pointer-events:none\"\n    for (const [key, value] of Object.entries(props)) {\n      probe.style.setProperty(key, value)\n    }\n    document.body.appendChild(probe)\n    try {\n      const swatch = swatchFromProbe(probe)\n      if (swatch) lightCache = swatch\n      return swatch ?? lightCache\n    } finally {\n      probe.remove()\n    }\n  }\n  if (!document.documentElement.classList.contains(\"dark\")) {\n    const live = swatchFromProbe(document.documentElement)\n    if (live) lightCache = live\n    return live\n  }\n  return lightCache\n}\n",
      "type": "registry:lib",
      "target": "lib/adaptive-theme.ts"
    }
  ],
  "type": "registry:lib"
}