def gradient_darker(grad, factor): # type: (QGradient, float) -> QGradient """Return a copy of the QGradient darkened by factor. .. note:: Only QLinearGradeint and QRadialGradient are supported. """ if type(grad) is QGradient: if grad.type() == QGradient.LinearGradient: grad = sip.cast(grad, QLinearGradient) elif grad.type() == QGradient.RadialGradient: grad = sip.cast(grad, QRadialGradient) if isinstance(grad, QLinearGradient): new_grad = QLinearGradient(grad.start(), grad.finalStop()) elif isinstance(grad, QRadialGradient): new_grad = QRadialGradient(grad.center(), grad.radius(), grad.focalPoint()) else: raise TypeError new_grad.setCoordinateMode(grad.coordinateMode()) for pos, color in grad.stops(): new_grad.setColorAt(pos, color.darker(factor)) return new_grad
def radial_gradient(color, color_light=50): """ radial_gradient(QColor, QColor) radial_gradient(QColor, int) Return a radial gradient. `color_light` can be a QColor or an int. In the later case the light color is derived from `color` using `saturated(color, color_light)`. """ if not isinstance(color_light, QColor): color_light = saturated(color, color_light) gradient = QRadialGradient(0.5, 0.5, 0.5) gradient.setColorAt(0.0, color_light) gradient.setColorAt(0.5, color_light) gradient.setColorAt(1.0, color) gradient.setCoordinateMode(QRadialGradient.ObjectBoundingMode) return gradient
def innerGlowBackgroundPixmap(color, size, radius=5): """ Draws radial gradient pixmap, then uses that to draw a rounded-corner gradient rectangle pixmap. Args: color (QColor): used as outer color (lightness 245 used for inner) size (QSize): size of output pixmap radius (int): radius of inner glow rounded corners """ key = "InnerGlowBackground " + \ color.name() + " " + \ str(radius) bg = QPixmapCache.find(key) if bg: return bg # set background colors for gradient color = color.toHsl() light_color = color.fromHsl(color.hslHue(), color.hslSaturation(), 245) dark_color = color # initialize radial gradient center = QPoint(radius, radius) pixRect = QRect(0, 0, radius * 2, radius * 2) gradientPixmap = QPixmap(radius * 2, radius * 2) gradientPixmap.fill(dark_color) # draw radial gradient pixmap pixPainter = QPainter(gradientPixmap) pixPainter.setPen(Qt.NoPen) gradient = QRadialGradient(center, radius - 1) gradient.setColorAt(0, light_color) gradient.setColorAt(1, dark_color) pixPainter.setBrush(gradient) pixPainter.drawRect(pixRect) pixPainter.end() # set tl and br to the gradient's square-shaped rect tl = QPoint(0, 0) br = QPoint(size.width(), size.height()) # fragments of radial gradient pixmap to create rounded gradient outline rectangle frags = [ # top-left corner QPainter.PixmapFragment.create( QPointF(tl.x() + radius / 2, tl.y() + radius / 2), QRectF(0, 0, radius, radius) ), # top-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(tl.x() + (br.x() - tl.x()) / 2, tl.y() + radius / 2), QRectF(radius, 0, 1, radius), scaleX=(br.x() - tl.x() - 2 * radius) ), # top-right corner QPainter.PixmapFragment.create( QPointF(br.x() - radius / 2, tl.y() + radius / 2), QRectF(radius, 0, radius, radius) ), # left-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(tl.x() + radius / 2, tl.y() + (br.y() - tl.y()) / 2), QRectF(0, radius, radius, 1), scaleY=(br.y() - tl.y() - 2 * radius) ), # mid solid QPainter.PixmapFragment.create( QPointF(tl.x() + (br.x() - tl.x()) / 2, tl.y() + (br.y() - tl.y()) / 2), QRectF(radius, radius, 1, 1), scaleX=(br.x() - tl.x() - 2 * radius), scaleY=(br.y() - tl.y() - 2 * radius) ), # right-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(br.x() - radius / 2, tl.y() + (br.y() - tl.y()) / 2), QRectF(radius, radius, radius, 1), scaleY=(br.y() - tl.y() - 2 * radius) ), # bottom-left corner QPainter.PixmapFragment.create( QPointF(tl.x() + radius / 2, br.y() - radius / 2), QRectF(0, radius, radius, radius) ), # bottom-mid 'linear gradient' QPainter.PixmapFragment.create( QPointF(tl.x() + (br.x() - tl.x()) / 2, br.y() - radius / 2), QRectF(radius, radius, 1, radius), scaleX=(br.x() - tl.x() - 2 * radius) ), # bottom-right corner QPainter.PixmapFragment.create( QPointF(br.x() - radius / 2, br.y() - radius / 2), QRectF(radius, radius, radius, radius) ), ] # draw icon background to pixmap outPix = QPixmap(size.width(), size.height()) outPainter = QPainter(outPix) outPainter.setPen(Qt.NoPen) outPainter.drawPixmapFragments(frags, gradientPixmap, QPainter.PixmapFragmentHints(QPainter.OpaqueHint)) outPainter.end() QPixmapCache.insert(key, outPix) return outPix