Skip to content

Colormap utilities

The building blocks of the colormap widgets (QColormapComboBox, CmapCatalogComboBox, and QColormapLineEdit), for drawing cmap colormaps elsewhere. They need the cmap extra (pip install "superqt[cmap]").

draw_colormap

draw_colormap(
    painter_or_device: QPainter | QPaintDevice,
    cmap: Colormap | ColorStopsLike,
    rect: QRect | QRectF | None = None,
    border_color: QColor | str | None = None,
    border_width: int = 1,
    lighter: int = 100,
    checkerboard_size: int = 4,
) -> None

Draw a colormap onto a QPainter or QPaintDevice.

Parameters:

  • painter_or_device (QPainter | QPaintDevice) –

    A QPainter instance or a QPaintDevice (e.g. a QWidget or QPixmap) onto which to paint the colormap.

  • cmap (Colormap | Any) –

    cmap.Colormap instance, or anything that can be converted to one (such as a string name of a colormap in the cmap catalog). https://cmap-docs.readthedocs.io/en/latest/colormaps/#colormaplike-objects

  • rect (QRect | QRectF | None, default: None ) –

    A rect onto which to draw. If None, the painter.viewport() will be used. by default None

  • border_color (QColor | str | None, default: None ) –

    If not None, a border of color border_color and width border_width is included around the edge, by default None.

  • border_width (int, default: 1 ) –

    The width of the border to draw (provided border_color is not None), by default 2

  • lighter (int, default: 100 ) –

    Percentage by which to lighten (or darken) the colors. Greater than 100 lightens, less than 100 darkens, by default 100 (i.e. no change).

  • checkerboard_size (bool, default: 4 ) –

    Size (in pixels) of the checkerboard pattern to draw, by default 5. If 0, no checkerboard is drawn.

Examples:

from qtpy.QtGui import QPixmap
from qtpy.QtWidgets import QWidget
from superqt.utils import draw_colormap

viridis = "viridis"  # or cmap.Colormap('viridis')


class W(QWidget):
    def paintEvent(self, event) -> None:
        draw_colormap(self, viridis, event.rect())


# or draw onto a QPixmap
pm = QPixmap(200, 200)
draw_colormap(pm, viridis)

QColormapItemDelegate

QColormapItemDelegate(
    parent: QObject | None = None,
    *,
    item_size: QSize = DEFAULT_SIZE,
    fractional_colormap_width: float = 1,
    padding: int = 1,
    checkerboard_size: int = 4,
)

Delegate that draws colormaps into a QAbstractItemView item.

Parameters:

  • parent (QObject, default: None ) –

    The parent object.

  • item_size (QSize, default: DEFAULT_SIZE ) –

    The size hint for each item, by default QSize(80, 22).

  • fractional_colormap_width (float, default: 1 ) –

    The fraction of the widget width to use for the colormap swatch. If the colormap is full width (greater than 0.75), the swatch will be drawn behind the text. Otherwise, the swatch will be drawn to the left of the text. Default is 0.33.

  • padding (int, default: 1 ) –

    The padding (in pixels) around the edge of the item, by default 1.

  • checkerboard_size (int, default: 4 ) –

    Size (in pixels) of the checkerboard pattern to draw behind colormaps with transparency, by default 4. If 0, no checkerboard is drawn.