{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "emoji-icon-picker-accent",
  "type": "registry:lib",
  "title": "Emoji Icon Picker accent sampling",
  "description": "Emoji Icon Picker accent sampling. Canonical Emoji Icon Picker owner.",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/lib/emoji-icon-picker-accent.ts",
      "type": "registry:lib",
      "target": "@lib/emoji-icon-picker-accent.ts",
      "content": "/** Opt-in browser accent extraction. Importing this module is safe during SSR. */\nconst SAMPLE_LIMIT = 4096;\n\nfunction colorQuality(r: number, g: number, b: number): number {\n  const max = Math.max(r, g, b);\n  const min = Math.min(r, g, b);\n  const saturation = max === 0 ? 0 : (max - min) / max;\n  const luminance = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;\n  if (luminance < 0.12 || luminance > 0.94 || saturation < 0.28) return 0;\n  const midTone = Math.max(0, 1 - Math.abs(luminance - 0.54) / 0.55);\n  return saturation * midTone;\n}\n\n/** Choose an accent from a bounded pixel sample, ignoring transparent and neutral noise. */\nexport function accentFromPixels(pixels: Uint8ClampedArray): string | null {\n  if (\n    !(pixels instanceof Uint8ClampedArray) ||\n    pixels.length === 0 ||\n    pixels.length % 4 !== 0\n  )\n    return null;\n  const total = pixels.length / 4;\n  const stride = Math.max(1, Math.ceil(total / SAMPLE_LIMIT));\n  const candidates: { r: number; g: number; b: number; quality: number }[] = [];\n  for (let pixel = 0; pixel < total; pixel += stride) {\n    const offset = pixel * 4;\n    if (pixels[offset + 3]! < 96) continue;\n    const r = pixels[offset]!;\n    const g = pixels[offset + 1]!;\n    const b = pixels[offset + 2]!;\n    const quality = colorQuality(r, g, b);\n    if (quality > 0) candidates.push({ r, g, b, quality });\n  }\n  if (!candidates.length) return null;\n  candidates.sort((a, b) => b.quality - a.quality);\n  const chosen = candidates.slice(\n    0,\n    Math.max(1, Math.ceil(candidates.length * 0.25)),\n  );\n  const weight = chosen.reduce((sum, pixel) => sum + pixel.quality, 0);\n  const channels = [\"r\", \"g\", \"b\"] as const;\n  const rgb = channels.map((channel) =>\n    Math.round(\n      chosen.reduce((sum, pixel) => sum + pixel[channel] * pixel.quality, 0) /\n        weight,\n    ),\n  );\n  return `#${rgb.map((channel) => channel.toString(16).padStart(2, \"0\")).join(\"\")}`;\n}\n\nexport type EmojiIconAccentSource =\n  | { readonly emoji: string }\n  | { readonly src: string };\nexport interface DetectEmojiIconAccentOptions {\n  readonly signal?: AbortSignal;\n  readonly size?: number;\n}\n\nfunction canvasAccent(\n  draw: (context: CanvasRenderingContext2D, size: number) => void,\n  size: number,\n): string | null {\n  if (\n    typeof document === \"undefined\" ||\n    typeof document.createElement !== \"function\"\n  )\n    return null;\n  try {\n    const canvas = document.createElement(\"canvas\");\n    canvas.width = size;\n    canvas.height = size;\n    const context = canvas.getContext(\"2d\", { willReadFrequently: true });\n    if (!context) return null;\n    draw(context, size);\n    return accentFromPixels(context.getImageData(0, 0, size, size).data);\n  } catch {\n    // Canvas may be unsupported or tainted by a cross-origin image.\n    return null;\n  }\n}\n\n/**\n * Explicitly sample a native glyph or caller supplied image. Image colors can be\n * unavailable when the host blocks cross-origin canvas access; that returns null.\n */\nexport function detectEmojiIconAccent(\n  source: EmojiIconAccentSource,\n  options: DetectEmojiIconAccentOptions = {},\n): Promise<string | null> {\n  if (options.signal?.aborted) return Promise.resolve(null);\n  const size = Number.isFinite(options.size)\n    ? Math.min(128, Math.max(16, Math.floor(options.size!)))\n    : 64;\n  if (\"emoji\" in source) {\n    return Promise.resolve(\n      canvasAccent((context, canvasSize) => {\n        context.textAlign = \"center\";\n        context.textBaseline = \"middle\";\n        context.font = `${Math.floor(canvasSize * 0.76)}px sans-serif`;\n        context.fillText(source.emoji, canvasSize / 2, canvasSize / 2);\n      }, size),\n    );\n  }\n  if (typeof document === \"undefined\" || typeof Image === \"undefined\")\n    return Promise.resolve(null);\n  return new Promise((resolve) => {\n    let settled = false;\n    let image: HTMLImageElement;\n    try {\n      image = new Image();\n    } catch {\n      resolve(null);\n      return;\n    }\n    image.crossOrigin = \"anonymous\";\n    const cleanup = () => {\n      image.onload = null;\n      image.onerror = null;\n      options.signal?.removeEventListener(\"abort\", onAbort);\n    };\n    const finish = (value: string | null) => {\n      if (settled) return;\n      settled = true;\n      cleanup();\n      resolve(value);\n    };\n    const onAbort = () => finish(null);\n    options.signal?.addEventListener(\"abort\", onAbort, { once: true });\n    image.onload = () =>\n      finish(\n        canvasAccent((context, canvasSize) => {\n          context.drawImage(image, 0, 0, canvasSize, canvasSize);\n        }, size),\n      );\n    image.onerror = () => finish(null);\n    try {\n      image.src = source.src;\n    } catch {\n      finish(null);\n    }\n  });\n}\n"
    }
  ]
}
