diff options
author | Ariya Hidayat <ariya.hidayat@nokia.com> | 2009-09-14 10:24:33 (GMT) |
---|---|---|
committer | Ariya Hidayat <ariya.hidayat@nokia.com> | 2009-09-14 13:10:13 (GMT) |
commit | 97594601ca84ae11f30bb35e6cbc7e3f70e0624d (patch) | |
tree | 88a755b717059517cd8d5e6d1fa7499ed3c221de /src | |
parent | f272d891a1fb622ced7a92d426099996f7890945 (diff) | |
download | Qt-97594601ca84ae11f30bb35e6cbc7e3f70e0624d.zip Qt-97594601ca84ae11f30bb35e6cbc7e3f70e0624d.tar.gz Qt-97594601ca84ae11f30bb35e6cbc7e3f70e0624d.tar.bz2 |
Add strength factor to the colorize filter.
To allow fading between the original and the colorized version of the
pixmaps, a new strength factor is introduced, 0.0 means the filter
has no effect at all, 1.0 means full colorization.
Still missing is the non-raster implementation.
Autotest: included
Reviewed-by: Bjørn Erik Nilsen
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/image/qpixmapfilter.cpp | 51 | ||||
-rw-r--r-- | src/gui/image/qpixmapfilter_p.h | 3 |
2 files changed, 53 insertions, 1 deletions
diff --git a/src/gui/image/qpixmapfilter.cpp b/src/gui/image/qpixmapfilter.cpp index 15704ba..4fa2e6c 100644 --- a/src/gui/image/qpixmapfilter.cpp +++ b/src/gui/image/qpixmapfilter.cpp @@ -759,6 +759,10 @@ class QPixmapColorizeFilterPrivate : public QPixmapFilterPrivate Q_DECLARE_PUBLIC(QPixmapColorizeFilter) public: QColor color; + qreal strength; + quint32 opaque : 1; + quint32 alphaBlend : 1; + quint32 padding : 30; }; /*! @@ -771,7 +775,11 @@ public: QPixmapColorizeFilter::QPixmapColorizeFilter(QObject *parent) : QPixmapFilter(*new QPixmapColorizeFilterPrivate, ColorizeFilter, parent) { - d_func()->color = QColor(0, 0, 192); + Q_D(QPixmapColorizeFilter); + d->color = QColor(0, 0, 192); + d->strength = qreal(1); + d->opaque = true; + d->alphaBlend = false; } /*! @@ -797,6 +805,31 @@ void QPixmapColorizeFilter::setColor(const QColor &color) } /*! + Gets the strength of the colorize filter, 1.0 means full colorized while + 0.0 equals to no filtering at all. + + \internal +*/ +qreal QPixmapColorizeFilter::strength() const +{ + Q_D(const QPixmapColorizeFilter); + return d->strength; +} + +/*! + Sets the strength of the colorize filter to \a strength. + + \internal +*/ +void QPixmapColorizeFilter::setStrength(qreal strength) +{ + Q_D(QPixmapColorizeFilter); + d->strength = qBound(qreal(0), strength, qreal(1)); + d->opaque = !qFuzzyIsNull(d->strength); + d->alphaBlend = !qFuzzyIsNull(d->strength - 1); +} + +/*! \internal */ void QPixmapColorizeFilter::draw(QPainter *painter, const QPointF &dest, const QPixmap &src, const QRectF &srcRect) const @@ -807,6 +840,7 @@ void QPixmapColorizeFilter::draw(QPainter *painter, const QPointF &dest, const Q QPixmapColorizeFilter *colorizeFilter = static_cast<QPixmapColorizeFilter*>(filter); if (colorizeFilter) { colorizeFilter->setColor(d->color); + colorizeFilter->setStrength(d->strength); colorizeFilter->draw(painter, dest, src, srcRect); delete colorizeFilter; return; @@ -814,6 +848,11 @@ void QPixmapColorizeFilter::draw(QPainter *painter, const QPointF &dest, const Q // falling back to raster implementation + if (!d->opaque) { + painter->drawPixmap(dest, src, srcRect); + return; + } + QImage srcImage; QImage destImage; @@ -836,6 +875,16 @@ void QPixmapColorizeFilter::draw(QPainter *painter, const QPointF &dest, const Q destPainter.fillRect(srcImage.rect(), d->color); destPainter.end(); + if (d->alphaBlend) { + // alpha blending srcImage and destImage + QImage buffer = srcImage; + QPainter bufPainter(&buffer); + bufPainter.setOpacity(d->strength); + bufPainter.drawImage(0, 0, destImage); + bufPainter.end(); + destImage = buffer; + } + if (srcImage.hasAlphaChannel()) destImage.setAlphaChannel(srcImage.alphaChannel()); diff --git a/src/gui/image/qpixmapfilter_p.h b/src/gui/image/qpixmapfilter_p.h index 4cbf7a3..2565110 100644 --- a/src/gui/image/qpixmapfilter_p.h +++ b/src/gui/image/qpixmapfilter_p.h @@ -155,6 +155,9 @@ public: void setColor(const QColor& color); QColor color() const; + void setStrength(qreal strength); + qreal strength() const; + void draw(QPainter *painter, const QPointF &dest, const QPixmap &src, const QRectF &srcRect = QRectF()) const; }; |