Alexey B.
Alexey B.

Reputation: 1156

Best way to draw QPixmap with 'color mod'

Some rendering libraries allow to set a 'color mod' (for example, in SDL2 you do it with SDL_SetTextureColorMod) when drawing a texture, which would effectively multiply the colours of the pixels by a given value before drawing. What is the best way to achieve this in Qt5, for example, when drawing a QPixmap with QPainter::drawPixmap? So far the only option I see is to use a temporary pixmap, fill it with the colour by which I want to multiply, then draw on it with QPainter::CompositionMode_Multiply and then draw the result to the target device. Is there a more straightforward way that maybe does not include drawing to a temporary pixmap?

Upvotes: 0

Views: 1037

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6594

You can do without the temporay pixmap by drawing a rect with size of your pixmap in your target:

QPixmap const src(":/images/img.png");
painter->fillRect(QRect(QPoint(0, 0), src.size()), Qt::red);
painter->setCompositionMode(QPainter::CompositionMode_Multiply);
painter->drawPixmap(QPoint(0, 0), src);

If your pixmap has transparent regions, you can add painter->setClipRegion(src.mask()); before calling fillRect.

Upvotes: 2

Related Questions