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#
Perceptual checks of a colour map, by vision type. |
|
WCAG contrast and CVD separation of a categorical palette. |
Functions#
Return colours (names, hex strings, RGB or RGBA) as an (…, 3) RGB array. |
|
Decode gamma-encoded sRGB in [0, 1] to linear light. |
|
Encode linear light in [0, 1] as gamma-encoded sRGB. |
|
Convert linear sRGB to CIELAB (D65). |
|
Convert CIELAB (D65) to linear sRGB, without clipping. |
|
Convert sRGB colours to CIELAB (D65). |
|
Convert CIELAB to cylindrical CIELCh with hue in degrees. |
|
Convert CIELCh (hue in degrees) to CIELAB. |
|
Convert sRGB colours to CIELCh. |
|
Convert CIELCh to sRGB, reducing chroma at fixed L* and hue if needed. |
|
CIEDE2000 colour difference with k_L = k_C = k_H = 1. |
|
Simulate how sRGB colours look with a colour-vision deficiency. |
|
WCAG relative luminance (the Y of linear sRGB). |
|
WCAG 2.1 contrast ratio between two colours, from 1 to 21. |
|
Return |
|
The candidate whose smallest WCAG contrast with |
|
The entries of |
|
Matrix of CIEDE2000 differences between colours seen with |
|
Smallest CIEDE2000 difference between any two of |
|
Order named colours for line plots on |
|
Give each element of |
|
Fritsch-Carlson slopes for a monotone cubic Hermite interpolant. |
|
Return |
|
Sample |
|
Unwrap hues so that each is at least the one before it. |
|
sRGB table of the sequential Ruhi map, dark wine to sunshine. |
|
sRGB table of the diverging Ruhi map, teal through grey to dark wine. |
|
Register |
|
Lightness and CIEDE2000 step profile of a colour map for each vision type. |
|
Check a categorical palette against WCAG and simulated CVD. |
Data#
API#
- 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._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.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 reachesratio.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
neighboursis 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
cmapthat reachratioagainstbackground.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
referenceone ofcolors.referenceholds the usual colour of each element, such as its Jmol colour. The anchors takecolorsone 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(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
pathsat shared lightness values set by arc-lengthpositions.positionsrun from 0 atl0to 1 atl1and 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._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_seqandruhi_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.
- lightness_monotonic(kind: str = 'normal') bool#
True if L* rises or falls strictly along the whole map.
- 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.
- lch: numpy.ndarray#
None
- 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 withkind;reference_min_delta_eis the same number forreference(Okabe-Ito by default).Added in version 1.14.0.