# Color Picker

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

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

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

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

导入组件以注册它的元素。这个导入在服务端是安全的。

```ts
import "@ui-sdk/web/color-picker"
```

## 组合
用下面的组合构建一个 `ui-color-picker`：

```text
ui-color-picker
├── ui-color-picker-area
│   ├── ui-color-picker-area-canvas
│   └── ui-color-picker-area-thumb
├── ui-color-picker-slider
│   └── ui-color-picker-slider-thumb
├── ui-color-picker-slider
│   └── ui-color-picker-slider-thumb
├── ui-color-picker-swatch
├── ui-color-picker-input
├── ui-color-picker-channel-input
├── ui-color-picker-eye-dropper
└── ui-color-picker-swatches
    ├── ui-color-picker-swatch-item
    └── ui-color-picker-swatch-item
```

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

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

每个部件都把颜色写成 `--color-picker-color`，所以手柄或色块可以用 `background: var(--color-picker-color)` 给自己上色。取色器与表单关联，所以它像原生输入框一样提交自己的值。输入框、画布、吸管和预设项会使用你放进它们里面的 `<input>`、`<canvas>` 或 `<button>`，没有就自己造一个。

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

```html
<script type="module">
  import "@ui-sdk/web/color-picker"
</script>

<ui-color-picker aria-label="Brand color" default-value="#0ea5e9">
  <ui-color-picker-area>
    <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
  </ui-color-picker-area>
  <ui-color-picker-slider channel="hue">
    <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
  </ui-color-picker-slider>
  <ui-color-picker-input>
    <input aria-label="Hex" />
  </ui-color-picker-input>
</ui-color-picker>
```

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

## 色彩空间与通道
`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`，来编辑其他通道：

```html
<ui-color-picker
  aria-label="Background"
  space="hsl"
  default-value="hsl(220 60% 50%)"
>
  <ui-color-picker-area x="hue" y="lightness">
    <ui-color-picker-area-canvas></ui-color-picker-area-canvas>
    <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
  </ui-color-picker-area>
  <ui-color-picker-slider channel="saturation" orientation="vertical">
    <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
  </ui-color-picker-slider>
</ui-color-picker>
```

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

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

```ts title="oklch.ts"
import "@ui-sdk/web/color-picker"

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-color-picker aria-label="Accent" space="oklch" default-value="oklch(0.62 0.19 259)">
      <ui-color-picker-area>
        <ui-color-picker-area-canvas></ui-color-picker-area-canvas>
        <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
      </ui-color-picker-area>
      <ui-color-picker-slider channel="hue">
        <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
      </ui-color-picker-slider>
      <ui-color-picker-slider channel="alpha">
        <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
      </ui-color-picker-slider>
      <div data-slot="row">
        <ui-color-picker-swatch></ui-color-picker-swatch>
        <ui-color-picker-input>
          <input aria-label="OKLCH" />
        </ui-color-picker-input>
      </div>
    </ui-color-picker>
  `
}

export { mount }
```

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

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

```html
<ui-color-picker
  aria-label="Accent"
  space="oklch"
  gamut="p3"
  default-value="oklch(0.7 0.25 150)"
>
  <!-- … -->
</ui-color-picker>
```

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

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

```ts title="swatches.ts"
import "@ui-sdk/web/color-picker"
import { createElement, Pipette } from "lucide"

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

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-color-picker aria-label="Label color" space="srgb" format="hex" default-value="#0ea5e9">
      <ui-color-picker-swatches aria-label="Presets">
        ${presets.map((preset) => `<ui-color-picker-swatch-item value="${preset}"></ui-color-picker-swatch-item>`).join("")}
      </ui-color-picker-swatches>
      <div data-slot="row">
        <ui-color-picker-channel-input channel="red"></ui-color-picker-channel-input>
        <ui-color-picker-channel-input channel="green"></ui-color-picker-channel-input>
        <ui-color-picker-channel-input channel="blue"></ui-color-picker-channel-input>
        <ui-color-picker-eye-dropper>
          <button aria-label="Pick from screen">${createElement(Pipette, { "aria-hidden": "true" }).outerHTML}</button>
        </ui-color-picker-eye-dropper>
      </div>
    </ui-color-picker>
  `
}

export { mount }
```

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

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

```ts title="popover.ts"
import "@ui-sdk/web/color-picker"
import "@ui-sdk/web/popover"

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-color-picker aria-label="Background" default-value="#22c55e">
      <ui-popover>
        <ui-popover-trigger>
          <button aria-label="Background">
            <ui-color-picker-swatch></ui-color-picker-swatch>
          </button>
        </ui-popover-trigger>
        <ui-popover-content>
          <ui-color-picker-area>
            <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
          </ui-color-picker-area>
          <ui-color-picker-slider channel="hue">
            <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
          </ui-color-picker-slider>
          <ui-color-picker-input>
            <input aria-label="Hex" />
          </ui-color-picker-input>
        </ui-popover-content>
      </ui-popover>
    </ui-color-picker>
  `
}

export { mount }
```

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

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

```ts title="disabled.ts"
import "@ui-sdk/web/color-picker"

function mount(root: HTMLElement) {
  root.innerHTML = `
    <ui-color-picker aria-label="Brand color" default-value="#0ea5e9" disabled>
      <ui-color-picker-area>
        <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
      </ui-color-picker-area>
      <ui-color-picker-slider channel="hue">
        <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
      </ui-color-picker-slider>
      <ui-color-picker-input>
        <input aria-label="Hex" />
      </ui-color-picker-input>
    </ui-color-picker>
  `
}

export { mount }
```

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

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

```html
<ui-color-picker aria-label="Theme color" default-value="#0ea5e9" read-only>
  <ui-color-picker-swatch></ui-color-picker-swatch>
  <ui-color-picker-input>
    <input aria-label="Hex" />
  </ui-color-picker-input>
</ui-color-picker>
```

## 受控
监听 `valuechange` 获取每次变更。每次变更都带一个[原因](https://ui-sdk.cn/docs/web-components/components/color-picker#reasons)，比如 `drag`、`step-key` 或 `input-commit`，以及 `event.detail.color`，也就是部件持有的颜色，它会在值变灰时保留色相。调用 `event.preventDefault()` 保留当前值。

`valuechange` 在拖动的每一步触发。`valuecommit` 只触发一次，在拖动、按键或输入颜色结束时，所以监听它来保存。元素的 `api` property 可以从任何地方用 `setValue()` 设置颜色，比如下面的 Use Purple 按钮。

```ts title="controlled.ts"
import "@ui-sdk/web/color-picker"
import { log } from "./log"

function mount(root: HTMLElement) {
  root.innerHTML = `
    <div>
      <div data-slot="controls">
        <button type="button">Use Purple</button>
      </div>
      <ui-color-picker aria-label="Theme color" default-value="#0ea5e9">
        <ui-color-picker-area>
          <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
        </ui-color-picker-area>
        <ui-color-picker-slider channel="hue">
          <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
        </ui-color-picker-slider>
        <ui-color-picker-input>
          <input aria-label="Hex" />
        </ui-color-picker-input>
      </ui-color-picker>
    </div>
  `
  const picker = root.querySelector("ui-color-picker")!
  picker.addEventListener("valuecommit", (event) => {
    const { value, reason } = event.detail
    log(root, `committed: ${value} (${reason})`)
  })
  root
    .querySelector('[data-slot="controls"] button')!
    .addEventListener("click", () => picker.api.setValue("#663399"))
}

export { mount }
```

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

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

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

```ts title="form.ts"
import "@ui-sdk/web/color-picker"
import "@ui-sdk/web/field"
import { log } from "./log"

function mount(root: HTMLElement) {
  root.innerHTML = `
    <form data-slot="stack">
      <ui-field>
        <ui-field-label>Brand color</ui-field-label>
        <ui-color-picker name="brand" default-value="#f97316">
          <ui-color-picker-area>
            <ui-color-picker-area-thumb></ui-color-picker-area-thumb>
          </ui-color-picker-area>
          <div data-slot="row">
            <ui-color-picker-swatch></ui-color-picker-swatch>
            <ui-color-picker-slider channel="hue">
              <ui-color-picker-slider-thumb></ui-color-picker-slider-thumb>
            </ui-color-picker-slider>
          </div>
        </ui-color-picker>
      </ui-field>
      <div data-slot="row">
        <button type="submit" data-variant="solid">Save</button>
        <button type="reset">Reset</button>
      </div>
    </form>
  `
  const form = root.querySelector("form")!
  form.addEventListener("submit", (event) => {
    event.preventDefault()
    log(root, `Submitted brand=${new FormData(form).get("brand")}`)
  })
}

export { mount }
```

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

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

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

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

### ui-color-picker
- `--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.

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

### ui-color-picker-area-thumb
- `--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.

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

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

### ui-color-picker-swatch
- `--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.

### ui-color-picker-swatch-item
- `--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.

## 数据属性
### ui-color-picker
`[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.

### ui-color-picker-area
`[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.

### ui-color-picker-area-thumb
`[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.

### ui-color-picker-slider
`[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.

### ui-color-picker-slider-thumb
`[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.

### ui-color-picker-input
`[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.

### ui-color-picker-channel-input
`[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.

### ui-color-picker-swatch
`[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.

### ui-color-picker-eye-dropper
`[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.

### ui-color-picker-swatches
`[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.

### ui-color-picker-swatch-item
`[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 参考
属性是 kebab 命名的 attribute，比如 `default-value` 和 `read-only`。`controller` 是 property。`value` 是实时的 property 和 attribute。回调是 DOM 事件。

### ui-color-picker
- `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.
- `valuechange event`: Runs while the value changes: on every step of a drag or a key. `details.color` is the color, and `details.cancel()` stops it.
- `valuecommit event`: 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.

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

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

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

### ui-color-picker-swatch-item
- `value` (`string`): The color it picks, as any CSS color.

### 状态
相同的状态会以数据属性的形式显示在每个元素上。

- `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.

### 原因
`event.detail.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.

### 控制器
元素的 `api` property 有相同的方法。`value` property 读取并设置颜色。

`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.

### 事件
事件在自己的元素上触发，不冒泡，新值在 `event.detail.value` 里，`reason`、`event` 和 `color` 在它旁边。对 `valuechange` 调用 `preventDefault()` 会取消变更。

| 成员                        | 所属              | 说明                                                    |
| --------------------------- | ----------------- | ------------------------------------------------------- |
| `value` property, attribute | `ui-color-picker` | 值。attribute 设置它开始时的值，并且是实时的            |
| `api` property              | `ui-color-picker` | [控制器](https://ui-sdk.cn/docs/web-components/components/color-picker#controller)                                   |
| `valuechange` event         | `ui-color-picker` | `detail: { value, reason, event, color }`               |
| `valuecommit` event         | `ui-color-picker` | `detail: { value, reason, event, 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”，`ui-color-picker-swatch` 也是如此。

手柄、输入框和预设默认用英语命名。传入 `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.
