# qualpal.Palette

Python · 1.1.0

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

<a id="constructor"></a>

```python
Palette(colors: collections.abc.Sequence[Color | str])
```

A collection of colors that behaves like a list.

Palette objects are immutable.

<a id="__init__"></a>

## `__init__`

```python
__init__(colors: collections.abc.Sequence[Color | str]) -> None
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Create a Palette from a list of colors.

### Parameters

**colors** : *Sequence\[Color \| str\]*

Sequence of Color objects or hex strings

### Raises

`ValueError`

If any color is invalid

<a id="hex"></a>

## `hex`

```python
hex() -> list[str]
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Get list of hex color strings.

### Returns

*list\[str\]*

List of hex strings in format \#rrggbb (lowercase)

<a id="rgb"></a>

## `rgb`

```python
rgb() -> list[tuple[float, float, float]]
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Get RGB values as list of tuples.

### Returns

*list\[tuple\[float, float, float\]\]*

List of RGB tuples in range \[0.0, 1.0\]

<a id="to_css"></a>

## `to_css`

```python
to_css(prefix: str = "color") -> list[str]
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Export palette as CSS custom properties (CSS variables).

### Parameters

**prefix** : *str*

Prefix for CSS variable names (default: ‘color’)

### Returns

*list\[str\]*

List of CSS custom property declarations

### Examples

```python
>>> from qualpal import Palette
>>> pal = Palette(['#ff0000', '#00ff00', '#0000ff'])
>>> pal.to_css()
['--color-1: #ff0000;', '--color-2: #00ff00;', '--color-3: #0000ff;']
>>> pal.to_css(prefix='theme')
['--theme-1: #ff0000;', '--theme-2: #00ff00;', '--theme-3: #0000ff;']
```

<a id="to_json"></a>

## `to_json`

```python
to_json() -> str
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Export palette as JSON array of hex colors.

### Returns

*str*

JSON string containing array of hex color strings

### Examples

```python
>>> from qualpal import Palette
>>> pal = Palette(['#ff0000', '#00ff00', '#0000ff'])
>>> pal.to_json()
'["#ff0000", "#00ff00", "#0000ff"]'
```

<a id="show"></a>

## `show`

```python
show(labels: bool | list[str] | None = None) -> object
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Display palette as color swatches (requires matplotlib).

### Parameters

**labels** : *bool \| list\[str\] \| None*) –

- None (default): No labels
- True: Show hex codes as labels
- list\[str\]: Custom labels for each color

### Returns

*matplotlib.figure.Figure*

Matplotlib Figure object that can be saved or further customized

### Raises

`ImportError`

If matplotlib is not installed

### Examples

```python
>>> from qualpal import Palette
>>> pal = Palette(['#ff0000', '#00ff00', '#0000ff'])
>>> fig = pal.show()  # Display swatches
>>> fig = pal.show(labels=True)  # With hex codes
>>> fig = pal.show(labels=['Red', 'Green', 'Blue'])  # Custom labels
>>> fig.savefig('palette.png')  # Save to file
```

<a id="distance_matrix"></a>

## `distance_matrix`

```python
distance_matrix(metric: str = "ciede2000") -> list[list[float]]
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Calculate pairwise distance matrix for all colors in the palette.

### Parameters

**metric** : *str*

Distance metric to use. Options:
- ‘ciede2000’ (default): CIEDE2000 metric
- ‘din99d’: DIN99d metric
- ‘cie76’: CIE76 (Euclidean distance in Lab space)

### Returns

*list\[list\[float\]\]*

Symmetric distance matrix where element \[i\]\[j\] is the distance
between colors i and j. Diagonal elements are 0.0.

### Examples

```python
>>> from qualpal import Palette
>>> pal = Palette(['#ff0000', '#00ff00', '#0000ff'])
>>> matrix = pal.distance_matrix()
>>> len(matrix)
3
>>> matrix[0][0]  # Distance to self
0.0
```

<a id="min_distance"></a>

## `min_distance`

```python
min_distance(metric: str = "ciede2000") -> float
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Get the minimum pairwise distance between any two colors.

### Parameters

**metric** : *str*

Distance metric to use (default: ‘ciede2000’)

### Returns

*float*

Minimum distance between any pair of distinct colors

### Examples

```python
>>> from qualpal import Palette
>>> pal = Palette(['#ff0000', '#00ff00', '#0000ff'])
>>> min_dist = pal.min_distance()
>>> min_dist > 0
True
```

<a id="min_distances"></a>

## `min_distances`

```python
min_distances(metric: str = "ciede2000") -> list[float]
```

[Source](<https://github.com/jolars/qualpal-py/blob/72a59d3a05195fb48e82e53ededa466950ed7d67/qualpal/palette.py>)

Get minimum distance for each color to its nearest neighbor.

### Parameters

**metric** : *str*

Distance metric to use (default: ‘ciede2000’)

### Returns

*list\[float\]*

List where element i is the minimum distance from color i
to any other color in the palette

### Examples

```python
>>> from qualpal import Palette
>>> pal = Palette(['#ff0000', '#00ff00', '#0000ff'])
>>> min_dists = pal.min_distances()
>>> len(min_dists)
3
>>> all(d > 0 for d in min_dists)
True
```
