Code SnippetsQt: Tint alphatransparent PNG Syndicate content

Wed, 09/21/2011 - 18:07

Let’s assume you want to display the logo of your company in your Qt app. Most probably that logo has just single color with an alpha channel.But: Having the color hard coded in the image is not nice, there are users (like me!) out there, who use a custom (dark!) color scheme. Meaning: If your logo is black/dark and assumes a bright background and you just embed it blindly in your app, I probably won’t see it since the background will be dark in my case.

Here is a solution for the simple case of a mono-colored PNG with an alpha channel which I came up with:

  1. QLabel* label = new QLabel;
  2. // load your image
  3. QImage img(QString("..."));
  4. // morph it into a grayscale image
  5. img = img.alphaChannel();
  6. // the new color we want the logo to have
  7. QColor foreground = label->palette().foreground().color();
  8. // now replace the colors in the image
  9. for(int i = 0; i < img.colorCount(); ++i) {
  10. foreground.setAlpha(qGray(img.color(i)));
  11. img.setColor(i, foreground.rgba());
  12. }
  13. // display the new logo
  14. label->setPixmap(QPixmap::fromImage(img));
  15. label->show();

This seems to work just fine for me. YMMV.


Milian you know what a Wed, 09/21/2011 - 21:00 — NUNO PINHEIRO (not verified)

Milian you know what a designer would do to you if you start changing the color of his logos don’t you? ;)

Probably he’d rip my head of Wed, 09/21/2011 - 21:59 — Milian Wolff

Probably he’d rip my head of for changing the app’s palette to a dark scheme in the first place. Wait a minute, do I have a deja vu? :D

Na, seriously: Better a visible logo in a different color than no logo at all.

Technically, good code, but Wed, 09/21/2011 - 19:44 — jstaniek (not verified)

Technically, good code, but in addition to what Ariya said, the example with logo can be only sometimes appropriate. By a rule of thumb, good logos should have defines limited number of backgrounds and foregrounds (2 to 3) and post-processing them is then forbidden. No cropping, and so on. There are a lot of rules noted down in the interweb, e.g.


Sure, but that would mean the Wed, 09/21/2011 - 22:03 — Milian Wolff

Sure, but that would mean the logo must come with a hard-coded background which would stick out of the rest of the application. Anyhow, the above worked for me for now :)

You can also use QPainter’s Wed, 09/21/2011 - 19:26 — Ariya (not verified)

You can also use QPainter’s composition support, see e.g. for the details.

I searched the hell of the Wed, 09/21/2011 - 22:01 — Milian Wolff

I searched the hell of the interwebz and did not find anything and now this… Sigh, would have made my live a lot easier.

Thanks, noted for the future.

Post new comment

  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options