Qualpal
Repositories

qualpal.Color

Python · python · 1.1.0

Color(hex_color: str)

A color with various representations and conversions.

Color objects are immutable.

__init__

__init__(hex_color: str) -> None

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

from_rgb

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

Create a Color from RGB values.

Parameters

r, g, b : float

RGB values in range [0.0, 1.0]

Returns

Color

New Color object

from_hsl

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

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

hex

hex() -> str

Get hex representation.

Returns

str

Hex color string in format #rrggbb (lowercase)

rgb

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

Get RGB tuple in range [0.0, 1.0].

Returns

tuple[float, float, float]

RGB values as (r, g, b)

rgb255

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

Get RGB tuple in range [0, 255].

Returns

tuple[int, int, int]

RGB values as (r, g, b) integers

hsl

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

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]

xyz

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

Get XYZ tuple.

Returns

tuple[float, float, float]

XYZ values as (x, y, z)

lab

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

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]

lch

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

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)

distance

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

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

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

simulate_cvd

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

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

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