# gradient

Python API: `luxonis_ml.utils.color.gradient`

Color gradients (colormaps) for scalar fields such as heatmaps.

A
[Gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient)
maps a scalar in `[0, 1]` to a
[Color](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color)
by linearly interpolating between ordered color stops. It is the color model for heatmaps, kept separate from the class
[Palette](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette)
because a heatmap colors a continuous magnitude, not a set of discrete classes.

A handful of ready-made gradients are registered by name in
[GRADIENTS](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Module-Attributes-GRADIENTS)
(perceptually-uniform ones like `"viridis"`/`"turbo"` plus classics like `"jet"` and `"hot"`). Resolve a name or pass your own
with
[resolve_gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Functions-resolve_gradient);
build a custom one with
[Gradient.from_colors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient-from_colors).

Only
[Gradient.colorize](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient-colorize)
needs NumPy, and it imports it when called, so this module stays importable on a base `luxonis-ml` install that has no NumPy.

## Classes

| Name | Description |
| --- | --- |
| [`Gradient`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient) | A colormap: an ordered set of color stops interpolated in RGB. |

### class `Gradient`

```python
class Gradient
```

A colormap: an ordered set of color stops interpolated in RGB.

A scalar `t` in `[0, 1]` is mapped to a color by piecewise-linear interpolation between the surrounding stops. Values outside `[0,
1]` are clamped.

> **Examples**
> ```pycon
>>> g = Gradient.from_colors(["#000000", "#ff0000"])
>>> g.color_at(0.0)
Color(r=0, g=0, b=0, a=255)
>>> g.color_at(0.5)
Color(r=128, g=0, b=0, a=255)
>>> g.color_at(1.0)
Color(r=255, g=0, b=0, a=255)
```

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `stops` | `tuple[tuple[float, Color], ...]` | Ascending `(position, color)` pairs with positions in `[0, 1]`; the first should be at `0.0` and the last at `1.0`. |

Methods

| Name | Description |
| --- | --- |
| [`color_at`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient-color_at) | Return the color at scalar `t` in `[0, 1]` (clamped). |
| [`colorize`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient-colorize) | Map a scalar field to RGB, interpolating each channel through the stops. |
| [`from_colors`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient-from_colors) | Build a gradient from colors, evenly spaced unless positions are given. |

#### method `color_at`

```python
def color_at(t: float) -> Color
```

Return the color at scalar `t` in `[0, 1]` (clamped).

All four channels are interpolated, so a gradient between translucent stops fades in alpha too.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `t` | `float` | The position along the gradient. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | The interpolated [Color](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color). |

#### method `colorize`

```python
def colorize(field: npt.ArrayLike) -> np.ndarray
```

Map a scalar field to RGB, interpolating each channel through the stops.

Only the color channels are mapped: the result is opaque RGB, so any alpha on the stops is ignored. Use [color_at](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient-color_at) when alpha matters.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `field` | `npt.ArrayLike` | An array of scalars, expected in `[0, 1]` (values are clamped). Any shape is accepted, as is anything `numpy.asarray` converts to a float array. | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | A `uint8` array shaped `(*field.shape, 3)` in RGB order. |

#### class method `from_colors`

```python
def from_colors(
    colors: list[ColorLike],
    *,
    positions: list[float] | None = None,
) -> Gradient
```

Build a gradient from colors, evenly spaced unless positions are given.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `colors` | `list[ColorLike]` | Two or more colors (hex strings, tuples, or [Color](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color)), from the low end of the scale to the high end. | required |
| `positions` | `list[float] | None` | Optional stop positions in `[0, 1]`, one per color; when `None` the colors are spread evenly from `0` to `1`. | `None` |

Returns

| Type | Description |
| --- | --- |
| `Gradient` | The [Gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient). |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If fewer than two colors are given, or `positions` is given with a different length than `colors`, a position outside `[0, 1]`, or a repeated position. |

## Functions

| Name | Description |
| --- | --- |
| [`resolve_gradient`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Functions-resolve_gradient) | Resolve a gradient or a preset name to a [Gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient). |

### function `resolve_gradient`

```python
def resolve_gradient(gradient: Gradient | str) -> Gradient
```

Resolve a gradient or a preset name to a [Gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient).

> **Examples**
> ```pycon
>>> resolve_gradient("grayscale").color_at(1.0)
Color(r=255, g=255, b=255, a=255)
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `gradient` | `Gradient | str` | A [Gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient), or the name of a preset in [GRADIENTS](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Module-Attributes-GRADIENTS). | required |

Returns

| Type | Description |
| --- | --- |
| `Gradient` | The [Gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Classes-Gradient). |

Raises

| Type | Description |
| --- | --- |
| `KeyError` | If a name is given that is not a registered preset. |

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`DEFAULT_DIVERGING_GRADIENT`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Module-Attributes-DEFAULT_DIVERGING_GRADIENT) | `str` | Name of the gradient used for a signed field when none is given. |
| [`DEFAULT_GRADIENT`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Module-Attributes-DEFAULT_GRADIENT) | `str` | Name of the gradient used for an unsigned field when none is given. |
| [`DIVERGING_GRADIENTS`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Module-Attributes-DIVERGING_GRADIENTS) |  | Presets built around a neutral midpoint. |
| [`GRADIENTS`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Module-Attributes-GRADIENTS) | `dict[str, Gradient]` | The preset gradients, keyed by name. See [resolve_gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Functions-resolve_gradient). |

### constant `DEFAULT_DIVERGING_GRADIENT`

```python
DEFAULT_DIVERGING_GRADIENT: str = 'coolwarm'
```

Name of the gradient used for a signed field when none is given.

### constant `DEFAULT_GRADIENT`

```python
DEFAULT_GRADIENT: str = 'turbo'
```

Name of the gradient used for an unsigned field when none is given.

### constant `DIVERGING_GRADIENTS`

```python
DIVERGING_GRADIENTS = frozenset({'coolwarm', 'rdbu', 'seismic'})
```

Presets built around a neutral midpoint.

Each has an odd number of evenly spaced stops with a near-white one in the middle, so centering a heatmap puts that neutral color
exactly on the center value. The sequential presets have no such anchor: centering them still balances the range, but no
particular color marks the middle.

### constant `GRADIENTS`

```python
GRADIENTS: dict[str, Gradient] = {
    name: Gradient.from_colors(list(colors)) for (name, colors) in _PRESETS.items(),
}
```

The preset gradients, keyed by name. See
[resolve_gradient](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/gradient.md#gradient-Functions-resolve_gradient).
