erlab.interactive.colors

Functions for manipulating colors in Qt.

Functions

color_to_QColor(c[, alpha])

Convert a matplotlib color to a PySide6.QtGui.QColor.

is_dark_mode()

Check if the system is in dark mode.

matplotlib_colormap_name(name)

Return the Matplotlib colormap name matching a display name.

pg_colormap_from_name(name[, skipCache])

Get a pyqtgraph.ColorMap from its name.

pg_colormap_names([source, exclude_local])

Get all valid pyqtgraph colormap names.

pg_colormap_powernorm(cmap, gamma[, ...])

Apply a power-law intensity mapping to a pyqtgraph colormap.

pg_colormap_to_QPixmap(cmap[, w, h, skipCache])

Convert a pyqtgraph.ColorMap to a w-by-h QPixmap thumbnail.

Classes

BetterColorBarItem([parent, image, ...])

Interactive colorbar for one or more BetterImageItem objects.

BetterImageItem([image])

pyqtgraph.ImageItem with improved colormap support.

ColorCycleDialog(colors, *[, parent, ...])

Dialog for selecting a color cycle.

ColorMapComboBox(*args, **kwargs)

Colormap selector with lazy thumbnails and optional external colormaps.

ColorMapGammaWidget([parent, value, ...])

Slider and spinbox for adjusting colormap gamma.

class erlab.interactive.colors.BetterColorBarItem(parent=None, image=None, autoLevels=False, limits=None, pen='c', hoverPen='m', hoverBrush='#FFFFFF33', *, show_colormap_edit_menu=True, _defer_context_menu_setup=False, **kargs)[source]

Bases: PlotItem

Interactive colorbar for one or more BetterImageItem objects.

Parameters:
  • parent (QWidget | None, default: None) – Parent widget.

  • image (Iterable[BetterImageItem] | BetterImageItem | None, default: None) – Image item or items whose colormap and intensity limits the colorbar controls.

  • autoLevels (bool, default: False) – Automatically update the color limits when image data changes.

  • limits (tuple[float, float] | None, default: None) – Fixed lower and upper bounds for the colorbar scale. If omitted, bounds are estimated from the primary image.

  • pen (QPen | str, default: 'c') – pyqtgraph style values for the movable limit region.

  • hoverPen (QPen | str, default: 'm') – pyqtgraph style values for the movable limit region.

  • hoverBrush (QBrush | str, default: '#FFFFFF33') – pyqtgraph style values for the movable limit region.

  • show_colormap_edit_menu (bool, default: True) – Include colormap controls in the context menu.

  • **kargs – Additional keyword arguments passed to pyqtgraph.PlotItem.

Notes

The colorbar keeps weak references to its image items. Changing its region updates the displayed intensity limits of all live associated images.

getMenu()[source]
getContextMenus(event)[source]
property images
property primary_image
property levels: Sequence[float]
property limits: tuple[float, float]
set_width(width)[source]
set_dimensions(horiz_pad=None, vert_pad=None, font_size=11.0)[source]
level_change()[source]
level_change_fin()[source]
spanRegion()[source]
setSpanRegion(levels)[source]
setLimits(limits)[source]
addImage(image)[source]
removeImage(image)[source]
setImageItem(image, insert_in=None)[source]
property colormap_properties: dict[str, Any] | None

Return the semantic ERLab colormap settings for the primary image.

set_colormap(cmap, gamma, reverse=False, high_contrast=False, zero_centered=False)[source]

Apply one ERLab colormap configuration to every controlled image.

set_pg_colormap(cmap)[source]

Apply a raw pyqtgraph colormap to every controlled image.

image_level_changed()[source]
image_changed()[source]
reset_levels()[source]
setAutoLevels(value)[source]
color_changed()[source]
limit_changed()[source]
mouseDragEvent(ev)[source]
class erlab.interactive.colors.BetterImageItem(image=None, **kwargs)[source]

Bases: ImageItem

pyqtgraph.ImageItem with improved colormap support.

Parameters:
Signals:
  • sigColorChanged()

  • sigLimitChanged(float, float)

set_colormap(cmap, gamma, reverse=False, high_contrast=False, zero_centered=False, update=True)[source]
set_pg_colormap(cmap, update=True)[source]
class erlab.interactive.colors.ColorCycleDialog(colors, *, parent=None, preview_cursors=False, opacity_values=(0.95, 0.75, 0.15, 0.35), default_colors=None)[source]

Bases: QDialog

Dialog for selecting a color cycle.

This dialog takes a list of colors and allows the user to edit each color in the cycle, or overwrite the entire cycle by sampling from a colormap.

Parameters:
  • colors (Iterable[QColor]) – An iterable of colors to use as the initial color cycle.

  • parent (QWidget | None, default: None) – The parent widget for the dialog.

  • preview_cursors (bool, default: False) – If True, the preview will include cursor lines and spans with the selected colors.

  • opacity_values (tuple[float, float, float, float], default: (0.95, 0.75, 0.15, 0.35)) – A tuple of four float values representing the opacity for the cursor line, cursor line hover, span background, and span edge, respectively. Each value should be between 0 and 1, where 1 is fully opaque and 0 is fully transparent.

  • default_colors (Iterable[QColor] | None, default: None) – An iterable of default colors to return to when the user clicks “Restore Defaults”. If None, the restore defaults button will not be shown.

property colors: tuple[QColor]

Return the current color cycle.

setup_ui()[source]
set_from_cmap()[source]

Set the color cycle from a colormap.

reset()[source]
restore_defaults()[source]

Restore the default colors if provided.

accept()[source]

Accept the dialog and emit the new colors.

class erlab.interactive.colors.ColorMapComboBox(*args, **kwargs)[source]

Bases: QComboBox

Colormap selector with lazy thumbnails and optional external colormaps.

The initial list contains Matplotlib colormaps and is populated when the widget is first shown. The context menu can load colormaps from the optional libraries that ERLabPy detects.

Parameters:
  • *args – Arguments passed to qtpy.QtWidgets.QComboBox.

  • **kwargs – Arguments passed to qtpy.QtWidgets.QComboBox.

ensure_populated()[source]

Populate the colormap list before the combo box is shown.

clear()[source]
showEvent(event)[source]
closeEvent(event)[source]
load_thumbnail(index)[source]
load_all()[source]
showPopup()[source]
setPopupMinimumWidthForItems()[source]
hidePopup()[source]
setDefaultCmap(cmap)[source]
resetCmap()[source]
setCurrentIndex(index)[source]

Set the current index and ensure its thumbnail is available.

current_matplotlib_name()[source]

Return the selected colormap name as registered by Matplotlib.

setCurrentText(text)[source]

Set the current text of the combobox.

class erlab.interactive.colors.ColorMapGammaWidget(parent=None, value=1.0, slider_cls=None, spin_cls=None)[source]

Bases: QWidget

Slider and spinbox for adjusting colormap gamma.

Signals:

valueChanged(float)

value()[source]
setValue(value)[source]
spin_changed(value)[source]
slider_changed(value)[source]
gamma_scale(y)[source]
gamma_scale_inv(x)[source]
erlab.interactive.colors.color_to_QColor(c, alpha=None)[source]

Convert a matplotlib color to a PySide6.QtGui.QColor.

Parameters:
Returns:

PySide6.QtGui.QColor

Return type:

QColor

erlab.interactive.colors.is_dark_mode()[source]

Check if the system is in dark mode.

If a QApplication is not running, this will always return False.

erlab.interactive.colors.matplotlib_colormap_name(name)[source]

Return the Matplotlib colormap name matching a display name.

erlab.interactive.colors.pg_colormap_from_name(name, skipCache=True)[source]

Get a pyqtgraph.ColorMap from its name.

Parameters:
Returns:

pyqtgraph.ColorMap

Return type:

ColorMap

erlab.interactive.colors.pg_colormap_names(source='all', exclude_local=False)[source]

Get all valid pyqtgraph colormap names.

Parameters:
  • source (Literal['local', 'all', 'matplotlib'], default: 'all') –

    • “local”: only built-in/pyqtgraph colormaps

    • ”matplotlib”: only matplotlib colormaps

    • ”all”: local + colorcet + matplotlib

  • exclude_local (bool, default: False) – If True, drop local colormaps from the final list.

Returns:

list of str – Ordered, de-duplicated list of colormap names.

Return type:

list[str]

erlab.interactive.colors.pg_colormap_powernorm(cmap, gamma, reverse=False, high_contrast=False, zero_centered=False, N=65536)[source]

Apply a power-law intensity mapping to a pyqtgraph colormap.

Parameters:
  • cmap (str | ColorMap) – Colormap name or pyqtgraph.ColorMap. A name is resolved with pg_colormap_from_name() without using the pyqtgraph cache. A supplied ColorMap is modified in place.

  • gamma (float) – Dimensionless power-law exponent. With the default contrast mode, normalized positions are mapped as x**gamma.

  • reverse (bool, default: False) – Reverse the colormap before the power-law mapping.

  • high_contrast (bool, default: False) – Use 1 - (1 - x)**(1 / gamma) instead of x**gamma.

  • zero_centered (bool, default: False) – Apply the mapping separately below and above the midpoint so that 0.5 remains fixed.

  • N (int, default: 65536) – Number of uniformly spaced colormap positions in the returned map.

Returns:

pyqtgraph.ColorMap – Colormap with remapped floating-point colors and positions. A supplied ColorMap is the same object after modification. The result records gamma, reverse, high_contrast, and zero_centered in _erlab_attrs.

Return type:

ColorMap

erlab.interactive.colors.pg_colormap_to_QPixmap(cmap, w=64, h=16, skipCache=True)[source]

Convert a pyqtgraph.ColorMap to a w-by-h QPixmap thumbnail.

Parameters:
  • cmap (str | ColorMap) – The colormap.

  • w (int, default: 64) – Specifies the dimension of the pixmap.

  • h (int, default: 16) – Specifies the dimension of the pixmap.

  • skipCache (bool, optional) – Whether to skip the pyqtgraph colormap cache when resolving a string colormap, by default True. Pass False to bypass the thumbnail cache and request the pyqtgraph-cached colormap.

Returns:

PySide6.QtGui.QPixmap

Return type:

QPixmap