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
QPainterinstance or aQPaintDevice(e.g. a QWidget or QPixmap) onto which to paint the colormap. -
cmap(Colormap | Any) –cmap.Colormapinstance, or anything that can be converted to one (such as a string name of a colormap in thecmapcatalog). https://cmap-docs.readthedocs.io/en/latest/colormaps/#colormaplike-objects -
rect(QRect | QRectF | None, default:None) –A rect onto which to draw. If
None, thepainter.viewport()will be used. by defaultNone -
border_color(QColor | str | None, default:None) –If not
None, a border of colorborder_colorand widthborder_widthis included around the edge, by default None. -
border_width(int, default:1) –The width of the border to draw (provided
border_coloris notNone), 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.