# qualpal.Color

Python · 1.1.0

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

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

```python
Color(hex_color: str)
```

A color with various representations and conversions.

Color objects are immutable.

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

## `__init__`

```python
__init__(hex_color: str) -> None
```

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

Create a Color from a hex string.

### Parameters

**hex\_color** : *str*

Hex color string in format \#RRGGBB

### Raises

`ValueError`

If hex\_color is not a valid hex color string

<a id="from_rgb"></a>

## `from_rgb`

```python
from_rgb(r: float, g: float, b: float) -> typing_extensions.Self
```

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

Create a Color from RGB values.

### Parameters

**r, g, b** : *float*

RGB values in range \[0.0, 1.0\]

### Returns

*Color*

New Color object

<a id="from_hsl"></a>

## `from_hsl`

```python
from_hsl(h: float, s: float, l: float) -> typing_extensions.Self
```

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

Create a Color from HSL values.

### Parameters

**h** : *float*

Hue in degrees \[0.0, 360.0)

**s** : *float*

Saturation in range \[0.0, 1.0\]

**l** : *float*

Lightness in range \[0.0, 1.0\]

### Returns

*Color*

New Color object

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

## `hex`

```python
hex() -> str
```

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

Get hex representation.

### Returns

*str*

Hex color string in format \#rrggbb (lowercase)

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

## `rgb`

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

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

Get RGB tuple in range \[0.0, 1.0\].

### Returns

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

RGB values as (r, g, b)

<a id="rgb255"></a>

## `rgb255`

```python
rgb255() -> tuple[int, int, int]
```

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

Get RGB tuple in range \[0, 255\].

### Returns

*tuple\[int, int, int\]*

RGB values as (r, g, b) integers

<a id="hsl"></a>

## `hsl`

```python
hsl() -> tuple[float, float, float]
```

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

Get HSL tuple.

### Returns

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

HSL values as (h, s, l) where h is in degrees \[0, 360)
and s, l are in range \[0.0, 1.0\]

<a id="xyz"></a>

## `xyz`

```python
xyz() -> tuple[float, float, float]
```

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

Get XYZ tuple.

### Returns

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

XYZ values as (x, y, z)

<a id="lab"></a>

## `lab`

```python
lab() -> tuple[float, float, float]
```

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

Get Lab tuple.

### Returns

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

Lab values as (l, a, b) where l is in range \[0, 100\]
and a, b are in range \[-128, 127\]

<a id="lch"></a>

## `lch`

```python
lch() -> tuple[float, float, float]
```

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

Get LCH tuple.

### Returns

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

LCH values as (l, c, h) where l is in range \[0, 100\],
c is chroma \[0, ∞), and h is hue in degrees \[0, 360)

<a id="distance"></a>

## `distance`

```python
distance(other: Color | str, metric: str = "ciede2000") -> float
```

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

Calculate perceptual color difference to another color.

### Parameters

**other** : *Color \| str*

Another Color object or hex color string

**metric** : *str*

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

### Returns

*float*

Perceptual color difference

### Raises

`ValueError`

If metric is invalid or other color is not a valid color

### Examples

```python
>>> red = Color('#ff0000')
>>> green = Color('#00ff00')
>>> round(red.distance(green), 2)
86.61
>>> round(red.distance('#00ff00', metric='din99d'), 2)
32.77
```

<a id="simulate_cvd"></a>

## `simulate_cvd`

```python
simulate_cvd(cvd_type: str, severity: float = 1.0) -> qualpal.Color
```

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

Simulate color vision deficiency on this color.

### Parameters

**cvd\_type** : *str*

Type of color vision deficiency:
- ‘protan’: Protanomaly/Protanopia (red-weak/blind)
- ‘deutan’: Deuteranomaly/Deuteranopia (green-weak/blind)
- ‘tritan’: Tritanomaly/Tritanopia (blue-weak/blind)

**severity** : *float*

Severity of the deficiency in range \[0, 1\]:
- 0.0: Normal vision (no change)
- 1.0: Complete deficiency

### Returns

*Color*

New Color object showing how this color appears with CVD

### Raises

`ValueError`

If cvd\_type is invalid or severity is out of range

### Examples

```python
>>> red = Color('#ff0000')
>>> red_protan = red.simulate_cvd('protan', severity=1.0)
>>> red_deutan = red.simulate_cvd('deutan', severity=0.5)
```
