{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "color-picker",
  "title": "Color Picker",
  "description": "A color picker with a draggable saturation/brightness area, hue and opacity sliders, hex input, preset swatches and an optional screen eyedropper, all keyboard accessible.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/color-picker/color-picker.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ColorPickerSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface ColorPickerProps {\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (hex: string) => void;\n  showAlpha?: boolean;\n  showEyeDropper?: boolean;\n  presets?: string[];\n  label?: string;\n  helperText?: string;\n  size?: ColorPickerSize;\n  theme?: \"dark\" | \"light\";\n  width?: number | string;\n  disabled?: boolean;\n  className?: string;\n}\n\ninterface HSVA {\n  h: number;\n  s: number;\n  v: number;\n  a: number;\n}\n\nconst DEFAULT_PRESETS = [\"#F5F4F1\", \"#0A0A0A\", \"#87FFE3\", \"#F2A841\", \"#FF7A6B\", \"#8FB8FF\", \"#B8A6FF\", \"#4ADE80\", \"#FACC15\", \"#F472B6\"];\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.28)\", bg: \"rgba(255,255,255,0.03)\", field: \"rgba(255,255,255,0.05)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.24)\", focus: \"rgba(245,244,241,0.85)\", menuBg: \"#0E0E0E\", menuBorder: \"rgba(255,255,255,0.1)\", hover: \"rgba(255,255,255,0.08)\", swatchRing: \"rgba(255,255,255,0.18)\", thumbRing: \"#FFFFFF\", shadow: \"0 24px 60px rgba(0,0,0,0.55)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.28)\", bg: \"#FFFFFF\", field: \"rgba(10,10,10,0.04)\", border: \"rgba(10,10,10,0.16)\", borderHover: \"rgba(10,10,10,0.3)\", focus: \"rgba(10,10,10,0.85)\", menuBg: \"#FFFFFF\", menuBorder: \"rgba(10,10,10,0.14)\", hover: \"rgba(10,10,10,0.06)\", swatchRing: \"rgba(10,10,10,0.16)\", thumbRing: \"#FFFFFF\", shadow: \"0 20px 50px rgba(0,0,0,0.18)\" },\n};\n\nconst SIZES: Record<ColorPickerSize, { font: number; height: number; padX: number; radius: number; swatch: number }> = {\n  sm: { font: 13, height: 36, padX: 8, radius: 11, swatch: 22 },\n  md: { font: 14.5, height: 44, padX: 10, radius: 13, swatch: 28 },\n  lg: { font: 16, height: 52, padX: 12, radius: 15, swatch: 34 },\n};\n\nconst clamp = (n: number, lo = 0, hi = 1) => Math.min(hi, Math.max(lo, n));\n\nfunction hsvToRgb(h: number, s: number, v: number): [number, number, number] {\n  const f = (n: number) => {\n    const k = (n + h / 60) % 6;\n    return v - v * s * Math.max(0, Math.min(k, 4 - k, 1));\n  };\n  return [Math.round(f(5) * 255), Math.round(f(3) * 255), Math.round(f(1) * 255)];\n}\n\nfunction rgbToHsv(r: number, g: number, b: number): [number, number, number] {\n  r /= 255; g /= 255; b /= 255;\n  const max = Math.max(r, g, b);\n  const min = Math.min(r, g, b);\n  const d = max - min;\n  let h = 0;\n  if (d !== 0) {\n    if (max === r) h = ((g - b) / d) % 6;\n    else if (max === g) h = (b - r) / d + 2;\n    else h = (r - g) / d + 4;\n    h *= 60;\n    if (h < 0) h += 360;\n  }\n  return [h, max === 0 ? 0 : d / max, max];\n}\n\nconst hex2 = (n: number) => Math.round(n).toString(16).padStart(2, \"0\").toUpperCase();\n\nfunction toHex({ h, s, v, a }: HSVA, withAlpha: boolean) {\n  const [r, g, b] = hsvToRgb(h, s, v);\n  return `#${hex2(r)}${hex2(g)}${hex2(b)}${withAlpha && a < 1 ? hex2(a * 255) : \"\"}`;\n}\n\nfunction parseHex(input: string): HSVA | null {\n  let x = input.trim().replace(/^#/, \"\");\n  if (/^[0-9a-f]{3,4}$/i.test(x)) x = x.split(\"\").map((c) => c + c).join(\"\");\n  if (!/^([0-9a-f]{6}|[0-9a-f]{8})$/i.test(x)) return null;\n  const r = parseInt(x.slice(0, 2), 16);\n  const g = parseInt(x.slice(2, 4), 16);\n  const b = parseInt(x.slice(4, 6), 16);\n  const a = x.length === 8 ? parseInt(x.slice(6, 8), 16) / 255 : 1;\n  const [h, s, v] = rgbToHsv(r, g, b);\n  return { h, s, v, a };\n}\n\nconst CHECKER = \"repeating-conic-gradient(#8884 0% 25%, transparent 0% 50%) 50% / 10px 10px\";\n\nconst subscribeNoop = () => () => {};\nconst detectEyeDropper = () => typeof window !== \"undefined\" && \"EyeDropper\" in window;\n\nfunction useDrag(onMove: (x: number, y: number) => void) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const handle = (e: React.PointerEvent) => {\n    const r = ref.current?.getBoundingClientRect();\n    if (!r) return;\n    onMove(clamp((e.clientX - r.left) / r.width), clamp((e.clientY - r.top) / r.height));\n  };\n  return {\n    ref,\n    onPointerDown: (e: React.PointerEvent) => {\n      e.currentTarget.setPointerCapture(e.pointerId);\n      handle(e);\n    },\n    onPointerMove: (e: React.PointerEvent) => {\n      if (e.buttons === 1) handle(e);\n    },\n  };\n}\n\nexport function ColorPicker({\n  value,\n  defaultValue = \"#F2A841\",\n  onValueChange,\n  showAlpha = false,\n  showEyeDropper = true,\n  presets = DEFAULT_PRESETS,\n  label,\n  helperText,\n  size = \"md\",\n  theme = \"dark\",\n  width = 260,\n  disabled = false,\n  className,\n}: ColorPickerProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const triggerRef = React.useRef<HTMLButtonElement>(null);\n  const controlled = value !== undefined;\n  const [hsva, setHsva] = React.useState<HSVA>(() => parseHex(value ?? defaultValue) ?? { h: 0, s: 0, v: 0, a: 1 });\n  const [open, setOpen] = React.useState(false);\n  const [hovered, setHovered] = React.useState(false);\n  const [draft, setDraft] = React.useState<string | null>(null);\n  const canPick = React.useSyncExternalStore(subscribeNoop, detectEyeDropper, () => false);\n\n  const hex = toHex(hsva, showAlpha);\n  if (controlled) {\n    const incoming = parseHex(value);\n    if (incoming && toHex(incoming, showAlpha) !== hex) setHsva(incoming);\n  }\n\n  const update = (patch: Partial<HSVA> | HSVA) => {\n    const next = { ...hsva, ...patch };\n    setHsva(next);\n    setDraft(null);\n    onValueChange?.(toHex(next, showAlpha));\n  };\n\n  const [r, g, b] = hsvToRgb(hsva.h, hsva.s, hsva.v);\n  const pure = `hsl(${Math.round(hsva.h)} 100% 50%)`;\n  const solid = `rgb(${r} ${g} ${b})`;\n\n  const area = useDrag((x, y) => update({ s: x, v: 1 - y }));\n  const hueDrag = useDrag((x) => update({ h: x * 360 }));\n  const alphaDrag = useDrag((x) => update({ a: Math.round(x * 100) / 100 }));\n\n  const onAreaKey = (e: React.KeyboardEvent) => {\n    const d = e.shiftKey ? 0.1 : 0.01;\n    const map: Record<string, Partial<HSVA>> = {\n      ArrowLeft: { s: clamp(hsva.s - d) },\n      ArrowRight: { s: clamp(hsva.s + d) },\n      ArrowUp: { v: clamp(hsva.v + d) },\n      ArrowDown: { v: clamp(hsva.v - d) },\n    };\n    if (map[e.key]) {\n      e.preventDefault();\n      update(map[e.key]);\n    }\n  };\n  const onSliderKey = (key: \"h\" | \"a\") => (e: React.KeyboardEvent) => {\n    const unit = key === \"h\" ? 360 : 1;\n    const d = (e.shiftKey ? 0.1 : 0.01) * unit;\n    let next: number | null = null;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") next = hsva[key] + d;\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") next = hsva[key] - d;\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = unit;\n    if (next === null) return;\n    e.preventDefault();\n    update({ [key]: clamp(next, 0, unit) } as Partial<HSVA>);\n  };\n\n  const commitDraft = () => {\n    if (draft === null) return;\n    const parsed = parseHex(draft);\n    if (parsed) update(showAlpha ? parsed : { ...parsed, a: 1 });\n    setDraft(null);\n  };\n\n  const pickWithEyeDropper = async () => {\n    try {\n      const Ctor = (window as unknown as { EyeDropper: new () => { open: () => Promise<{ sRGBHex: string }> } }).EyeDropper;\n      const res = await new Ctor().open();\n      const parsed = parseHex(res.sRGBHex);\n      if (parsed) update({ ...parsed, a: hsva.a });\n    } catch {\n      /* user cancelled */\n    }\n  };\n\n  React.useEffect(() => {\n    if (!open) return;\n    const onDown = (e: MouseEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);\n    };\n    const onKey = (e: KeyboardEvent) => {\n      if (e.key === \"Escape\") {\n        setOpen(false);\n        triggerRef.current?.focus();\n      }\n    };\n    document.addEventListener(\"mousedown\", onDown);\n    document.addEventListener(\"keydown\", onKey);\n    return () => {\n      document.removeEventListener(\"mousedown\", onDown);\n      document.removeEventListener(\"keydown\", onKey);\n    };\n  }, [open]);\n\n  const borderColor = open ? p.focus : hovered ? p.borderHover : p.border;\n  const thumbStyle: React.CSSProperties = { position: \"absolute\", width: 16, height: 16, borderRadius: 999, border: `2.5px solid ${p.thumbRing}`, boxShadow: \"0 0 0 1px rgba(0,0,0,0.35), 0 2px 6px rgba(0,0,0,0.4)\", transform: \"translate(-50%, -50%)\", pointerEvents: \"none\" };\n\n  return (\n    <div ref={rootRef} className={cn(\"relative inline-flex flex-col gap-2\", className)} style={{ width, maxWidth: \"100%\", fontFamily: \"Inter, sans-serif\", opacity: disabled ? 0.5 : 1 }}>\n      {label && (\n        <label id={`${uid}-label`} className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font }}>\n          {label}\n        </label>\n      )}\n\n      <div className=\"relative\">\n        <button\n          ref={triggerRef}\n          type=\"button\"\n          aria-haspopup=\"dialog\"\n          aria-expanded={open}\n          aria-labelledby={label ? `${uid}-label` : undefined}\n          aria-label={label ? undefined : \"Choose color\"}\n          disabled={disabled}\n          onClick={() => setOpen((o) => !o)}\n          onMouseEnter={() => setHovered(true)}\n          onMouseLeave={() => setHovered(false)}\n          className=\"flex w-full items-center text-left outline-none\"\n          style={{ height: s.height, gap: 10, padding: `0 ${s.padX + 4}px 0 ${s.padX}px`, borderRadius: s.radius, background: p.bg, border: `1px solid ${borderColor}`, boxShadow: open ? `0 0 0 3px color-mix(in srgb, ${p.focus} 14%, transparent)` : \"0 0 0 0 transparent\", color: p.text, fontSize: s.font, fontWeight: 600, cursor: disabled ? \"default\" : \"pointer\", transition: \"border-color 0.18s ease, box-shadow 0.18s ease\" }}\n        >\n          <span className=\"relative shrink-0 overflow-hidden\" style={{ width: s.swatch, height: s.swatch, borderRadius: s.radius - 5, boxShadow: `inset 0 0 0 1px ${p.swatchRing}`, background: CHECKER }}>\n            <span className=\"absolute inset-0\" style={{ background: `rgb(${r} ${g} ${b} / ${hsva.a})` }} />\n          </span>\n          <span className=\"min-w-0 flex-1 truncate font-mono tabular-nums\" style={{ letterSpacing: \"0.02em\" }}>\n            {hex}\n          </span>\n          <svg width=\"14\" height=\"14\" viewBox=\"0 0 16 16\" fill=\"none\" stroke={p.muted} strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" style={{ transform: open ? \"rotate(180deg)\" : \"none\", transition: \"transform 0.2s ease\" }}>\n            <path d=\"M4 6L8 10L12 6\" />\n          </svg>\n        </button>\n\n        <AnimatePresence>\n          {open && (\n            <motion.div\n              role=\"dialog\"\n              aria-label=\"Color picker\"\n              className=\"absolute left-0 z-50 flex flex-col\"\n              style={{ top: \"calc(100% + 8px)\", width: Math.max(280, typeof width === \"number\" ? width : 280), gap: 12, padding: 12, borderRadius: s.radius + 6, border: `1px solid ${p.menuBorder}`, background: p.menuBg, boxShadow: p.shadow, transformOrigin: \"top left\" }}\n              initial={{ opacity: 0, y: -6, scale: 0.97 }}\n              animate={{ opacity: 1, y: 0, scale: 1 }}\n              exit={{ opacity: 0, y: -6, scale: 0.97 }}\n              transition={{ duration: 0.16, ease: [0.16, 1, 0.3, 1] }}\n            >\n              <div\n                {...area}\n                role=\"slider\"\n                tabIndex={0}\n                aria-label=\"Saturation and brightness\"\n                aria-valuetext={`Saturation ${Math.round(hsva.s * 100)}%, brightness ${Math.round(hsva.v * 100)}%`}\n                aria-valuenow={Math.round(hsva.s * 100)}\n                aria-valuemin={0}\n                aria-valuemax={100}\n                onKeyDown={onAreaKey}\n                className=\"relative cursor-crosshair touch-none rounded-xl outline-none focus-visible:ring-2\"\n                style={{ height: 150, background: `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, ${pure})`, [\"--tw-ring-color\" as string]: p.focus }}\n              >\n                <span style={{ ...thumbStyle, left: `${hsva.s * 100}%`, top: `${(1 - hsva.v) * 100}%`, background: solid }} />\n              </div>\n\n              <div className=\"flex items-center\" style={{ gap: 12 }}>\n                {canPick && showEyeDropper && (\n                  <button type=\"button\" aria-label=\"Pick color from screen\" onClick={pickWithEyeDropper} className=\"flex shrink-0 cursor-pointer items-center justify-center rounded-lg border-none outline-none focus-visible:ring-2\" style={{ width: 32, height: 32, background: p.field, color: p.text, [\"--tw-ring-color\" as string]: p.focus }}>\n                    <svg width=\"15\" height=\"15\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n                      <path d=\"M10.5 2.5l3 3-2 2-3-3 2-2ZM8.5 4.5l-5.2 5.2a1 1 0 0 0-.3.7V13h2.6a1 1 0 0 0 .7-.3L11.5 7.5\" />\n                    </svg>\n                  </button>\n                )}\n                <div className=\"flex flex-1 flex-col\" style={{ gap: 10 }}>\n                  <div\n                    {...hueDrag}\n                    role=\"slider\"\n                    tabIndex={0}\n                    aria-label=\"Hue\"\n                    aria-valuemin={0}\n                    aria-valuemax={360}\n                    aria-valuenow={Math.round(hsva.h)}\n                    onKeyDown={onSliderKey(\"h\")}\n                    className=\"relative cursor-pointer touch-none rounded-full outline-none focus-visible:ring-2\"\n                    style={{ height: 12, background: \"linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00)\", [\"--tw-ring-color\" as string]: p.focus }}\n                  >\n                    <span style={{ ...thumbStyle, top: \"50%\", left: `${(hsva.h / 360) * 100}%`, background: pure }} />\n                  </div>\n                  {showAlpha && (\n                    <div\n                      {...alphaDrag}\n                      role=\"slider\"\n                      tabIndex={0}\n                      aria-label=\"Opacity\"\n                      aria-valuemin={0}\n                      aria-valuemax={100}\n                      aria-valuenow={Math.round(hsva.a * 100)}\n                      onKeyDown={onSliderKey(\"a\")}\n                      className=\"relative cursor-pointer touch-none rounded-full outline-none focus-visible:ring-2\"\n                      style={{ height: 12, background: CHECKER, [\"--tw-ring-color\" as string]: p.focus }}\n                    >\n                      <span className=\"absolute inset-0 rounded-full\" style={{ background: `linear-gradient(to right, rgb(${r} ${g} ${b} / 0), rgb(${r} ${g} ${b}))` }} />\n                      <span style={{ ...thumbStyle, top: \"50%\", left: `${hsva.a * 100}%`, background: `rgb(${r} ${g} ${b} / ${hsva.a})` }} />\n                    </div>\n                  )}\n                </div>\n              </div>\n\n              <input\n                aria-label=\"Hex value\"\n                value={draft ?? hex}\n                spellCheck={false}\n                onChange={(e) => setDraft(e.target.value)}\n                onBlur={commitDraft}\n                onKeyDown={(e) => {\n                  if (e.key === \"Enter\") commitDraft();\n                  else if (e.key === \"Escape\") {\n                    e.stopPropagation();\n                    setDraft(null);\n                  }\n                }}\n                className=\"w-full rounded-lg border-none font-mono font-semibold uppercase tabular-nums outline-none focus-visible:ring-2\"\n                style={{ height: 34, padding: \"0 10px\", background: p.field, color: p.text, fontSize: s.font - 1.5, letterSpacing: \"0.04em\", [\"--tw-ring-color\" as string]: p.focus }}\n              />\n\n              {presets.length > 0 && (\n                <div className=\"flex flex-wrap justify-between\" style={{ gap: 6 }} role=\"group\" aria-label=\"Preset colors\">\n                  {presets.map((c) => {\n                    const active = hex.slice(0, 7).toUpperCase() === c.toUpperCase();\n                    return (\n                      <button\n                        key={c}\n                        type=\"button\"\n                        aria-label={`Use ${c}`}\n                        aria-pressed={active}\n                        onClick={() => {\n                          const parsed = parseHex(c);\n                          if (parsed) update({ ...parsed, a: hsva.a });\n                        }}\n                        className=\"cursor-pointer rounded-full border-none outline-none focus-visible:ring-2\"\n                        style={{ width: 20, height: 20, background: c, boxShadow: active ? `0 0 0 2px ${p.menuBg}, 0 0 0 4px ${p.text}` : `inset 0 0 0 1px ${p.swatchRing}`, transition: \"box-shadow 0.15s ease, transform 0.15s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n                        onMouseEnter={(e) => (e.currentTarget.style.transform = \"scale(1.12)\")}\n                        onMouseLeave={(e) => (e.currentTarget.style.transform = \"none\")}\n                      />\n                    );\n                  })}\n                </div>\n              )}\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </div>\n\n      {helperText && <span style={{ color: p.muted, fontSize: s.font - 1.5, lineHeight: 1.4 }}>{helperText}</span>}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}