# Color Picker

拖动区域和滑块、输入任何 CSS 颜色或选择预设，来挑选一个颜色。

用取色器挑一个颜色，比如品牌色或主题色。值是 CSS 颜色字符串，每个部件都在编辑它：一个同时编辑两个通道的区域、每个通道一个滑块、一个能读取任何 CSS 颜色的文本输入框，以及预设。

<ComponentExample
  name="color-picker/basic"
  inspect
  caption="拖动区域或滑块，或者选择一个预设。选择 HSL 换一种方式写颜色，或者输入 rebeccapurple 这样的颜色并按 Enter。"
/>

## 安装
```bash
npm install @ui-sdk/svelte @ui-sdk/core
```

## 组合
用下面的组合构建 `ColorPicker`：

```text
ColorPicker
├── ColorPickerArea
│   ├── ColorPickerAreaCanvas
│   └── ColorPickerAreaThumb
├── ColorPickerSlider
│   └── ColorPickerSliderThumb
├── ColorPickerSlider
│   └── ColorPickerSliderThumb
├── ColorPickerSwatch
├── ColorPickerInput
├── ColorPickerChannelInput
├── ColorPickerEyeDropper
└── ColorPickerSwatches
    ├── ColorPickerSwatchItem
    └── ColorPickerSwatchItem
```

每个部件都是可选的，按你的取色器需要选用。

- **ColorPicker** 持有值，是包住各部件的一组。
- **ColorPickerArea** 一次编辑两个通道，比如横向是饱和度、纵向是亮度。按压会把手柄移到那里并开始拖动。它的背景是 `--color-area-background`，所以要给它尺寸和这个背景。
- **ColorPickerAreaCanvas** 逐像素绘制区域，用于渐变画不出来的通道，比如 OKLCH 的色度和明度。
- **ColorPickerAreaThumb** 显示颜色在区域里的位置。它是一个滑块，屏幕阅读器会把它听成这两个通道。
- **ColorPickerSlider** 编辑一个 `channel`，比如 `hue` 或 `alpha`，由 `--color-slider-background` 绘制。
- **ColorPickerSliderThumb** 显示该通道在滑块上的位置。
- **ColorPickerSwatch** 显示颜色，并由屏幕阅读器读出名称，比如“vivid blue”。
- **ColorPickerInput** 在你按 Enter 或离开它时读取任何 CSS 颜色。不是颜色的文本会退回到原来的值。
- **ColorPickerChannelInput** 把一个 `channel` 当作数字编辑，用人们平时读它的单位，比如红色是 0 到 255。
- **ColorPickerEyeDropper** 在浏览器支持的地方，从屏幕上的任何位置取色。
- **ColorPickerSwatches** 和 **ColorPickerSwatchItem** 是预设颜色的列表。

每个部件都把颜色写成 `--color-picker-color`，所以手柄或色块可以用 `background: var(--color-picker-color)` 给自己上色。取色器还渲染一个隐藏输入框，供表单提交。

## 用法
导入部件并把它们组合起来：

```svelte
<script lang="ts">
  import {
    ColorPicker,
    ColorPickerArea,
    ColorPickerAreaThumb,
    ColorPickerInput,
    ColorPickerSlider,
    ColorPickerSliderThumb,
  } from "@ui-sdk/svelte/color-picker"
</script>

<ColorPicker aria-label="Brand color" defaultValue="#0ea5e9">
  <ColorPickerArea>
    <ColorPickerAreaThumb />
  </ColorPickerArea>
  <ColorPickerSlider channel="hue">
    <ColorPickerSliderThumb />
  </ColorPickerSlider>
  <ColorPickerInput aria-label="Hex" />
</ColorPicker>
```

用 `aria-label` 给取色器命名，或者把它放进 [Field](/docs/svelte/components/field) 里，由字段的标签给它命名。没有 `defaultValue` 时，它从黑色开始。

## 色彩空间与通道
`space` 决定部件编辑哪些通道，以及值用什么写法：

| `space` | 区域，横向与纵向 | 滑块 | 值的写法                      |
| ------- | ---------------- | ---- | ----------------------------- |
| `hsv`   | 饱和度、亮度     | 色相 | 十六进制，比如 `#0ea5e9`      |
| `hsl`   | 饱和度、明度     | 色相 | `hsl(198.63 88.66% 48.43%)`   |
| `srgb`  | 红、绿           | 蓝   | `rgb(14 165 233)`             |
| `oklch` | 色度、明度       | 色相 | `oklch(0.6847 0.1479 237.32)` |

设置 `format` 换一种方式写值，比如在任何空间里写 `format="hex"`。每个空间也都有一个 `alpha` 通道。在区域上设置 `x` 和 `y`，或者在滑块上设置 `channel`，来编辑其他通道：

```svelte
<ColorPicker
  aria-label="Background"
  space="hsl"
  defaultValue="hsl(220 60% 50%)"
>
  <ColorPickerArea x="hue" y="lightness">
    <ColorPickerAreaCanvas />
    <ColorPickerAreaThumb />
  </ColorPickerArea>
  <ColorPickerSlider channel="saturation" orientation="vertical">
    <ColorPickerSliderThumb />
  </ColorPickerSlider>
</ColorPicker>
```

颜色变灰或变黑时，部件会记住色相，所以从角落拖回来，还是同一个色相。

## OKLCH
OKLCH 按人眼的感觉均匀地排布颜色，所以明度相同的两种颜色看起来一样亮。它与屏幕能显示的颜色之间的边界是一条曲线，任何渐变都画不出来。给区域加上 `ColorPickerAreaCanvas`，它会画出边界以内的颜色，其余部分留空。

```svelte title="oklch.svelte"
<script lang="ts">
  import {
    ColorPicker,
    ColorPickerArea,
    ColorPickerAreaCanvas,
    ColorPickerAreaThumb,
    ColorPickerInput,
    ColorPickerSlider,
    ColorPickerSliderThumb,
    ColorPickerSwatch,
  } from "@ui-sdk/svelte/color-picker"
</script>

<ColorPicker
  aria-label="Accent"
  space="oklch"
  defaultValue="oklch(0.62 0.19 259)"
>
  <ColorPickerArea>
    <ColorPickerAreaCanvas />
    <ColorPickerAreaThumb />
  </ColorPickerArea>
  <ColorPickerSlider channel="hue">
    <ColorPickerSliderThumb />
  </ColorPickerSlider>
  <ColorPickerSlider channel="alpha">
    <ColorPickerSliderThumb />
  </ColorPickerSlider>
  <div data-slot="row">
    <ColorPickerSwatch />
    <ColorPickerInput aria-label="OKLCH" />
  </div>
</ColorPicker>
```

把滑块手柄向右拖过画出的边界，然后松开。手柄会退回到边界内最接近的颜色。

拖动把颜色带到边界之外时，值已经是边界内最接近的颜色，每个部件都带有 `data-out-of-gamut`。拖动结束后，手柄会移到那个颜色。默认情况下颜色保持在 sRGB 之内。为广色域屏幕设置 `gamut="p3"`，或者用 `gamut={false}` 允许任何颜色：

```svelte
<ColorPicker
  aria-label="Accent"
  space="oklch"
  gamut="p3"
  defaultValue="oklch(0.7 0.25 150)"
>
  <!-- … -->
</ColorPicker>
```

## 预设与通道输入
把 `ColorPickerSwatchItem` 放进 `ColorPickerSwatches`，作为预设颜色。当值是它的颜色时，它就会被选中，不管两者各自怎么写，所以 `#0000ff` 会选中 `rgb(0 0 255)`。列表是一个 Tab 停靠点，方向键在预设之间移动。

`ColorPickerChannelInput` 把一个通道当作数字编辑，按 Enter、离开它或者用它的箭头时设置。`ColorPickerEyeDropper` 在支持的浏览器里从屏幕上取色。在其他地方它是禁用的，带有 `data-unsupported`，所以你可以把它隐藏。

```svelte title="swatches.svelte"
<script lang="ts">
  import { Pipette } from "@lucide/svelte"
  import {
    ColorPicker,
    ColorPickerChannelInput,
    ColorPickerEyeDropper,
    ColorPickerSwatches,
    ColorPickerSwatchItem,
  } from "@ui-sdk/svelte/color-picker"

  const presets = [
    "#ef4444",
    "#f97316",
    "#eab308",
    "#22c55e",
    "#0ea5e9",
    "#6366f1",
    "#a855f7",
    "#ec4899",
  ]
</script>

<ColorPicker
  aria-label="Label color"
  space="srgb"
  format="hex"
  defaultValue="#0ea5e9"
>
  <ColorPickerSwatches aria-label="Presets">
    {#each presets as preset (preset)}
      <ColorPickerSwatchItem value={preset} />
    {/each}
  </ColorPickerSwatches>
  <div data-slot="row">
    <ColorPickerChannelInput channel="red" />
    <ColorPickerChannelInput channel="green" />
    <ColorPickerChannelInput channel="blue" />
    <ColorPickerEyeDropper aria-label="Pick from screen">
      <Pipette aria-hidden="true" />
    </ColorPickerEyeDropper>
  </div>
</ColorPicker>
```

选择一个预设，或者在它们之间使用方向键。在第一个数字里输入 255 来设置红色。

## Popover
把 `Popover` 放进取色器，从色块打开它。浮层里的部件编辑同一个颜色，触发器里的色块显示它。

```svelte title="popover.svelte"
<script lang="ts">
  import {
    ColorPicker,
    ColorPickerArea,
    ColorPickerAreaThumb,
    ColorPickerInput,
    ColorPickerSlider,
    ColorPickerSliderThumb,
    ColorPickerSwatch,
  } from "@ui-sdk/svelte/color-picker"
  import {
    Popover,
    PopoverContent,
    PopoverTrigger,
  } from "@ui-sdk/svelte/popover"
</script>

<ColorPicker aria-label="Background" defaultValue="#22c55e">
  <Popover>
    <PopoverTrigger aria-label="Background">
      <ColorPickerSwatch />
    </PopoverTrigger>
    <PopoverContent>
      <ColorPickerArea>
        <ColorPickerAreaThumb />
      </ColorPickerArea>
      <ColorPickerSlider channel="hue">
        <ColorPickerSliderThumb />
      </ColorPickerSlider>
      <ColorPickerInput aria-label="Hex" />
    </PopoverContent>
  </Popover>
</ColorPicker>
```

按压 Background 色块打开取色器，然后拖动来改变颜色。

## 禁用
设置 `disabled`。禁用的取色器不能获得焦点，也不能修改，并且不会被提交。包住它的禁用 `<fieldset>` 也会禁用它。

```svelte title="disabled.svelte"
<script lang="ts">
  import {
    ColorPicker,
    ColorPickerArea,
    ColorPickerAreaThumb,
    ColorPickerInput,
    ColorPickerSlider,
    ColorPickerSliderThumb,
  } from "@ui-sdk/svelte/color-picker"
</script>

<ColorPicker aria-label="Brand color" defaultValue="#0ea5e9" disabled>
  <ColorPickerArea>
    <ColorPickerAreaThumb />
  </ColorPickerArea>
  <ColorPickerSlider channel="hue">
    <ColorPickerSliderThumb />
  </ColorPickerSlider>
  <ColorPickerInput aria-label="Hex" />
</ColorPicker>
```

手柄不能获得焦点，也不能拖动。

设置 `readOnly`，让部件保持可聚焦、值仍会被提交，同时拖动、按键和输入都不会改变颜色：

```svelte
<ColorPicker aria-label="Theme color" defaultValue="#0ea5e9" readOnly>
  <ColorPickerSwatch />
  <ColorPickerInput aria-label="Hex" />
</ColorPicker>
```

## 受控
用 `bind:value` 绑定 `value`，并传入 `onValueChange` 获取详情。每次变更都带一个[原因](https://ui-sdk.cn/docs/svelte/components/color-picker#reasons)，比如 `drag`、`step-key` 或 `input-commit`，以及 `details.color`，也就是部件持有的颜色，它会在值变灰时保留色相。在回调里调用 `details.cancel()` 保留当前值。

`onValueChange` 在拖动的每一步运行。`onValueCommit` 只运行一次，在拖动、按键或输入颜色结束时，所以用它来保存。来自 `createColorPickerController()` 的控制器可以从任何地方用 `setValue()` 设置颜色，比如下面的 Use Purple 按钮。

```svelte title="controlled.svelte"
<script lang="ts">
  import {
    ColorPicker,
    ColorPickerArea,
    ColorPickerAreaThumb,
    ColorPickerInput,
    ColorPickerSlider,
    ColorPickerSliderThumb,
    createColorPickerController,
  } from "@ui-sdk/svelte/color-picker"
  import { useLog } from "./log"

  const log = useLog()
  const picker = createColorPickerController()
  let value = $state("#0ea5e9")
</script>

<div>
  <div data-slot="controls">
    <button type="button" onclick={() => picker.setValue("#663399")}
      >Use Purple</button
    >
  </div>
  <ColorPicker
    aria-label="Theme color"
    bind:value
    onValueCommit={(next, details) =>
      log(`committed: ${next} (${details.reason})`)}
    controller={picker}
  >
    <ColorPickerArea>
      <ColorPickerAreaThumb />
    </ColorPickerArea>
    <ColorPickerSlider channel="hue">
      <ColorPickerSliderThumb />
    </ColorPickerSlider>
    <ColorPickerInput aria-label="Hex" />
  </ColorPicker>
</div>
```

拖动区域再松开，或者按 Use Purple。控制台记录每次提交的颜色和它的原因。

受控状态、取消和调试见[状态与事件](/docs/svelte/state-and-events)。

## 表单
隐藏输入框在 `name` 下提交值，比如 `#f97316`，禁用的取色器不提交任何东西。表单重置把它恢复到 `defaultValue`。设置 `form` 按 id 加入某个表单。

```svelte title="form.svelte"
<script lang="ts">
  import {
    ColorPicker,
    ColorPickerArea,
    ColorPickerAreaThumb,
    ColorPickerSlider,
    ColorPickerSliderThumb,
    ColorPickerSwatch,
  } from "@ui-sdk/svelte/color-picker"
  import { Field, FieldLabel } from "@ui-sdk/svelte/field"
  import { useLog } from "./log"

  const log = useLog()

  function onsubmit(event: SubmitEvent & { currentTarget: HTMLFormElement }) {
    event.preventDefault()
    log(`Submitted brand=${new FormData(event.currentTarget).get("brand")}`)
  }
</script>

<form data-slot="stack" {onsubmit}>
  <Field>
    <FieldLabel>Brand color</FieldLabel>
    <ColorPicker name="brand" defaultValue="#f97316">
      <ColorPickerArea>
        <ColorPickerAreaThumb />
      </ColorPickerArea>
      <div data-slot="row">
        <ColorPickerSwatch />
        <ColorPickerSlider channel="hue">
          <ColorPickerSliderThumb />
        </ColorPickerSlider>
      </div>
    </ColorPicker>
  </Field>
  <div data-slot="row">
    <button type="submit" data-variant="solid">Save</button>
    <button type="reset">Reset</button>
  </div>
</form>
```

改变颜色并按 Save，记录提交的值。Reset 恢复橙色。

取色器总是有值，所以 `required` 只会加上 `data-required`。

## 自定义渲染
每个部件都接受 `render`，一个用部件的 props 和状态渲染你自己元素的 snippet，它的 `children` snippet 会收到状态。滑块手柄的状态有它的 `channelValue`，预设的状态有它是否 `selected`，每个部件都有 `color` 以及它是否 `outOfGamut`：

```svelte
<ColorPickerSlider channel="alpha">
  <ColorPickerSliderThumb>
    {#snippet children(state)}
      <span class:dashed={state.outOfGamut}
        >{Math.round(state.channelValue)}%</span
      >
    {/snippet}
  </ColorPickerSliderThumb>
</ColorPickerSlider>
<ColorPickerSwatch>
  {#snippet children(state)}
    {#if state.color.alpha < 1}<span>Transparent</span>{/if}
  {/snippet}
</ColorPickerSwatch>
```

渲染约定见[部件与组合](/docs/svelte/parts-and-composition)，每个部件的字段见[状态](https://ui-sdk.cn/docs/svelte/components/color-picker#state)。

## 样式
部件没有样式，除了它们需要的位置：手柄会以内联样式获得 `position: absolute` 和它的百分比位置。给区域和滑块设置尺寸和它们的背景变量，并用 `translate: -50% -50%` 把每个手柄居中到它的点上。

每个部件都有一个 `data-slot`，比如 `color-picker-area`，它的状态属性列在[数据属性](https://ui-sdk.cn/docs/svelte/components/color-picker#data-attributes)下。

### ColorPicker
- `--color-picker-color`: The color as CSS, inside the gamut. On a swatch item, its own color. Use it to fill a thumb or a swatch.

### ColorPickerArea
- `--color-area-background`: Gradients that draw the area, when its channels are saturation and brightness or lightness. Unset for others, which a canvas paints.

### ColorPickerAreaThumb
- `--area-x`: Where the thumb is across the area, from 0 to 1.
- `--area-y`: Where the thumb is up the area, from 0 at the bottom to 1 at the top.

### ColorPickerSlider
- `--color-slider-background`: A gradient through the slider's channel, with the other channels as they are.

### ColorPickerSliderThumb
- `--color-slider-thumb-position`: Where the thumb is along the slider, as a percentage.

### ColorPickerSwatch
- `--color-picker-color`: The color as CSS, inside the gamut. On a swatch item, its own color. Use it to fill a thumb or a swatch.

### ColorPickerSwatchItem
- `--color-picker-color`: The color as CSS, inside the gamut. On a swatch item, its own color. Use it to fill a thumb or a swatch.

## 数据属性
### ColorPicker
`[data-slot="color-picker"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerArea
`[data-slot="color-picker-area"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerAreaThumb
`[data-slot="color-picker-area-thumb"]`

- `data-area-thumb`: Present on the area's thumb, which a press on the area moves and focuses.
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerSlider
`[data-slot="color-picker-slider"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-orientation` (`horizontal`, `vertical`): The slider's axis.
- `data-channel` (`hue`, `saturation`, `brightness`, `lightness`, `chroma`, `red`, `green`, `blue`, `alpha`): The channel the part edits.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerSliderThumb
`[data-slot="color-picker-slider-thumb"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-orientation` (`horizontal`, `vertical`): The slider's axis.
- `data-channel` (`hue`, `saturation`, `brightness`, `lightness`, `chroma`, `red`, `green`, `blue`, `alpha`): The channel the part edits.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerInput
`[data-slot="color-picker-input"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerChannelInput
`[data-slot="color-picker-channel-input"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-channel` (`hue`, `saturation`, `brightness`, `lightness`, `chroma`, `red`, `green`, `blue`, `alpha`): The channel the part edits.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerSwatch
`[data-slot="color-picker-swatch"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerEyeDropper
`[data-slot="color-picker-eye-dropper"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.
- `data-unsupported`: Present when the browser can't pick a color from the screen.

### ColorPickerSwatches
`[data-slot="color-picker-swatches"]`

- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.
- `data-required`: Present when the picker is required.
- `data-out-of-gamut`: Present while a drag or a key holds a color outside the gamut. The value is the closest color inside, and the parts move there once the change commits.

### ColorPickerSwatchItem
`[data-slot="color-picker-swatch-item"]`

- `data-selected`: Present on the swatch item whose color is the value.
- `data-disabled`: Present when the picker is disabled, by its own prop or a `<fieldset>`.
- `data-readonly`: Present when the picker shows its color but can't change it.

## API 参考
属性和回调与下面相同，都作为 Svelte props。`value` 可以用 `bind:value` 绑定。

### ColorPicker
- `value?` (`string`): The value, as a CSS color, like `"#0ea5e9"` or `"oklch(0.62 0.19 259)"`. Pass it to control the picker.
- `defaultValue?` (`string`): The value at first, when uncontrolled. A form reset returns to it.
- `onValueChange?` (`((value: string, details: ColorPickerValueChangeDetails) => void)`): Runs while the value changes: on every step of a drag or a key. `details.color` is the color, and `details.cancel()` stops it.
- `onValueCommit?` (`((value: string, details: ColorPickerValueCommitDetails) => void)`): Runs once an interaction that changed the value ends: the pointer lifts, a key is pressed, a swatch is picked. For saving.
- `space?` (`ColorPickerSpace`): The space the parts edit in, which decides their channels: `"hsv"` has hue, saturation and brightness, `"hsl"` hue, saturation and lightness, `"srgb"` red, green and blue, `"oklch"` lightness, chroma and hue.
- `format?` (`ColorFormat`): How the value is written: `"hex"`, or a space in its CSS syntax, like `"oklch"`.
- `gamut?` (`false | ColorGamut`): The colors the value can hold. A drag can go past the edge, and the value is the closest color inside it; once the drag ends, the picker moves there too. `"p3"` allows wide-gamut screens' colors, and `false` any color.
- `disabled?` (`boolean`): Disables every part: they can't be focused or changed, and the value isn't submitted.
- `readOnly?` (`boolean`): Keeps the parts focusable and the value submitted, but stops changes.
- `required?` (`boolean`): Marks it required, with `data-required`. A color picker always has a value, so it never blocks a form.
- `name?` (`string`): The name the value submits under. Without one, the picker submits nothing.
- `form?` (`string`): The id of a form to belong to, when it isn't inside that form.
- `controller?` (`{ getValue: () => string; setValue: (value: string, options?: ColorPickerChangeOptions) => boolean; focus: () => void; }`): Sets the value from anywhere. Create one with `createColorPickerController()`.
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.

### ColorPickerArea
- `x?` (`ColorChannel`): The channel across, left to right.
- `y?` (`ColorChannel`): The channel up, bottom to top.

### ColorPickerSlider
- `channel?` (`ColorChannel`): The channel the slider edits, like `"hue"` or `"alpha"`.
- `orientation?` (`"horizontal" | "vertical"`): The slider's axis. Vertical sliders grow upward.

### ColorPickerChannelInput
- `channel?` (`ColorChannel`): The channel the input edits, like `"hue"` or `"alpha"`.

### ColorPickerSwatchItem
- `value` (`string`): The color it picks, as any CSS color.

### 状态
`render` 和 `children` snippet 会收到什么。每个部件都会收到取色器的状态。

- `value` (`string`): The value, as a CSS color.
- `color` (`Color`): The color the parts show, in the picker's space. During a drag it can be outside the gamut.
- `space` (`ColorPickerSpace`): The space the parts edit in.
- `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
- `readOnly` (`boolean`): Whether it's read-only: the parts stay focusable, but nothing changes the value.
- `required` (`boolean`): Whether it's marked required.
- `outOfGamut` (`boolean`): Whether the color the parts show is outside the gamut, so the value is the closest one inside.

滑块和它们的手柄会增加自己的通道：

- `value` (`string`): The value, as a CSS color.
- `color` (`Color`): The color the parts show, in the picker's space. During a drag it can be outside the gamut.
- `space` (`ColorPickerSpace`): The space the parts edit in.
- `disabled` (`boolean`): Whether it's disabled, by itself or by a `<fieldset>`.
- `readOnly` (`boolean`): Whether it's read-only: the parts stay focusable, but nothing changes the value.
- `required` (`boolean`): Whether it's marked required.
- `outOfGamut` (`boolean`): Whether the color the parts show is outside the gamut, so the value is the closest one inside.
- `channel` (`ColorChannel`): The channel the slider edits.
- `channelValue` (`number`): The channel's value, in the units people read it in: degrees, percentages, 0 to 255 for red.
- `orientation` (`"horizontal" | "vertical"`): The slider's axis.

预设项有自己的状态：

- `selected` (`boolean`): Whether the picker's color looks the same as this swatch's, whatever each is written in.
- `disabled` (`boolean`): Whether pressing it changes nothing, since the picker is disabled or read-only.

### 原因
`details.reason` 说明值为什么变化。

- `track-press` (`PointerEvent`): A press on a slider's track or an area's surface moved the nearest thumb there.
- `drag` (`PointerEvent`): A slider thumb or a resize handle was dragged, or an item was dragged to a new place in a list.
- `step-key` (`KeyboardEvent`): Arrow, Page Up, Page Down, Home or End stepped a slider or a number input.
- `input-commit` (`KeyboardEvent | FocusEvent`): Enter, or leaving a number input, committed what was typed: a number input parses, clamps and snaps it to the step, and an autocomplete closes its suggestions on it.
- `item-press` (`MouseEvent | KeyboardEvent`): A member of a collection was chosen: a menu item, a tab, a radio or a checkbox item.
- `eye-dropper-pick`
- `reset` (`none`): A form reset.
- `restore` (`none`): The browser restored state, like after going back, or a saved value was read back from storage.
- `imperative` (`none`): A controller or `api` call.

### 控制器
用 `createColorPickerController()` 创建一个，并作为 `controller` 传入。

`setValue()` 接受任何 CSS 颜色和可选的 `{ reason, event }`，像拖动一样运行变更回调，并在颜色无法读取或变更被取消时返回 `false`。

- `getValue()` (`() => string`): The value, as a CSS color.
- `setValue(value, options?)` (`(value: string, options?: ColorPickerChangeOptions | undefined) => boolean`): Sets the value from any CSS color, as a user would. Returns false for text that isn't one.
- `focus()` (`() => void`): Moves focus to the area's thumb, or the first slider's.

### 事件
回调先收到新值，然后收到详情。绑定的 `value` 只在 `onValueChange` 没有被取消时更新。

| 属性            | 所属          | 收到                                                           |
| --------------- | ------------- | -------------------------------------------------------------- |
| `bind:value`    | `ColorPicker` | 值                                                             |
| `onValueChange` | `ColorPicker` | `(value, details)`：新值、[原因](https://ui-sdk.cn/docs/svelte/components/color-picker#reasons)和 `details.color`   |
| `onValueCommit` | `ColorPicker` | `(value, details)`：交互结束时的值、它的原因和 `details.color` |

## 无障碍
没有 WAI-ARIA 模式直接覆盖取色器，所以它由能覆盖它的模式搭成。取色器是一个分组。区域的手柄和每个滑块的手柄遵循 [WAI-ARIA 滑块模式](https://www.w3.org/WAI/ARIA/apg/patterns/slider/)，区域手柄的名称包含它的两个通道，比如“Saturation 76%, Brightness 96%”。预设遵循[列表框模式](https://www.w3.org/WAI/ARIA/apg/patterns/listbox/)，每个预设由它的颜色命名，比如“vivid red”，`ColorPickerSwatch` 也是如此。

手柄、输入框和预设默认用英语命名。传入 `aria-label` 来用其他语言命名它们。

### 键盘
| 按键                 | 作用                                                                        |
| -------------------- | --------------------------------------------------------------------------- |
| Arrow keys           | 把手柄移动一个步进。在区域上，Left 和 Right 横向移动，Up 和 Down 纵向移动。 |
| Shift with an arrow  | 移动十个步进                                                                |
| Page Up or Page Down | 移动范围的十分之一，沿区域的纵向或沿滑块                                    |
| Home or End          | 移到起点或终点，沿区域的横向或沿滑块                                        |
| Enter                | 读取输入框里的文本，或者选择聚焦的预设                                      |
| Escape               | 把输入框的文本恢复成当前值                                                  |

每次按键都会提交，就像一次拖动结束。

### 经过测试的行为
下面每一行都是一个自动化测试。

**Value**

- The parts show the default value.
- A press on the area moves the color there, and commits when it lifts.
- A drag along a slider follows the pointer and commits once.
- The text input reads any CSS color on Enter, and text that isn't one goes back.
- A channel input sets its channel in its own units.
- A controller sets the value, and a canceled change keeps it.

**Keyboard**

- Arrows step a slider, Shift ten steps, and each press commits.
- Arrows move the area's thumb on both axes.
- Swatches are one tab stop, arrows move between them, and a press picks one.

**Forms**

- The value submits under its name, and a reset returns to the default.
- A disabled picker can't be focused or changed, and submits nothing.

**Gamut**

- In OKLCH, a color past the sRGB edge writes the closest one inside, and the thumb moves there once it lifts.
- The canvas paints the area, with the colors outside the gamut left clear.

Runs in React、Vue 和 web components.
