# Module /color

Color space conversions (hex, RGB, HSL, OKLCH), WCAG contrast ratios, and tonal palette operations.

```tangu
(use /color)
```

## Structs

### HSL

`Struct` in `/color`

Cylindrical HSL color space:
- h: hue angle in degrees (0.0..360.0)
- s: saturation (0.0..1.0)
- l: lightness (0.0..1.0)
- a: alpha (0.0..1.0, default 1.0)

| Field | Type | Default | Notes |
| :--- | :--- | :--- | :--- |
| `h` | `Float` | `—` | — |
| `s` | `Float` | `—` | — |
| `l` | `Float` | `—` | — |
| `a` | `Float` | `—` | — |

### Oklch

`Struct` in `/color`

Perceptual Oklch color space:
- l: perceived lightness (0.0..1.0)
- c: chroma (0.0..~0.4)
- h: hue angle in degrees (0.0..360.0)
- a: alpha (0.0..1.0, default 1.0)

| Field | Type | Default | Notes |
| :--- | :--- | :--- | :--- |
| `l` | `Float` | `—` | — |
| `c` | `Float` | `—` | — |
| `h` | `Float` | `—` | — |
| `a` | `Float` | `—` | — |

### RGB

`Struct` in `/color`

3-channel linear or sRGB color (0.0..1.0).
Derives `Eq` and `Vec` for full Euclidean vector arithmetic (`+`, `-`, `*`, `/`, `cmul`, `lerp`, `dot`).

| Field | Type | Default | Notes |
| :--- | :--- | :--- | :--- |
| `r` | `Float` | `—` | — |
| `g` | `Float` | `—` | — |
| `b` | `Float` | `—` | — |

### RGBA

`Struct` in `/color`

4-channel sRGB color with alpha (0.0..1.0, default alpha 1.0).

| Field | Type | Default | Notes |
| :--- | :--- | :--- | :--- |
| `r` | `Float` | `—` | — |
| `g` | `Float` | `—` | — |
| `b` | `Float` | `—` | — |
| `a` | `Float` | `—` | — |

## Functions & symbols

### contrast-ratio

`(contrast-ratio fg bg) -> Float`

Computes the WCAG 2.1 contrast ratio (`1.0` to `21.0`) between two colors.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `fg` | `Any` | no | Foreground color |
| `bg` | `Any` | no | Background color |

```tangu
(color/contrast-ratio "#000000" "#ffffff") ; -> 21.0
```

### darken

`(darken color amount) -> Oklch`

Decreases perceptual Oklch lightness `l` by `amount`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Base color |
| `amount` | `Float` | no | Lightness delta in Oklch (0.0..1.0) |

```tangu
(color/darken "#3b82f6" 0.12)
```

### desaturate

`(desaturate color amount) -> Oklch`

Decreases Oklch chroma `c` toward neutral gray.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Base color |
| `amount` | `Float` | no | Chroma decrease |

```tangu
(color/desaturate "#3b82f6" 0.05)
```

### invert

`(invert color) -> RGBA`

Inverts the RGB channels (`1.0 - c`) while keeping alpha unchanged.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/invert "#000000")
```

### lighten

`(lighten color amount) -> Oklch`

Increases perceptual Oklch lightness `l` by `amount`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Base color |
| `amount` | `Float` | no | Lightness delta in Oklch (0.0..1.0) |

```tangu
(color/lighten "#3b82f6" 0.12)
```

### linear-to-srgb

`(linear-to-srgb c) -> Float | RGB`

Encodes linear-light channel(s) into sRGB gamma.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `value` | `Float | RGB | RGBA` | no | Linear-light channel or color |

```tangu
(color/linear-to-srgb 0.214)
```

### luminance

`(luminance color) -> Float`

Computes WCAG relative luminance (`0.0` black to `1.0` white).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/luminance "#ffffff") ; -> 1.0
```

### mix

`(mix c1 c2 [t]) -> Oklch`

Perceptually interpolates between `c1` and `c2` in Oklch space.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `c1` | `Any` | no | First color |
| `c2` | `Any` | no | Second color |
| `t` | `Float` | yes | Interpolation factor 0.0..1.0 (default 0.5) |

```tangu
(color/mix "#3b82f6" "#ec4899" 0.5)
```

### parse

`(parse input) -> RGBA`

Parses any CSS color string, integer, or color struct into normalized `RGBA` (`0.0..1.0`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `input` | `String | Int | RGB | RGBA | Oklch | HSL` | no | CSS hex/rgb()/oklch() string, packed int, or color struct |

```tangu
(color/parse "#3b82f6")
```

### rotate-hue

`(rotate-hue color degrees) -> Oklch`

Shifts the Oklch hue angle by `degrees` (modulo 360).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Base color |
| `degrees` | `Float` | no | Hue rotation in degrees |

```tangu
(color/rotate-hue "#3b82f6" 180.0)
```

### saturate

`(saturate color amount) -> Oklch`

Increases Oklch chroma `c` for a more vivid color.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Base color |
| `amount` | `Float` | no | Chroma increase |

```tangu
(color/saturate "#3b82f6" 0.05)
```

### srgb-to-linear

`(srgb-to-linear c) -> Float | RGB`

Decodes gamma-encoded sRGB channel(s) into linear light.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `value` | `Float | RGB | RGBA` | no | sRGB channel or color |

```tangu
(color/srgb-to-linear 0.5)
```

### to-css

`(to-css color [format]) -> String`

Serializes a color into a standard CSS color function or hex string.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Color value |
| `format` | `KeySymbol` | yes | Optional output syntax (:rgb, :hsl, :oklch, :hex) |

```tangu
(color/to-css (color/Oklch 0.7 0.15 240.0) :oklch)
```

### to-hex

`(to-hex color [include-alpha]) -> String`

Formats a color as a CSS hex string (`#rrggbb` or `#rrggbbaa`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Color value |
| `include_alpha` | `Bool` | yes | Include alpha channel as #RRGGBBAA |

```tangu
(color/to-hex (color/RGB 1.0 0.0 0.0)) ; -> "#ff0000"
```

### to-hsl

`(to-hsl color) -> HSL`

Converts any color value into cylindrical `HSL` (`h`, `s`, `l`, `a`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/to-hsl "#10b981")
```

### to-int

`(to-int color) -> Int`

Packs an RGB(A) color into a 32-bit `0xRRGGBBAA` or `0xAARRGGBB` integer.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/to-int "#ffffff")
```

### to-oklch

`(to-oklch color) -> Oklch`

Converts any color value into perceptual Oklch (`l`, `c`, `h`, `a`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/to-oklch (color/RGB 0.2 0.4 0.9))
```

### to-rgb

`(to-rgb color) -> RGB`

Converts any color value to 3-channel `RGB`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/to-rgb "#ff0000")
```

### to-rgba

`(to-rgba color) -> RGBA`

Converts any color value to 4-channel `RGBA`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/to-rgba "#ff000080")
```

### to-u8

`(to-u8 color) -> Array`

Converts `RGBA` floating channels (`0.0..1.0`) into `[r g b a]` bytes (`0..255`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `RGB | RGBA | Oklch | HSL | String | Int` | no | Input color struct, CSS color string, or 0xRRGGBB integer |

```tangu
(color/to-u8 (color/RGBA 1.0 0.5 0.0 1.0))
```

### with-alpha

`(with-alpha color alpha) -> RGBA`

Returns a copy of `color` with its alpha channel set to `alpha`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `color` | `Any` | no | Base color |
| `alpha` | `Float` | no | Alpha channel 0.0..1.0 |

```tangu
(color/with-alpha "#000000" 0.4)
```
