# Colour Picker Components

Two components for picking OKLCH colours. Use `ixfx-colour-picker` when you need a full inline picker; use `ixfx-colour-picker-popup` when you need a compact swatch that opens a picker in a popover.

Both work in OKLCH colour space, which gives perceptually uniform gradients across all three channels.

---

## `<ixfx-colour-picker>`

A full inline picker with gradient-rendered sliders for Hue, Lightness, and Chroma (and optionally Opacity). Includes a colour preview swatch. Layout adapts automatically — portrait (stacked), landscape (preview beside sliders), and a compact narrow-height mode.

### Basic usage

```html
<ixfx-colour-picker></ixfx-colour-picker>

<script>
  const picker = document.querySelector('ixfx-colour-picker');
  picker.addEventListener('input', e => {
    console.log(e.detail); // { l, c, h, a }
    console.log(picker.value); // "oklch(0.7 0.15 180)"
  });
</script>
```

### Attributes

| Attribute        | Type    | Default | Description |
|------------------|---------|---------|-------------|
| `l`              | number  | `0.7`   | Lightness (0–1) |
| `c`              | number  | `0.15`  | Chroma (0–0.4) |
| `h`              | number  | `180`   | Hue (0–360) |
| `a`              | number  | `1`     | Opacity (0–1). Only relevant when `show-a` is set. |
| `value`          | string  | —       | Set initial colour from any CSS colour string (e.g. `"#ff0"`, `"hsl(120 50% 50%)"`) |
| `hide-preview`   | boolean | `false` | Hide the colour preview swatch |
| `hide-h`         | boolean | `false` | Hide the Hue slider |
| `hide-l`         | boolean | `false` | Hide the Lightness slider |
| `hide-c`         | boolean | `false` | Hide the Chroma slider |
| `show-a`         | boolean | `false` | Show the Opacity slider |
| `horizontal`     | boolean | `false` | Force horizontal layout (preview left, sliders right) |
| `sliders-vertical` | boolean | `false` | Render sliders as side-by-side vertical bars |
| `hide-tooltips`  | boolean | `false` | Suppress hover tooltips on slider labels |
| `range-l`        | string  | `"0-1"` | Restrict Lightness slider range, e.g. `"0.2-0.9"` |
| `range-c`        | string  | `"0-0.4"` | Restrict Chroma slider range |
| `range-h`        | string  | `"0-360"` | Restrict Hue slider range |
| `range-a`        | string  | `"0-1"` | Restrict Opacity slider range |

### Properties

All attributes above are also settable as JS properties (camelCase where applicable: `hidePreview`, `hideH`, `hideL`, `hideC`, `showA`, `horizontal`, `slidersVertical`, `hideTooltips`).

Range setters accept a `[min, max]` tuple and can also be set via the attributes above:

```js
picker.rangeH = [0, 180];   // restrict hue to cool colours
picker.rangeL = [0.2, 0.8]; // exclude near-black and near-white
```

### Getters

| Getter  | Returns | Description |
|---------|---------|-------------|
| `value` | `string` | Current colour as a CSS `oklch()` string, e.g. `"oklch(0.7 0.15 180)"` |
| `rangeL` | `[number, number]` | Active lightness range |
| `rangeC` | `[number, number]` | Active chroma range |
| `rangeH` | `[number, number]` | Active hue range |
| `rangeA` | `[number, number]` | Active opacity range |

### Methods

#### `setColour(colour)`

Set the colour programmatically from any ixfx-compatible colour format. Also updates the double-click reset target for all sliders.

```js
picker.setColour('hsl(200, 80%, 50%)');
picker.setColour('#e74c3c');
picker.setColour({ l: 0.6, c: 0.2, h: 260 }); // OKLCH object
```

Accepts any format that ixfx's `Colour.convert()` understands.

### Events

Both events carry `CustomEvent<{ l: number, c: number, h: number, a: number }>` and bubble + compose.

| Event    | When |
|----------|------|
| `input`  | Every slider move (continuous, high frequency) |
| `change` | When slider interaction ends (committed value) |

```js
picker.addEventListener('input', e => {
  document.body.style.background = picker.value;
});

picker.addEventListener('change', e => {
  saveColour(e.detail); // { l, c, h, a }
});
```

### CSS custom properties

| Property | Default | Description |
|----------|---------|-------------|
| `--picker-alt-width` | `240px` | Component width |
| `--picker-alt-height` | `240px` | Component height |
| `--picker-alt-min-slider-height` | `2.5em` | Minimum height per slider |

Standard theme variables (`--surface`, `--surface-ll`, `--radius-m`, `--space-s`, `--accent`) are used where available.

### Layout behaviour

The component uses CSS container queries on its own size, so you can size it freely:

```css
ixfx-colour-picker {
  width: 100%;
  height: 300px;
}
```

- **Portrait / square**: preview swatch on top, sliders below (stacked column)
- **Landscape** (aspect ratio > 1): preview swatch on left, sliders on right
- **Short** (height < 150px): switches to landscape regardless of width
- **`sliders-vertical`**: sliders become narrow side-by-side vertical bars (good for wide, short layouts)

---

## `<ixfx-colour-picker-popup>`

A compact inline swatch button. Clicking it opens a full `ixfx-colour-picker` in a native popover anchored to the swatch. The picker is lazy — it is not added to the DOM until the swatch is clicked for the first time.

### Basic usage

```html
<label>
  Background colour
  <ixfx-colour-picker-popup></ixfx-colour-picker-popup>
</label>

<script>
  const popup = document.querySelector('ixfx-colour-picker-popup');
  popup.addEventListener('change', e => {
    document.body.style.background = popup.value;
  });
</script>
```

### Modes

#### Fluid mode (default)

Every slider move updates `l/c/h/a` on the popup and fires `input`. The swatch updates live. `change` fires when the slider interaction ends.

#### Confirm mode

Slider changes are held in a pending state. Nothing is committed until the user clicks the confirm (✓) button, which fires `change` and closes the popover. Dismissing the popover without confirming (light-dismiss / clicking outside) reverts the picker to the last committed values.

```html
<ixfx-colour-picker-popup mode="confirm"></ixfx-colour-picker-popup>
```

### Attributes

| Attribute      | Type    | Default    | Description |
|----------------|---------|------------|-------------|
| `mode`         | string  | `"fluid"`  | `"fluid"` or `"confirm"` |
| `l`            | number  | `0.7`      | Committed lightness (0–1) |
| `c`            | number  | `0.15`     | Committed chroma (0–0.4) |
| `h`            | number  | `180`      | Committed hue (0–360) |
| `a`            | number  | `1`        | Committed opacity (0–1) |
| `size`         | string  | `"medium"` | Popover picker size: `"small"` \| `"medium"` \| `"large"` \| `"huge"` |
| `show-preview` | boolean | `false`    | Show the colour preview inside the picker (fluid mode hides it by default since the swatch already shows the live colour) |
| `hide-h`       | boolean | `false`    | Hide the Hue slider inside the picker |
| `hide-l`       | boolean | `false`    | Hide the Lightness slider inside the picker |
| `hide-c`       | boolean | `false`    | Hide the Chroma slider inside the picker |
| `show-a`       | boolean | `false`    | Show the Opacity slider inside the picker |
| `horizontal`   | boolean | `false`    | Force horizontal layout inside the picker |

Picker sizes (width × height):

| `size`    | Dimensions |
|-----------|------------|
| `small`   | 130 × 100 px |
| `medium`  | 100 × 155 px (default) |
| `large`   | 200 × 180 px |
| `huge`    | 290 × 240 px |

### Getters/setters

| Property | Type                    | Description |
|----------|-------------------------|-------------|
| `value`  | `string` (get) / `Colour.Colourish` (set) | Committed colour. Reading returns a CSS `oklch()` string; assigning accepts any ixfx-compatible colour format (equivalent to calling `setColour()`) — including via a Lit property binding, e.g. `.value=${someColour}`. |

### Methods

#### `setColour(colour)`

Set the committed colour from any ixfx-compatible format. The swatch updates immediately. The inner picker reflects the new colour the next time the popover opens.

#### `close()`

Close the popover. In confirm mode, uncommitted changes are reverted.

### Slots

| Slot | Description |
|------|-------------|
| `action` | Optional button(s) at the bottom-left of the popup, inline with the confirm button. Wire up its click handler yourself and call `close()` to dismiss. |

```html
<ixfx-colour-picker-popup mode="confirm" id="bg">
  <button slot="action" id="none">No colour</button>
</ixfx-colour-picker-popup>
<script>
  none.addEventListener('click', () => { /* clear your value */ bg.close(); });
</script>
```

### Limitations

The popover is positioned relative to the swatch using CSS anchor positioning (`anchor-name`/`position-anchor`). Chrome does not resolve anchor positioning for elements inside an SVG `<foreignObject>` — if you embed this component there, the popover will render unanchored at the top-left of the viewport instead of next to the swatch. Workaround: keep the swatch as a plain SVG element and render this component (or just the picker) in a fixed-positioned HTML overlay positioned via `getBoundingClientRect()` on your own swatch element.

```js
popup.setColour('#3498db');
popup.setColour({ l: 0.5, c: 0.25, h: 30 });
```

### Events

| Event    | Mode    | When |
|----------|---------|------|
| `input`  | fluid   | Every slider move |
| `change` | fluid   | Slider interaction ends |
| `change` | confirm | Confirm button clicked |

Both carry `CustomEvent<{ l: number, c: number, h: number, a: number }>`, bubble, and compose.

### CSS custom properties

| Property | Default | Description |
|----------|---------|-------------|
| `--cp-popup-swatch-size` | `1.5em` | Width and height of the swatch button |

The popover panel uses standard theme variables for background, border, radius, and shadow (`--surface`, `--border`, `--radius-l`, `--shadow-l`, `--space-s`).

---

## Shared event detail type

```ts
type ColourPickerEventDetail = {
  l: number; // Lightness 0–1
  c: number; // Chroma 0–0.4
  h: number; // Hue 0–360
  a?: number; // Opacity 0–1 (defaults to 1)
};
```
