DragonDrop
DragonDrop

Reputation: 221

Graying Out a QPixmap

I am trying to figure how with I can take a QPixmap and make it appear 'grayed out'... basically, remove all color and lower the alpha.

Upvotes: 2

Views: 2751

Answers (3)

PhotoMonkee
PhotoMonkee

Reputation: 436

A desaturation algorithm (removing all color) would involve iterating over every pixel and normalizing the RGB values.

The pseudocode looks like this:

foreach pixel in image.bits() {

   int grayColor = 0.299f * pixel.red +
                    0.587f * pixel.green +
                    0.114f * pixel.blue ;

   image.putPixel(grayColor);
}

Upvotes: 2

Stephen Chu
Stephen Chu

Reputation: 12832

QGraphicsColorizeEffect is probably a more preferred way going forward.

Upvotes: 2

jkerian
jkerian

Reputation: 17046

Can you use something like this to set the alpha?

QPixmap &setAlpha(QPixmap &px, int val){
  QPixmap alpha = px;
  QPainter p(&alpha);
  p.fillRect(alpha.rect(), QColor(val, val, val));
  p.end();
  px.setAlphaChannel(alpha);
  return px;
}

You may need to convert to a QImage and use convertToFormat() to conveniently convert to greyscale.

Upvotes: 2

Related Questions