# base

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

Color parsing and manipulation.

The
[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)
class is the single color primitive shared across `luxonis-ml`. The visualization layer re-exports it, and other submodules (e.g.
augmentation fill values) parse colors through it too, so no module reimplements color handling.

Colors are normalized to an `(r, g, b, a)` tuple of 8-bit integers.
[Color.parse](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-parse)
accepts the shapes users actually have on hand (hex strings, CSS color names, a single grayscale integer, RGB/RGBA tuples, or
another
[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))
and exposes the HSL-space operations the palette and style derivation rely on.

## Classes

| Name | Description |
| --- | --- |
| [`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) | An immutable RGBA color with HSL-space helpers. |

### class `Color`

```python
class Color
```

An immutable RGBA color with HSL-space helpers.

> **Examples**
> ```pycon
>>> Color.parse("#4c8dff")
Color(r=76, g=141, b=255, a=255)
>>> Color.parse("#abc")
Color(r=170, g=187, b=204, a=255)
>>> Color.parse("white")
Color(r=255, g=255, b=255, a=255)
>>> Color.parse(128)
Color(r=128, g=128, b=128, a=255)
>>> Color.parse(300)  # out-of-range channels are clamped
Color(r=255, g=255, b=255, a=255)
>>> Color.parse((255, 0, 0))
Color(r=255, g=0, b=0, a=255)
>>> Color.parse("white").rgb
(255, 255, 255)
>>> Color(255, 0, 0).with_alpha(0.5)
Color(r=255, g=0, b=0, a=128)
>>> Color(1, 2, 3, 4).rgba
(1, 2, 3, 4)
>>> Color(255, 255, 255).readable_text_color()
Color(r=17, g=17, b=17, a=255)
>>> Color(0, 0, 0).readable_text_color()
Color(r=255, g=255, b=255, a=255)
>>> c = Color(120, 120, 120)
>>> c.lighten(0.5).hls[1] > c.hls[1]
True
>>> c.darken(0.5).hls[1] < c.hls[1]
True
>>> Color.parse("#12")
Traceback (most recent call last):
    ...
ValueError: invalid hex color '#12'
```

Attributes

| Name | Type | Description |
| --- | --- | --- |
| `a` | `int` | Alpha channel, 0-255 (255 is opaque). |
| `b` | `int` | Blue channel, 0-255. |
| `g` | `int` | Green channel, 0-255. |
| `r` | `int` | Red channel, 0-255. |

Properties

| Name | Type | Description |
| --- | --- | --- |
| [`hls`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-hls) | `tuple[float, float, float]` | The color as a `(hue, lightness, saturation)` tuple in `[0, 1]`. |
| [`is_light`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-is_light) | `bool` | Whether this color is bright enough to need dark text on top. |
| [`rgb`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-rgb) | `RGB` | The color as an `(r, g, b)` tuple. |
| [`rgba`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-rgba) | `RGBA` | The color as an `(r, g, b, a)` tuple. |

Methods

| Name | Description |
| --- | --- |
| [`darken`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-darken) | Move the color toward black in HSL space. |
| [`from_hls`](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_hls) | Build a color from hue/lightness/saturation. |
| [`lighten`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-lighten) | Move the color toward white in HSL space. |
| [`parse`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-parse) | Coerce a color-like value into 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). |
| [`readable_text_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-readable_text_color) | Return near-black or white, whichever is more readable on this color. |
| [`saturate`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-saturate) | Increase (or, with a negative amount, decrease) saturation. |
| [`shift_hue`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-shift_hue) | Rotate the hue around the color wheel. |
| [`with_alpha`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Classes-Color-with_alpha) | Return a copy with a new alpha. |

#### property `hls`

```python
hls: tuple[float, float, float]
```

The color as a `(hue, lightness, saturation)` tuple in `[0, 1]`.

#### property `is_light`

```python
is_light: bool
```

Whether this color is bright enough to need dark text on top.

Weights the channels the way WCAG relative luminance does, but leaves them gamma-encoded rather than linearizing first. The result approximates perceived brightness, which is all a light/dark decision needs.

#### property `rgb`

```python
rgb: RGB
```

The color as an `(r, g, b)` tuple.

#### property `rgba`

```python
rgba: RGBA
```

The color as an `(r, g, b, a)` tuple.

#### method `darken`

```python
def darken(amount: float) -> Color
```

Move the color toward black in HSL space.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `amount` | `float` | Fraction of the current lightness to remove, in `[0, 1]`. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | A darker [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), alpha preserved. |

#### class method `from_hls`

```python
def from_hls(hue: float, lightness: float, saturation: float, a: int = 255) -> Color
```

Build a color from hue/lightness/saturation.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `hue` | `float` | Hue in `[0, 1]`. | required |
| `lightness` | `float` | Lightness in `[0, 1]`. | required |
| `saturation` | `float` | Saturation in `[0, 1]`. | required |
| `a` | `int` | Alpha channel, 0-255. | `255` |

Returns

| Type | Description |
| --- | --- |
| `Color` | The corresponding [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 `lighten`

```python
def lighten(amount: float) -> Color
```

Move the color toward white in HSL space.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `amount` | `float` | Fraction of the remaining lightness to add, in `[0, 1]`. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | A lighter [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), alpha preserved. |

#### class method `parse`

```python
def parse(value: object) -> Color
```

Coerce a color-like value into 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).

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `value` | `object` | A hex string (`"#rgb"`, `"#rrggbb"`, `"#rrggbbaa"`, with or without the leading `#`), a CSS color name (`"red"`), a single grayscale integer (`128` → gray), an `(r, g, b)` or `(r, g, b, a)` tuple, or an existing [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). | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | The corresponding [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). |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If the value cannot be interpreted as a color. |

#### method `readable_text_color`

```python
def readable_text_color() -> Color
```

Return near-black or white, whichever is more readable on this color.

Returns

| Type | Description |
| --- | --- |
| `Opaque Color` | a soft near-black on light colors, white on dark ones, so label text on a chip of this color stays legible. |

#### method `saturate`

```python
def saturate(amount: float) -> Color
```

Increase (or, with a negative amount, decrease) saturation.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `amount` | `float` | Fraction of the remaining saturation to add, in `[-1, 1]`. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | 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) with adjusted saturation, alpha preserved. |

#### method `shift_hue`

```python
def shift_hue(turns: float) -> Color
```

Rotate the hue around the color wheel.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `turns` | `float` | Amount to rotate, in turns (`1.0` is a full circle). | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | A hue-rotated [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), alpha preserved. |

#### method `with_alpha`

```python
def with_alpha(a: float) -> Color
```

Return a copy with a new alpha.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `a` | `float` | Alpha as an int (0-255) or a float in `[0, 1]`. | required |

Returns

| Type | Description |
| --- | --- |
| `Color` | A new [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) with the requested alpha. |

## Module Attributes

| Name | Type | Description |
| --- | --- | --- |
| [`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) | `TypeAlias` | a hex string or color name, a grayscale int, an RGB/RGBA tuple, or 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). |
| [`RGB`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Module-Attributes-RGB) | `TypeAlias` | An `(r, g, b)` tuple of 8-bit integers (0-255). |
| [`RGBA`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-ml/luxonis-ml-api-reference/utils/color/base.md#base-Module-Attributes-RGBA) | `TypeAlias` | An `(r, g, b, a)` tuple of 8-bit integers (0-255). |

### type alias `ColorLike`

```python
ColorLike: TypeAlias = str | int | tuple[int, ...] | Color
```

a hex string or color name, a grayscale int, an RGB/RGBA tuple, or 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).

### type alias `RGB`

```python
RGB: TypeAlias = tuple[int, int, int]
```

An `(r, g, b)` tuple of 8-bit integers (0-255).

### type alias `RGBA`

```python
RGBA: TypeAlias = tuple[int, int, int, int]
```

An `(r, g, b, a)` tuple of 8-bit integers (0-255).
