# palette

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

Distinct class colors from a pluggable, index-based generator.

A
[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)
hands each new class the next color from a color generator: a callable `int -> Color` that maps a sequence position to a color.
The default generator,
[GoldenRatioColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-GoldenRatioColors),
offsets each hue from the last by the golden angle (~137.5°) so no two classes land on a near-identical hue, the failure mode a
fixed palette (or a hash into one) can't avoid once the class count approaches the palette size. Class labels stay on this
distinct-hue scheme on purpose; the Luxonis brand colors are reserved for chrome (see
[luxonis_ml.utils.color.brand](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/brand.md)),
though
[SequenceColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-SequenceColors)
lets a caller anchor a palette to the brand
[BRAND_COLORS](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-BRAND_COLORS)
when they want to.

Swapping the whole color scheme is a single argument (`Palette(generator=...)`), so the strategy lives in exactly one place and
callers only ever touch
[Palette.color_for](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-color_for).

Because the spacing guarantee only holds for sequential indices, colors are assigned in order of first appearance and memoized:
within a process, a class keeps its color across every image (the module-level
[DEFAULT_PALETTE](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-DEFAULT_PALETTE)
is shared). The trade-off is that a class's color depends on the order classes are first seen, not on its name alone: stable
within a run and across runs that request classes in the same order, but not across arbitrary reorderings. Pin a color explicitly
with
[Palette.pin](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-pin)
or by pre-registering classes in a fixed order when a name must map to one exact color forever.

## Classes

| Name | Description |
| --- | --- |
| [`GoldenRatioColors`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-GoldenRatioColors) | Maps a sequence index to a distinct color via golden-ratio hue spacing. |
| [`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) | Assigns colors to classes in order of first use, from a color generator. |
| [`SequenceColors`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-SequenceColors) | Hand out a fixed list of colors first, then fall back to a generator. |

### class `GoldenRatioColors`

```python
class GoldenRatioColors
```

Maps a sequence index to a distinct color via golden-ratio hue spacing.

This is the default
[ColorGenerator](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-ColorGenerator).
Every knob that shapes the look lives here, so restyling the whole palette means constructing one of these (or any other `int ->
Color` callable) and passing it to
[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).

> **Examples**
> ```pycon
>>> gen = GoldenRatioColors()
>>> gen(0) == gen(0)
True
>>> gen(0) == gen(1)
False
```

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `hue0` | `float` | Hue of index 0, in `[0, 1]` turns (default a blue). |
| `lightness` | `float` | Base lightness of every color, in `[0, 1]`. |
| `lightness_jitter` | `float` | Peak lightness deviation from `lightness`, applied via a second low-discrepancy sequence so brightness varies subtly (which keeps colors apart even when hues get crowded at high class counts). |
| `saturation` | `float` | Fixed saturation of every color, in `[0, 1]`. |

### class `Palette`

```python
class Palette
```

Assigns colors to classes in order of first use, from a color generator.

> **Examples**
> A class keeps its color; different classes get different colors:

```pycon
>>> p = Palette()
>>> p.color_for("car") == p.color_for("car")
True
>>> p.color_for("car") == p.color_for("bus")
False
>>> len(p)
2
```

Two fresh palettes agree as long as classes are requested in the same order (`"car"` is index 0 in both) ...

```pycon
>>> Palette().color_for("car") == Palette().color_for("car")
True
```

... but the color follows first-seen order, not the name, so reordering changes it:

```pycon
>>> Palette(["a", "b"]).color_for("b") == Palette(
...     ["b", "a"]
... ).color_for("b")
False
```

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-__init__) | Create a palette, optionally pinning class order and exact colors. |
| [`at`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-at) | Return the color for sequence position `index` (without registering it). |
| [`color_for`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-color_for) | Return the color assigned to `key`, assigning a new one if unseen. |
| [`pin`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-pin) | Pin `key` to exactly `color` and return `self` for chaining. |
| [`with_colors`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-with_colors) | Return a copy of this palette with additional class-color pins. |

#### method `__init__`

```python
def __init__(
    classes: Iterable[str] | None = None,
    *,
    generator: ColorGenerator | None = None,
    colors: Mapping[str, ColorLike] | None = None,
)
```

Create a palette, optionally pinning class order and exact colors.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `classes` | `Iterable[str] | None` | Class names to register up front, in the order that fixes their colors. Any class not listed is assigned on first use. | `None` |
| `generator` | `ColorGenerator | None` | The `int -> Color` strategy; defaults to [GoldenRatioColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-GoldenRatioColors). | `None` |
| `colors` | `Mapping[str, ColorLike] | None` | Explicit `{class_name: color}` pins. A pinned class always gets exactly that color and never consumes a generator slot, so pins do not shift the sequence colors of the other classes. | `None` |

#### method `at`

```python
def at(index: int) -> Color
```

Return the color for sequence position `index` (without registering it).

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `index` | `int` | The zero-based position in the generator's sequence. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | The generated [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 `color_for`

```python
def color_for(key: str) -> Color
```

Return the color assigned to `key`, assigning a new one if unseen.

The first time a key is seen it takes the next color in the sequence; every later call for that key returns the same color. Two threads racing to register different keys get different colors: only the assignment is locked, so the common case of an already-colored key stays lock-free.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `key` | `str` | The label (or any string identity) to color. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | The assigned [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 `pin`

```python
def pin(key: str, color: ColorLike) -> Palette
```

Pin `key` to exactly `color` and return `self` for chaining.

A pinned class always gets this color from [color_for](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-Palette-color_for), overriding any generated one, and never consumes a generator slot.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `key` | `str` | The class name (or string identity) to pin. | required |
| `color` | `ColorLike` | The exact color for `key` (any [ColorLike](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Module-Attributes-ColorLike)). | required |

Returns

| Type | Description |
| --- | --- |
| `Palette` | This palette, to allow fluent chaining. |

#### method `with_colors`

```python
def with_colors(colors: Mapping[str, ColorLike]) -> Palette
```

Return a copy of this palette with additional class-color pins.

The copy shares the generator and inherits already-assigned colors and pins; `colors` are merged on top. The original is not mutated, so a shared palette can be specialized per render without side effects.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `colors` | `Mapping[str, ColorLike]` | Extra `{class_name: color}` pins to add. | required |

Returns

| Type | Description |
| --- | --- |
| `Palette` | A new [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) with the merged pins. |

### class `SequenceColors`

```python
class SequenceColors
```

Hand out a fixed list of colors first, then fall back to a generator.

Wraps a list of `anchors` (e.g. the Luxonis [BRAND_COLORS](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-BRAND_COLORS)) so the first classes seen get those exact colors, in order. Once the anchors run out, index `n` continues through `overflow` (a [GoldenRatioColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-GoldenRatioColors) generator by default, queried with an index that restarts at the first post-anchor class), so any number of classes still receive distinct colors.

> **Examples**
> ```pycon
>>> from luxonis_ml.utils.color import Color
>>> gen = SequenceColors((Color(255, 0, 0), Color(0, 255, 0)))
>>> gen(0)
Color(r=255, g=0, b=0, a=255)
>>> gen(1)
Color(r=0, g=255, b=0, a=255)
>>> gen(2) == gen(2)  # overflow is deterministic
True
```

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `anchors` | `tuple[Color, ...]` | Colors handed out for indices `0 .. len(anchors) - 1`. |
| `overflow` | `ColorGenerator` | Generator used once the anchors are exhausted. |

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`BRAND_COLORS`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-BRAND_COLORS) | `tuple[Color, ...]` | The Luxonis brand color sequence, for use with [SequenceColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-SequenceColors). |
| [`ColorGenerator`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-ColorGenerator) |  | A callable mapping a zero-based sequence index 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). |
| [`DEFAULT_PALETTE`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Module-Attributes-DEFAULT_PALETTE) |  | The process-wide default [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), shared so a class keeps its color. |

### constant `BRAND_COLORS`

```python
BRAND_COLORS: tuple[Color, ...] = (
    PURPLE,
    GREEN,
    ORANGE,
    RED,
    PERIWINKLE,
    MINT,
    AMBER,
    SALMON,
)
```

The Luxonis brand color sequence, for use with
[SequenceColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-SequenceColors).

### type alias `ColorGenerator`

```python
ColorGenerator = Callable[[int], Color]
```

A callable mapping a zero-based sequence index 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).

### constant `DEFAULT_PALETTE`

```python
DEFAULT_PALETTE = Palette()
```

The process-wide default
[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),
shared so a class keeps its color.

Uses the distinct-hue
[GoldenRatioColors](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/palette.md#palette-Classes-GoldenRatioColors)
generator: label colors are chosen to stay far apart, not to match the brand (which is reserved for chrome).
