{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "color-picker",
  "title": "Color Picker",
  "type": "registry:ui",
  "files": [
    {
      "path": "packages/registry-native/components/color-picker/ColorPicker.tsx",
      "content": "import * as React from 'react'\nimport { Platform, Pressable, View, type ViewProps } from 'react-native'\nimport Svg, { Defs, LinearGradient, Rect, Stop } from 'react-native-svg'\nimport { Input } from '@/components/ui/input'\nimport { Slider } from '@/components/ui/slider'\nimport { Text } from '@/components/ui/text'\nimport { cn } from '@/lib/utils'\nimport { hexToHsl, hslToHex, normalizeHex, type Hsla } from './color'\n\nexport type ColorSwatch = string | { value: string; label: string }\n\nexport interface ColorPickerLabels {\n  hue: string\n  saturation: string\n  lightness: string\n  alpha: string\n  hex: string\n  invalidHex: string\n  /** Prefix for the preview's spoken value, e.g. \"Current color #1E90FF\". */\n  current: string\n}\n\nexport interface ColorPickerProps extends Omit<ViewProps, 'children'> {\n  /** Controlled hex colour: `#RRGGBB`, or `#RRGGBBAA` when `alpha` is on. Any parseable hex is accepted. */\n  value?: string\n  defaultValue?: string\n  /** Called with the uppercase hex on every change. A controlled owner may refuse it. */\n  onValueChange?: (value: string) => void\n  /** Called once a slider drag ends, a swatch is chosen, or a hex entry is committed. */\n  onValueChangeComplete?: (value: string) => void\n  swatches?: ColorSwatch[]\n  /** Adds an opacity slider and emits `#RRGGBBAA`. */\n  alpha?: boolean\n  disabled?: boolean\n  labels?: Partial<ColorPickerLabels>\n  className?: string\n}\n\nconst DEFAULT_LABELS: ColorPickerLabels = {\n  hue: 'Hue',\n  saturation: 'Saturation',\n  lightness: 'Lightness',\n  alpha: 'Opacity',\n  hex: 'Hex color',\n  invalidHex: 'Enter a hex color such as #1E90FF.',\n  current: 'Current color',\n}\nconst FALLBACK = '#000000'\n// The slider announces percentages by default; hue reads in degrees instead.\nconst HUE_STEPS = Array.from({ length: 361 }, (_, i) => String(i))\n\ninterface GradientStop {\n  offset: number\n  color: string\n  opacity?: number\n}\n\n/** Gradient drawn under a transparent native slider track. Android's SeekBar insets its track by 16dp. */\nfunction Track({ id, stops }: { id: string; stops: GradientStop[] }) {\n  return (\n    <View\n      pointerEvents=\"none\"\n      className={cn(\n        'absolute top-[20px] h-[8px] overflow-hidden rounded-full',\n        Platform.OS === 'android' ? 'inset-x-[16px]' : 'inset-x-0',\n      )}\n    >\n      <Svg width=\"100%\" height=\"100%\">\n        <Defs>\n          <LinearGradient id={id} x1=\"0\" y1=\"0\" x2=\"1\" y2=\"0\">\n            {stops.map((stop) => (\n              <Stop key={stop.offset} offset={stop.offset} stopColor={stop.color} stopOpacity={stop.opacity ?? 1} />\n            ))}\n          </LinearGradient>\n        </Defs>\n        <Rect width=\"100%\" height=\"100%\" fill={`url(#${id})`} />\n      </Svg>\n    </View>\n  )\n}\n\nexport const ColorPicker = React.forwardRef<React.ComponentRef<typeof View>, ColorPickerProps>(\n  (\n    {\n      value,\n      defaultValue,\n      onValueChange,\n      onValueChangeComplete,\n      swatches = [],\n      alpha = false,\n      disabled = false,\n      labels: labelOverrides,\n      className,\n      ...props\n    },\n    ref,\n  ) => {\n    const labels = { ...DEFAULT_LABELS, ...labelOverrides }\n    const gradientId = React.useId().replace(/[^a-zA-Z0-9]/g, '')\n    const [internal, setInternal] = React.useState(defaultValue)\n    const hex = normalizeHex(value ?? internal ?? FALLBACK, alpha) ?? normalizeHex(FALLBACK, alpha)!\n\n    // HSL is the editing state; hex is the value. HSL is only re-derived when the hex it maps to\n    // changes, so integer slider positions never round-trip through hex and drift.\n    const [state, setState] = React.useState(() => ({ hex, hsl: hexToHsl(hex)! }))\n    if (state.hex !== hex) setState({ hex, hsl: hexToHsl(hex, state.hsl)! })\n    const { h, s, l, a } = state.hex === hex ? state.hsl : hexToHsl(hex, state.hsl)!\n\n    const [draft, setDraft] = React.useState({ for: hex, text: hex, invalid: false })\n    if (draft.for !== hex) setDraft({ for: hex, text: hex, invalid: false })\n\n    function emit(next: string) {\n      if (next === hex) return\n      if (value === undefined) setInternal(next)\n      onValueChange?.(next)\n    }\n    function setHsl(next: Hsla) {\n      const nextHex = hslToHex(next, alpha)\n      setState({ hex: nextHex, hsl: next })\n      emit(nextHex)\n    }\n\n    function choose(next: string) {\n      setState({ hex: next, hsl: hexToHsl(next, { h, s, l, a })! })\n      emit(next)\n      onValueChangeComplete?.(next)\n    }\n    function commitDraft() {\n      const next = normalizeHex(draft.text, alpha)\n      if (!next) return setDraft({ ...draft, invalid: true })\n      if (next !== hex) choose(next)\n      // An accepted value resyncs on the next render; a refused one shows the owner's value again.\n      setDraft({ for: hex, text: hex, invalid: false })\n    }\n\n    const rows: {\n      key: string\n      label: string\n      max: number\n      current: number\n      text: string\n      set: (n: number) => Hsla\n      stops: GradientStop[]\n    }[] = [\n      {\n        key: 'hue',\n        label: labels.hue,\n        max: 360,\n        current: h,\n        text: `${Math.round(h)}°`,\n        set: (n) => ({ h: n, s, l, a }),\n        stops: [0, 60, 120, 180, 240, 300, 360].map((deg) => ({\n          offset: deg / 360,\n          color: hslToHex({ h: deg, s: 100, l: 50, a: 1 }),\n        })),\n      },\n      {\n        key: 'saturation',\n        label: labels.saturation,\n        max: 100,\n        current: s,\n        text: `${Math.round(s)}%`,\n        set: (n) => ({ h, s: n, l, a }),\n        stops: [0, 1].map((offset) => ({ offset, color: hslToHex({ h, s: offset * 100, l, a: 1 }) })),\n      },\n      {\n        key: 'lightness',\n        label: labels.lightness,\n        max: 100,\n        current: l,\n        text: `${Math.round(l)}%`,\n        set: (n) => ({ h, s, l: n, a }),\n        stops: [0, 0.5, 1].map((offset) => ({ offset, color: hslToHex({ h, s, l: offset * 100, a: 1 }) })),\n      },\n    ]\n    if (alpha)\n      rows.push({\n        key: 'alpha',\n        label: labels.alpha,\n        max: 100,\n        current: a * 100,\n        text: `${Math.round(a * 100)}%`,\n        set: (n) => ({ h, s, l, a: n / 100 }),\n        stops: [0, 1].map((offset) => ({ offset, color: hslToHex({ h, s, l, a: 1 }), opacity: offset })),\n      })\n\n    return (\n      <View ref={ref} className={cn('gap-4', disabled && 'opacity-50', className)} {...props}>\n        <View className=\"flex-row items-center gap-3\">\n          <View\n            accessible\n            accessibilityRole=\"image\"\n            accessibilityLabel={`${labels.current} ${hex}`}\n            className=\"h-[48px] w-[48px] rounded-md border border-border\"\n            style={{ backgroundColor: hex }}\n          />\n          <View className=\"flex-1\">\n            <Input\n              value={draft.text}\n              onChangeText={(text) => setDraft({ for: hex, text, invalid: false })}\n              onSubmitEditing={commitDraft}\n              onBlur={commitDraft}\n              invalid={draft.invalid}\n              editable={!disabled}\n              autoCapitalize=\"characters\"\n              autoCorrect={false}\n              maxLength={9}\n              returnKeyType=\"done\"\n              accessibilityLabel={labels.hex}\n              accessibilityHint={draft.invalid ? labels.invalidHex : undefined}\n            />\n          </View>\n        </View>\n        {draft.invalid ? (\n          <Text size=\"sm\" className=\"text-destructive\" accessibilityLiveRegion=\"polite\">\n            {labels.invalidHex}\n          </Text>\n        ) : null}\n\n        {swatches.length ? (\n          <View className=\"flex-row flex-wrap gap-1\">\n            {swatches.map((swatch, index) => {\n              const raw = typeof swatch === 'string' ? swatch : swatch.value\n              const swatchHex = normalizeHex(raw, alpha)\n              if (!swatchHex) return null\n              const selected = swatchHex === hex\n              return (\n                <Pressable\n                  key={`${swatchHex}-${index}`}\n                  disabled={disabled}\n                  onPress={() => choose(swatchHex)}\n                  accessibilityRole=\"button\"\n                  accessibilityLabel={typeof swatch === 'string' ? swatchHex : swatch.label}\n                  accessibilityState={{ selected, disabled }}\n                  className={cn(\n                    'h-[48px] w-[48px] items-center justify-center rounded-full border-2',\n                    selected ? 'border-ring' : 'border-transparent',\n                  )}\n                >\n                  <View\n                    className=\"h-[36px] w-[36px] rounded-full border border-border\"\n                    style={{ backgroundColor: swatchHex }}\n                  />\n                </Pressable>\n              )\n            })}\n          </View>\n        ) : null}\n\n        {rows.map((row) => (\n          <View key={row.key}>\n            <View className=\"flex-row justify-between\">\n              <Text size=\"sm\">{row.label}</Text>\n              <Text size=\"sm\" variant=\"muted\">\n                {row.text}\n              </Text>\n            </View>\n            <View className=\"relative\">\n              <Track id={`${gradientId}${row.key}`} stops={row.stops} />\n              <Slider\n                minimumValue={0}\n                maximumValue={row.max}\n                step={1}\n                value={row.current}\n                disabled={disabled}\n                minimumTrackTintColor=\"transparent\"\n                maximumTrackTintColor=\"transparent\"\n                onValueChange={(n) => {\n                  if (!Number.isFinite(n)) return\n                  setHsl(row.set(Math.min(row.max, Math.max(0, n))))\n                }}\n                onSlidingComplete={(n) => {\n                  if (!Number.isFinite(n)) return\n                  onValueChangeComplete?.(hslToHex(row.set(Math.min(row.max, Math.max(0, n))), alpha))\n                }}\n                accessibilityLabel={row.label}\n                {...(row.key === 'hue' ? { accessibilityUnits: 'degrees', accessibilityIncrements: HUE_STEPS } : null)}\n              />\n            </View>\n          </View>\n        ))}\n      </View>\n    )\n  },\n)\nColorPicker.displayName = 'ColorPicker'\n",
      "type": "registry:ui",
      "target": "~/components/ui/color-picker/ColorPicker.tsx"
    },
    {
      "path": "packages/registry-native/components/color-picker/color.ts",
      "content": "/** Channels 0–255, alpha 0–1. */\nexport interface Rgba {\n  r: number\n  g: number\n  b: number\n  a: number\n}\n/** Hue 0–360, saturation and lightness 0–100, alpha 0–1. Kept as floats so hex → hsl → hex is lossless. */\nexport interface Hsla {\n  h: number\n  s: number\n  l: number\n  a: number\n}\n\nconst HEX = /^#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i\n\n/** Parses `#RGB`, `#RGBA`, `#RRGGBB`, `#RRGGBBAA` (leading `#` optional, any case). Anything else is `null`. */\nexport function parseHex(input: string): Rgba | null {\n  const match = HEX.exec(input.trim())\n  if (!match) return null\n  let digits = match[1]\n  if (digits.length <= 4) digits = [...digits].map((d) => d + d).join('')\n  const byte = (i: number) => parseInt(digits.slice(i * 2, i * 2 + 2), 16)\n  return { r: byte(0), g: byte(1), b: byte(2), a: digits.length === 8 ? byte(3) / 255 : 1 }\n}\n\nconst clamp = (n: number, min: number, max: number, fallback = min) =>\n  Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : fallback\n\nconst pair = (n: number) =>\n  Math.round(clamp(n, 0, 255))\n    .toString(16)\n    .padStart(2, '0')\n\n/** Uppercase `#RRGGBB`, or `#RRGGBBAA` when `alpha` is true. */\nexport function formatHex({ r, g, b, a }: Rgba, alpha = false): string {\n  return `#${pair(r)}${pair(g)}${pair(b)}${alpha ? pair(clamp(a, 0, 1, 1) * 255) : ''}`.toUpperCase()\n}\n\n/** Canonical form of any accepted hex, or `null` when it does not parse. */\nexport function normalizeHex(input: string, alpha = false): string | null {\n  const rgba = parseHex(input)\n  return rgba ? formatHex(rgba, alpha) : null\n}\n\nexport function rgbToHsl({ r, g, b, a }: Rgba): Hsla {\n  const [rn, gn, bn] = [clamp(r, 0, 255) / 255, clamp(g, 0, 255) / 255, clamp(b, 0, 255) / 255]\n  const an = clamp(a, 0, 1, 1)\n  const max = Math.max(rn, gn, bn)\n  const min = Math.min(rn, gn, bn)\n  const l = (max + min) / 2\n  const d = max - min\n  if (d === 0) return { h: 0, s: 0, l: l * 100, a: an }\n  const s = d / (1 - Math.abs(2 * l - 1))\n  let h = max === rn ? ((gn - bn) / d) % 6 : max === gn ? (bn - rn) / d + 2 : (rn - gn) / d + 4\n  h *= 60\n  if (h < 0) h += 360\n  return { h, s: s * 100, l: l * 100, a: an }\n}\n\nexport function hslToRgb({ h, s, l, a }: Hsla): Rgba {\n  const hn = Number.isFinite(h) ? ((h % 360) + 360) % 360 : 0\n  const sn = clamp(s, 0, 100) / 100\n  const ln = clamp(l, 0, 100) / 100\n  const an = clamp(a, 0, 1, 1)\n  const k = (n: number) => (n + hn / 30) % 12\n  const m = sn * Math.min(ln, 1 - ln)\n  const f = (n: number) => ln - m * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1))\n  return { r: f(0) * 255, g: f(8) * 255, b: f(4) * 255, a: an }\n}\n\nexport const hslToHex = (hsl: Hsla, alpha = false) => formatHex(hslToRgb(hsl), alpha)\n\n/**\n * HSL for `hex`, keeping `previous` hue (and saturation at black/white) where the colour has none,\n * so dragging lightness to 0 and back, or saturation to 0 and back, returns to the same hue.\n */\nexport function hexToHsl(hex: string, previous?: Hsla): Hsla | null {\n  const rgba = parseHex(hex)\n  if (!rgba) return null\n  const next = rgbToHsl(rgba)\n  if (!previous) return next\n  if (next.l === 0 || next.l === 100) return { ...next, h: previous.h, s: previous.s }\n  if (next.s === 0) return { ...next, h: previous.h }\n  return next\n}\n",
      "type": "registry:ui",
      "target": "~/components/ui/color-picker/color.ts"
    },
    {
      "path": "packages/registry-native/components/color-picker/index.ts",
      "content": "export { ColorPicker, type ColorPickerLabels, type ColorPickerProps, type ColorSwatch } from './ColorPicker'\nexport * from './color'\n",
      "type": "registry:ui",
      "target": "~/components/ui/color-picker/index.ts"
    }
  ],
  "dependencies": [
    "react-native-svg"
  ],
  "devDependencies": [],
  "registryDependencies": [
    "https://native.uipkge.dev/r/native/input.json",
    "https://native.uipkge.dev/r/native/slider.json",
    "https://native.uipkge.dev/r/native/text.json",
    "https://native.uipkge.dev/r/native/utils.json"
  ],
  "description": "HSL colour picker with swatches, gradient-backed hue/saturation/lightness/opacity sliders, and a validated hex field. Hue survives achromatic colours.",
  "categories": [
    "control"
  ]
}