chemparseplot.plot.palette#

Ruhi palette, colour science and perceptually ordered colour maps.

The Ruhi palette (Ruhila Goswami) has five colours. This module holds the colour science used to check it and to build colour maps from it.

  • sRGB primaries, white point and transfer function: IEC 61966-2-1:1999.

  • CIELAB: ISO/CIE 11664-4:2019, D65 white.

  • CIEDE2000 colour difference: ISO/CIE 11664-6:2014, as written out by Sharma, Wu and Dalal, Color Res. Appl. 30, 21 (2005).

  • Colour-vision deficiency: Machado, Oliveira and Fernandes, IEEE Trans. Vis. Comput. Graph. 15, 1291 (2009), severity 1, applied to linear sRGB. Their tritan model is an approximation, not a model of tritanopia.

  • Contrast ratio and thresholds: WCAG 2.1, success criteria 1.4.3 (text) and 1.4.11 (graphical objects).

Formula coefficients of these standards stay inline in the functions that implement them.

Added in version 1.14.0.

Module Contents#

Classes#

ColormapReport

Perceptual checks of a colour map, by vision type.

PaletteReport

WCAG contrast and CVD separation of a categorical palette.

Functions#

_xy_to_xyz

_rgb_to_xyz_matrix

as_rgb

Return colours (names, hex strings, RGB or RGBA) as an (…, 3) RGB array.

_has_strings

srgb_to_linear

Decode gamma-encoded sRGB in [0, 1] to linear light.

linear_to_srgb

Encode linear light in [0, 1] as gamma-encoded sRGB.

_lab_f

_lab_f_inv

linear_to_lab

Convert linear sRGB to CIELAB (D65).

lab_to_linear

Convert CIELAB (D65) to linear sRGB, without clipping.

srgb_to_lab

Convert sRGB colours to CIELAB (D65).

lab_to_lch

Convert CIELAB to cylindrical CIELCh with hue in degrees.

lch_to_lab

Convert CIELCh (hue in degrees) to CIELAB.

srgb_to_lch

Convert sRGB colours to CIELCh.

_in_gamut

lch_to_srgb

Convert CIELCh to sRGB, reducing chroma at fixed L* and hue if needed.

delta_e_2000

CIEDE2000 colour difference with k_L = k_C = k_H = 1.

simulate_cvd

Simulate how sRGB colours look with a colour-vision deficiency.

relative_luminance

WCAG relative luminance (the Y of linear sRGB).

contrast_ratio

WCAG 2.1 contrast ratio between two colours, from 1 to 21.

readable_color

Return color, or its shade at the same hue that reaches ratio.

best_contrast

The candidate whose smallest WCAG contrast with neighbours is largest.

contrast_limited

The entries of cmap that reach ratio against background.

pairwise_delta_e

Matrix of CIEDE2000 differences between colours seen with kind.

min_pairwise_delta_e

Smallest CIEDE2000 difference between any two of colors.

order_for_lines

Order named colours for line plots on background.

_cvd_distance

assign_element_colors

Give each element of reference one of colors.

_pchip_slopes

Fritsch-Carlson slopes for a monotone cubic Hermite interpolant.

_pchip_end_slope

_pchip

_lightness_path

Return f(L) -> (L, C, h) through LCh anchors sorted by lightness.

_even_in_arc_length

Sample paths at shared lightness values set by arc-length positions.

_unwrap_increasing

Unwrap hues so that each is at least the one before it.

_ruhi_lch

_anchors

ruhi_sequential_colors

sRGB table of the sequential Ruhi map, dark wine to sunshine.

ruhi_diverging_colors

sRGB table of the diverging Ruhi map, teal through grey to dark wine.

register_ruhi_colormaps

Register ruhi_seq and ruhi_div (and their _r) with matplotlib.

colormap_report

Lightness and CIEDE2000 step profile of a colour map for each vision type.

palette_report

Check a categorical palette against WCAG and simulated CVD.

Data#

API#

chemparseplot.plot.palette.RUHI_COLORS: dict[str, str]#

None

chemparseplot.plot.palette._SRGB_PRIMARIES_XY#

((0.64, 0.33), (0.3, 0.6), (0.15, 0.06))

chemparseplot.plot.palette._D65_XY#

(0.3127, 0.329)

chemparseplot.plot.palette._SRGB_SLOPE#

12.92

chemparseplot.plot.palette._SRGB_OFFSET#

0.055

chemparseplot.plot.palette._SRGB_GAMMA#

2.4

chemparseplot.plot.palette._SRGB_BREAK#

0.04045

chemparseplot.plot.palette.WCAG_TEXT_CONTRAST#

4.5

chemparseplot.plot.palette.WCAG_GRAPHICS_CONTRAST#

3.0

chemparseplot.plot.palette._MACHADO_2009#

None

chemparseplot.plot.palette.VISION_TYPES#

(‘normal’, ‘protan’, ‘deutan’, ‘tritan’)

chemparseplot.plot.palette._BISECTION_STEPS#

None

chemparseplot.plot.palette._CODE_MAX#

None

chemparseplot.plot.palette._TABLE_TOL#

None

chemparseplot.plot.palette._HALF_TURN#

180.0

chemparseplot.plot.palette._FULL_TURN#

360.0

chemparseplot.plot.palette._xy_to_xyz(x: float, y: float) → numpy.ndarray#
chemparseplot.plot.palette._rgb_to_xyz_matrix() → numpy.ndarray#
chemparseplot.plot.palette._RGB_TO_XYZ#

‘_rgb_to_xyz_matrix(…)’

chemparseplot.plot.palette._XYZ_TO_RGB#

‘inv(…)’

chemparseplot.plot.palette._WHITE_XYZ#

‘_xy_to_xyz(…)’

chemparseplot.plot.palette.as_rgb(colors: chemparseplot.plot.palette.ColorLike) → numpy.ndarray#

Return colours (names, hex strings, RGB or RGBA) as an (…, 3) RGB array.

Added in version 1.14.0.

chemparseplot.plot.palette._has_strings(colors) → bool#
chemparseplot.plot.palette.srgb_to_linear(rgb: numpy.typing.ArrayLike) → numpy.ndarray#

Decode gamma-encoded sRGB in [0, 1] to linear light.

Added in version 1.14.0.

chemparseplot.plot.palette.linear_to_srgb(lin: numpy.typing.ArrayLike) → numpy.ndarray#

Encode linear light in [0, 1] as gamma-encoded sRGB.

Added in version 1.14.0.

chemparseplot.plot.palette._lab_f(t: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.palette._lab_f_inv(f: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.palette.linear_to_lab(lin: numpy.typing.ArrayLike) → numpy.ndarray#

Convert linear sRGB to CIELAB (D65).

Added in version 1.14.0.

chemparseplot.plot.palette.lab_to_linear(lab: numpy.typing.ArrayLike) → numpy.ndarray#

Convert CIELAB (D65) to linear sRGB, without clipping.

Added in version 1.14.0.

chemparseplot.plot.palette.srgb_to_lab(colors: chemparseplot.plot.palette.ColorLike) → numpy.ndarray#

Convert sRGB colours to CIELAB (D65).

Added in version 1.14.0.

chemparseplot.plot.palette.lab_to_lch(lab: numpy.typing.ArrayLike) → numpy.ndarray#

Convert CIELAB to cylindrical CIELCh with hue in degrees.

Added in version 1.14.0.

chemparseplot.plot.palette.lch_to_lab(lch: numpy.typing.ArrayLike) → numpy.ndarray#

Convert CIELCh (hue in degrees) to CIELAB.

Added in version 1.14.0.

chemparseplot.plot.palette.srgb_to_lch(colors: chemparseplot.plot.palette.ColorLike) → numpy.ndarray#

Convert sRGB colours to CIELCh.

Added in version 1.14.0.

chemparseplot.plot.palette._in_gamut(lin: numpy.ndarray) → numpy.ndarray#
chemparseplot.plot.palette.lch_to_srgb(lch: numpy.typing.ArrayLike) → numpy.ndarray#

Convert CIELCh to sRGB, reducing chroma at fixed L* and hue if needed.

Out-of-gamut colours keep their lightness and hue; chroma is bisected down to the sRGB gamut boundary.

Added in version 1.14.0.

chemparseplot.plot.palette.delta_e_2000(lab1: numpy.typing.ArrayLike, lab2: numpy.typing.ArrayLike) → numpy.ndarray#

CIEDE2000 colour difference with k_L = k_C = k_H = 1.

Added in version 1.14.0.

chemparseplot.plot.palette.simulate_cvd(colors: chemparseplot.plot.palette.ColorLike, kind: str) → numpy.ndarray#

Simulate how sRGB colours look with a colour-vision deficiency.

Parameters

colors Colour names, hex strings or sRGB triples. kind "normal", "protan", "deutan" or "tritan".

Added in version 1.14.0.

chemparseplot.plot.palette.relative_luminance(colors: chemparseplot.plot.palette.ColorLike) → numpy.ndarray#

WCAG relative luminance (the Y of linear sRGB).

Added in version 1.14.0.

chemparseplot.plot.palette.contrast_ratio(color: chemparseplot.plot.palette.ColorLike, background: chemparseplot.plot.palette.ColorLike = 'white') → float#

WCAG 2.1 contrast ratio between two colours, from 1 to 21.

Added in version 1.14.0.

chemparseplot.plot.palette.readable_color(color: chemparseplot.plot.palette.ColorLike, background: chemparseplot.plot.palette.ColorLike = 'white', ratio: float = WCAG_TEXT_CONTRAST) → str#

Return color, or its shade at the same hue that reaches ratio.

The shade keeps hue, scales chroma with lightness and moves L* away from the background until the WCAG contrast ratio is met. The ratio holds for the returned 8-bit hex value, not only before rounding. Use it for direct labels (ratio 4.5, text) and outlines (ratio 3, graphics).

Added in version 1.14.0.

chemparseplot.plot.palette.best_contrast(neighbours: chemparseplot.plot.palette.ColorLike, candidates: collections.abc.Sequence[str] = ('black', 'white')) → str#

The candidate whose smallest WCAG contrast with neighbours is largest.

Picks a line colour that stays visible against every colour it borders, such as a contour line between two filled bands.

Added in version 1.14.0.

chemparseplot.plot.palette.contrast_limited(cmap: str | matplotlib.colors.Colormap, background: chemparseplot.plot.palette.ColorLike = 'white', ratio: float = WCAG_GRAPHICS_CONTRAST) → matplotlib.colors.ListedColormap#

The entries of cmap that reach ratio against background.

Lines coloured from the result all meet the WCAG 1.4.11 contrast for graphics. For maps with monotone lightness the entries kept are one end of the map, in their original order.

Added in version 1.14.0.

chemparseplot.plot.palette.pairwise_delta_e(colors: chemparseplot.plot.palette.ColorLike, kind: str = 'normal') → numpy.ndarray#

Matrix of CIEDE2000 differences between colours seen with kind.

Added in version 1.14.0.

chemparseplot.plot.palette.min_pairwise_delta_e(colors: chemparseplot.plot.palette.ColorLike, kind: str = 'normal') → float#

Smallest CIEDE2000 difference between any two of colors.

Added in version 1.14.0.

chemparseplot.plot.palette.OKABE_ITO#

(‘#000000’, ‘#E69F00’, ‘#56B4E9’, ‘#009E73’, ‘#F0E442’, ‘#0072B2’, ‘#D55E00’, ‘#CC79A7’)

chemparseplot.plot.palette.order_for_lines(colors: collections.abc.Mapping[str, str], background: chemparseplot.plot.palette.ColorLike = 'white') → list[str]#

Order named colours for line plots on background.

Colours that pass the WCAG 1.4.11 graphics contrast go first, the most contrasting one leading. Each next colour is the one whose smallest CIEDE2000 distance to those already chosen, over normal vision and the three simulated deficiencies, is largest. Colours that fail the contrast go last, ordered the same way.

Added in version 1.14.0.

chemparseplot.plot.palette._cvd_distance(color, others: collections.abc.Iterable) → float#
chemparseplot.plot.palette.RUHI_LINE_ORDER#

‘tuple(…)’

chemparseplot.plot.palette.RUHI_CYCLE#

‘tuple(…)’

chemparseplot.plot.palette.CPK_ANCHORS#

(‘H’, ‘C’, ‘N’, ‘O’)

chemparseplot.plot.palette.assign_element_colors(reference: collections.abc.Mapping[str, matplotlib.typing.ColorType], colors: collections.abc.Mapping[str, str], anchors: collections.abc.Sequence[str] = CPK_ANCHORS) → dict[str, str]#

Give each element of reference one of colors.

reference holds the usual colour of each element, such as its Jmol colour. The anchors take colors one to one, by the pairing with the least summed CIEDE2000 difference between each anchor’s reference colour and its new colour. Every other element takes the colour of the anchor whose reference colour is nearest its own, so an element drawn like carbon in the reference is drawn like carbon here.

Added in version 1.14.0.

chemparseplot.plot.palette._pchip_slopes(x: numpy.ndarray, y: numpy.ndarray) → numpy.ndarray#

Fritsch-Carlson slopes for a monotone cubic Hermite interpolant.

chemparseplot.plot.palette._pchip_end_slope(h0, h1, d0, d1) → float#
chemparseplot.plot.palette._pchip(x, y, xq) → numpy.ndarray#
chemparseplot.plot.palette._lightness_path(anchors: numpy.ndarray)#

Return f(L) -> (L, C, h) through LCh anchors sorted by lightness.

chemparseplot.plot.palette._even_in_arc_length(paths, l0: float, l1: float, positions) → numpy.ndarray#

Sample paths at shared lightness values set by arc-length positions.

positions run from 0 at l0 to 1 at l1 and are fractions of the CIEDE2000 arc length summed over all paths, so one path is spaced evenly and several paths share one lightness schedule. Returns an array of shape (len(paths), len(positions), 3).

chemparseplot.plot.palette._unwrap_increasing(hues: collections.abc.Sequence[float]) → numpy.ndarray#

Unwrap hues so that each is at least the one before it.

chemparseplot.plot.palette._ruhi_lch() → dict[str, numpy.ndarray]#
chemparseplot.plot.palette._anchors(rows) → numpy.ndarray#
chemparseplot.plot.palette.ruhi_sequential_colors(n: int | None = None) → numpy.ndarray#

sRGB table of the sequential Ruhi map, dark wine to sunshine.

The anchors are magenta, coral and sunshine, whose lightness and hue rise together, below a dark end with teal’s lightness and chroma at magenta’s hue, so the map spans the palette’s lightness range. Chroma and hue follow monotone cubic curves in L* and the table is spaced evenly in CIEDE2000 arc length (Kovesi, arXiv:1509.03700). Teal and sky are left out. Sky’s lightness lies between magenta’s and coral’s, so a map through it turns the hue by 94 and then 116 degrees over 15.5 units of L*, and its lightness is no longer monotone for simulated protan, deutan or tritan vision. Starting at teal itself turns the hue by 166 degrees on the way to magenta, and lightness is no longer monotone for simulated protan vision.

Added in version 1.14.0.

chemparseplot.plot.palette.ruhi_diverging_colors(n: int | None = None) → numpy.ndarray#

sRGB table of the diverging Ruhi map, teal through grey to dark wine.

The cool arm runs from teal through sky, the warm arm from a dark end (teal’s lightness and chroma at magenta’s hue) through magenta and coral. Both meet in a neutral grey at sunshine’s lightness. The arms share one lightness schedule, spaced evenly in their summed CIEDE2000 arc length, so entries equally far from the centre have equal L* (Moreland, ISVC 2009).

Added in version 1.14.0.

chemparseplot.plot.palette.RUHI_SEQUENTIAL#

‘ruhi_seq’

chemparseplot.plot.palette.RUHI_DIVERGING#

‘ruhi_div’

chemparseplot.plot.palette.register_ruhi_colormaps() → None#

Register ruhi_seq and ruhi_div (and their _r) with matplotlib.

Added in version 1.14.0.

class chemparseplot.plot.palette.ColormapReport#

Perceptual checks of a colour map, by vision type.

Added in version 1.14.0.

name: str#

None

lightness: dict[str, numpy.ndarray]#

None

steps: dict[str, numpy.ndarray]#

None

lightness_monotonic(kind: str = 'normal') → bool#

True if L* rises or falls strictly along the whole map.

arms_monotonic(kind: str = 'normal') → bool#

True if L* is strictly monotone on each side of the centre, in opposite directions, as a diverging map needs.

step_cv(kind: str = 'normal') → float#

Coefficient of variation of CIEDE2000 steps between entries.

chemparseplot.plot.palette.colormap_report(cmap: str | matplotlib.colors.Colormap) → chemparseplot.plot.palette.ColormapReport#

Lightness and CIEDE2000 step profile of a colour map for each vision type.

Added in version 1.14.0.

class chemparseplot.plot.palette.PaletteReport#

WCAG contrast and CVD separation of a categorical palette.

Added in version 1.14.0.

names: tuple[str, ...]#

None

hex: tuple[str, ...]#

None

lch: numpy.ndarray#

None

contrast: tuple[float, ...]#

None

min_delta_e: dict[str, float]#

None

reference_min_delta_e: dict[str, float]#

None

passes_graphics_contrast() → tuple[str, ...]#

Names of the colours that reach the WCAG 1.4.11 contrast for graphics.

at_least_reference(kind: str) → bool#

True if the palette separates its colours seen with kind at least as well as the reference palette does (smallest CIEDE2000 difference).

chemparseplot.plot.palette.palette_report(colors: collections.abc.Mapping[str, str] | None = None, background: chemparseplot.plot.palette.ColorLike = 'white', reference: collections.abc.Sequence[str] = OKABE_ITO) → chemparseplot.plot.palette.PaletteReport#

Check a categorical palette against WCAG and simulated CVD.

min_delta_e[kind] is the smallest CIEDE2000 difference between any two colours seen with kind; reference_min_delta_e is the same number for reference (Okabe-Ito by default).

Added in version 1.14.0.