Change pixel color of QPixmap
Solved
General and Desktop
-
If I comment out the line setPixel(), then the picture will look like the original:
If I do as described, then it looks like this:
So it is changing the pixelvalues. But it is a little rough on the edges.@Jakob-Clausen Perhaps you should try to force pixel format to what you need:
QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from) { QImage temp = pixmap.toImage(); qDebug() << "Pixel format is:" << temp.pixelFormat(); if(temp.pixelFormat() != QImage::Format_ARGB32) temp.convertTo(QImage::Format_ARGB32); for (int y = 0; y < temp.height(); ++y) { QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y)); for (int x= 0; x < temp.width(); ++x, ++s) { QColor color = QColor::fromRgba(*s); uint8_t alpha = color.alpha(); to.setAlpha(alpha); from.setAlpha(alpha); if (color == from) *s = to.rgba(); } } return QPixmap::fromImage(temp); }
-
QImage::Format_ARGB32_Premultiplied is not a good idea to do the pixel stuff then... see the docs: https://doc.qt.io/qt-5/qimage.html#Format-enum
-
@Jakob-Clausen Perhaps you should try to force pixel format to what you need:
QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from) { QImage temp = pixmap.toImage(); qDebug() << "Pixel format is:" << temp.pixelFormat(); if(temp.pixelFormat() != QImage::Format_ARGB32) temp.convertTo(QImage::Format_ARGB32); for (int y = 0; y < temp.height(); ++y) { QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y)); for (int x= 0; x < temp.width(); ++x, ++s) { QColor color = QColor::fromRgba(*s); uint8_t alpha = color.alpha(); to.setAlpha(alpha); from.setAlpha(alpha); if (color == from) *s = to.rgba(); } } return QPixmap::fromImage(temp); }
@KroMignon Thats a good idea. convertTo() is introduced in 5.13. But if I use
temp = temp.convertToFormat(QImage::Format_ARGB32);
Then it seems to work.
Thank you very much. -
J JoeCFD referenced this topic on