def cmap2pixmap(cmap, steps=50): """ Convert a maplotlib colormap into a QPixmap Parameters ---------- cmap : `~matplotlib.colors.Colormap` The colormap to use steps : int The number of color steps in the output. Default=50 Returns ------- pixmap : ``QPixmap`` The QPixmap instance """ sm = cm.ScalarMappable(cmap=cmap) sm.norm.vmin = 0.0 sm.norm.vmax = 1.0 inds = np.linspace(0, 1, steps) rgbas = sm.to_rgba(inds) rgbas = [ QtGui.QColor(int(r * 255), int(g * 255), int(b * 255), int(a * 255)).rgba() for r, g, b, a in rgbas ] im = QtGui.QImage(steps, 1, QtGui.QImage.Format_Indexed8) im.setColorTable(rgbas) for i in range(steps): im.setPixel(i, 0, i) im = im.scaled(100, 100) pm = QtGui.QPixmap.fromImage(im) return pm
def tint_pixmap(bm, color): """ Re-color a monochrome pixmap object using `color` Parameters ---------- bm : ``QBitmap`` The Pixmap object color : ``QColor`` The Qt color Returns ------- pixmap : ``QPixmap`` The new pixmap """ if bm.depth() != 1: raise TypeError("Input pixmap must have a depth of 1: %i" % bm.depth()) image = bm.toImage() image.setColor(1, color.rgba()) image.setColor(0, QtGui.QColor(0, 0, 0, 0).rgba()) result = QtGui.QPixmap.fromImage(image) return result
def paintEvent(self, event): super(GlueMdiArea, self).paintEvent(event) painter = QtGui.QPainter(self.viewport()) painter.setPen(QtGui.QColor(210, 210, 210)) font = painter.font() font.setPointSize(48) font.setWeight(font.Black) painter.setFont(font) rect = self.contentsRect() painter.drawText(rect, Qt.AlignHCenter | Qt.AlignVCenter, "Drag Data To Plot")
def __init__(self, application, parent=None): """ :param application: The Glue application to which this is attached :type application: :class:`~glue.app.qt.application.GlueApplication` """ super(GlueMdiArea, self).__init__(parent) self._application = application self.setAcceptDrops(True) self.setAttribute(Qt.WA_DeleteOnClose) self.setBackground(QtGui.QBrush(QtGui.QColor(250, 250, 250))) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded) self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
def mpl_to_qt4_color(color, alpha=1.0): """ Convert a matplotlib color stirng into a Qt QColor object Parameters ---------- color : str A color specification that matplotlib understands alpha : float Optional opacity. Float in range [0,1] Returns ------- qcolor : ``QColor`` A QColor object representing the converted color """ if color in [None, 'none', 'None']: return QtGui.QColor(0, 0, 0, 0) cc = ColorConverter() r, g, b = cc.to_rgb(color) alpha = max(0, min(255, int(256 * alpha))) return QtGui.QColor(r * 255, g * 255, b * 255, alpha)
def cmap2pixmap(cmap, steps=50): """Convert a Ginga colormap into a QtGui.QPixmap :param cmap: The colormap to use :type cmap: Ginga colormap instance (e.g. ginga.cmap.get_cmap('gray')) :param steps: The number of color steps in the output. Default=50 :type steps: int :rtype: QtGui.QPixmap """ inds = np.linspace(0, 1, steps) n = len(cmap.clst) - 1 tups = [cmap.clst[int(x * n)] for x in inds] rgbas = [ QtGui.QColor(int(r * 255), int(g * 255), int(b * 255), 255).rgba() for r, g, b in tups ] im = QtGui.QImage(steps, 1, QtGui.QImage.Format_Indexed8) im.setColorTable(rgbas) for i in range(steps): im.setPixel(i, 0, i) im = im.scaled(128, 32) pm = QtGui.QPixmap.fromImage(im) return pm
def _set_palette(self): p = self.palette() c = QtGui.QColor(240, 240, 240) p.setColor(QtGui.QPalette.Highlight, c) p.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor(Qt.black)) self.setPalette(p)
def test_colors(): assert qt4_to_mpl_color(QtGui.QColor(255, 0, 0)) == '#ff0000' assert qt4_to_mpl_color(QtGui.QColor(255, 255, 255)) == '#ffffff'