spacr.figure_style¶
Resolve spaCR figure styles for display and export.
General settings define the shared appearance of every figure. Per-graph settings override only the values needed by a specific graph type. The style tables and export-colour helpers can be used without starting Qt or importing Matplotlib at module import time.
Classes¶
Describe how a figure should be rendered during export. |
Functions¶
|
Apply a resolved spaCR style to Matplotlib. |
|
Resolve the color of a grid, spine, or box element. |
|
Calculate the WCAG contrast ratio between two colours. |
|
Choose an export replacement colour for a figure element. |
|
Return the configured figure export mode. |
|
Format a warning for low-contrast data colours. |
|
Find data colours with insufficient contrast against a background. |
|
Determine whether a colour meets a contrast threshold. |
|
Calculate figure dimensions for a named page shape. |
|
Return a palette as hexadecimal colour strings. |
|
Convert spaCR style settings to Matplotlib |
|
Calculate the WCAG relative luminance of a colour. |
|
Resolve the effective style for a graph type. |
|
Resolve the background and figure-element colours for export. |
|
Return the allowed values for a closed-choice style setting. |
|
Return figure-element colours for the active application theme. |
|
Convert a colour specification to RGB components. |
Module Contents¶
- class spacr.figure_style.SavedFigureAppearance[source]¶
Bases:
NamedTupleDescribe how a figure should be rendered during export.
- Parameters:
mode (str) – Active mode from
SAVE_MODES.ground (str or None) – Export background, or
Noneto retain or remove the current background according tomode.ink (str or None) – Replacement colour for low-contrast non-data elements, or
Noneto preserve their colours.grid (str or None) – Replacement colour for low-contrast gridlines.
transparent (bool) – Whether the figure writer should request a transparent background.
flip (bool) – Whether low-contrast figure elements may be recoloured.
- spacr.figure_style.apply(kind: str | None = None, general: Mapping[str, Any] | None = None, overrides: Mapping[str, Any] | None = None) dict[source]¶
Apply a resolved spaCR style to Matplotlib.
Matplotlib parameters and the colour cycle are updated when their optional dependencies are available. Styling failures are ignored so that figure generation can continue.
- Parameters:
kind (str, optional) – Graph type from
GRAPH_KINDS.general (mapping of str to Any, optional) – User-defined general settings.
overrides (mapping of str to mapping, optional) – User-defined settings keyed by graph type.
- Returns:
dict – Fully resolved spaCR style, including settings that have no Matplotlib
rcParamequivalent.
- spacr.figure_style.chrome_of(style, element: str = 'grid') str[source]¶
Resolve the color of a grid, spine, or box element.
- Parameters:
style – figure-style mapping containing chrome color overrides.
A nonempty element-specific value takes precedence over
chrome_colour. An empty result indicates that the resolved figure ink color should be used.
- spacr.figure_style.contrast_ratio(colour, other) float | None[source]¶
Calculate the WCAG contrast ratio between two colours.
- Parameters:
colour (Any) – First colour specification.
other (Any) – Second colour specification.
- Returns:
float or None – Contrast ratio from
1.0to21.0.Noneis returned when either colour is transparent or cannot be parsed.
- spacr.figure_style.export_colour(current, kind: str, look=None) str | None[source]¶
Choose an export replacement colour for a figure element.
Data colours are always preserved. A dark background may be replaced in print mode; gridlines and other figure elements are replaced only when the active export mode allows it and their contrast is below the configured threshold.
- Parameters:
current (Any) – Current artist colour. Transparent and unrecognized values are left unchanged.
kind ({'ground', 'grid', 'chrome', 'data'}) – Role of the artist in the figure.
look (SavedFigureAppearance, optional) – Export appearance. If
None, usesaved_figure_appearance().
- Returns:
str or None – Replacement colour, or
Nonewhen the current colour should be preserved.
Examples
>>> look = saved_figure_appearance("print") >>> export_colour("#FFFFFF", "chrome", look) '#222222' >>> export_colour("#222222", "chrome", look) is None True >>> export_colour("#FFFFFF", "data", look) is None True
- spacr.figure_style.figure_save_mode() str[source]¶
Return the configured figure export mode.
A valid
SPACR_FIGURE_SAVE_MODEvalue takes precedence over the Qt preference store, which allows command-line and notebook workflows to choose a mode without starting the GUI. Missing or invalid environment values fall through to the stored preference; if no valid preference is available, the mode is'print'.- Returns:
{‘print’, ‘screen’, ‘transparent’} – Active export mode.
- spacr.figure_style.illegible_colour_warning(names) str[source]¶
Format a warning for low-contrast data colours.
- Parameters:
names (iterable of str) – Colour names returned by
illegible_colours().- Returns:
str – Warning text, or an empty string when
namesis empty.
- spacr.figure_style.illegible_colours(colours, ground=PRINT_GROUND, floor: float | None = None) list[source]¶
Find data colours with insufficient contrast against a background.
- Parameters:
colours (iterable of colour specifications) – Colours accepted by
to_rgb(). Unrecognized values and numeric RGBA entries with alpha below0.5are ignored.ground (Any, default=PRINT_GROUND) – Background colour used for the contrast calculation.
floor (float, optional) – Minimum accepted contrast ratio. If
None, useDATA_CONTRAST_FLOOR.
- Returns:
list of str – Sorted, deduplicated colours in
#RRGGBBformat.
Notes
This function reports low-contrast data colours but does not replace them, because colour may encode a result or category.
- spacr.figure_style.is_legible_on(colour, ground, floor: float = CHROME_CONTRAST_FLOOR) bool[source]¶
Determine whether a colour meets a contrast threshold.
- Parameters:
colour (Any) – Foreground colour specification.
ground (Any) – Background colour specification.
floor (float, default=CHROME_CONTRAST_FLOOR) – Minimum accepted WCAG contrast ratio.
- Returns:
bool –
Truewhen the ratio meetsfloor. Unrecognized and transparent colours are treated as legible so they are not recoloured.
- spacr.figure_style.page_size(shape: str, width: float) tuple[source]¶
Calculate figure dimensions for a named page shape.
- spacr.figure_style.palette_colours(name: str | None) list[source]¶
Return a palette as hexadecimal colour strings.
Named seaborn palettes are used when they resolve successfully; otherwise, spaCR’s built-in palette is used. An empty name, or failure to load either source, returns an empty list so callers can preserve the current colour cycle.
- Parameters:
name – Palette name from
STYLE_CHOICES["palette"], orNone.- Returns:
The resolved sequence of hexadecimal colours.
- spacr.figure_style.rc_params(style: Mapping[str, Any]) dict[source]¶
Convert spaCR style settings to Matplotlib
rcParams.Settings without an equivalent Matplotlib parameter, such as
per_rowandlabel_top_n, are omitted.- Parameters:
style (mapping of str to Any) – Resolved or partial spaCR figure style.
- Returns:
dict – Matplotlib parameter names and values derived from
style.
- spacr.figure_style.relative_luminance(colour) float | None[source]¶
Calculate the WCAG relative luminance of a colour.
- Parameters:
colour (Any) – Colour specification accepted by
to_rgb().- Returns:
float or None – Relative luminance in the interval
[0, 1], orNonewhen the colour is transparent or cannot be parsed.
- spacr.figure_style.resolve(kind: str | None = None, general: Mapping[str, Any] | None = None, overrides: Mapping[str, Any] | None = None) dict[source]¶
Resolve the effective style for a graph type.
Settings are merged in this order: general defaults, user-defined general settings, graph-type defaults, and user-defined graph-type overrides. Entries whose value is
Nonedo not replace an earlier value.- Parameters:
kind (str, optional) – Graph type from
GRAPH_KINDS. IfNone, only the general layers are applied.general (mapping of str to Any, optional) – User-defined general settings.
overrides (mapping of str to mapping, optional) – User-defined settings keyed by graph type.
- Returns:
dict – Merged style settings. Unknown graph types inherit only the general layers and any matching entry in
overrides.
- spacr.figure_style.saved_figure_appearance(mode: str | None = None) SavedFigureAppearance[source]¶
Resolve the background and figure-element colours for export.
- Parameters:
mode ({'print', 'screen', 'transparent'}, optional) – Export mode. If
None, usefigure_save_mode(). Invalid values fall back to'print'.- Returns:
SavedFigureAppearance – Rendering instructions shared by the Matplotlib and pyqtgraph export paths. Data colours are outside this appearance and remain unchanged.
- spacr.figure_style.style_choices(name: str) tuple[source]¶
Return the allowed values for a closed-choice style setting.
- Parameters:
name (str) – Style key from
GENERAL_DEFAULTSorGRAPH_DEFAULTS.- Returns:
tuple – Allowed values. An empty tuple indicates that the setting is free-form or unknown.
- spacr.figure_style.theme_ink() Tuple[str, str][source]¶
Return figure-element colours for the active application theme.
- Returns:
ink (str) – Colour for labels, ticks, spines, and annotations.
grid (str) – Colour for gridlines.
Notes
Light themes use
PRINT_INKandPRINT_GRID. Dark themes useDARK_INKandDARK_GRID. The light-theme pair is returned when the Qt preference store is unavailable.
- spacr.figure_style.to_rgb(colour) tuple | None[source]¶
Convert a colour specification to RGB components.
- Parameters:
colour (Any) – Hexadecimal, named, RGB, or RGBA colour specification.
- Returns:
tuple of float or None – Three RGB components. Hexadecimal and Matplotlib colour inputs are normalized to the interval
[0, 1]; numeric sequences are returned as floats.Noneis returned for transparent or unrecognized colours.