Mercurial > hg > svgui
comparison widgets/IconLoader.cpp @ 289:4ca7562dd246
* Add icon loader that automatically inverts icons where appropriate for
(slightly) better display on a dark background
author | Chris Cannam |
---|---|
date | Fri, 13 Jul 2007 13:19:15 +0000 |
parents | |
children | e4773943c9c1 |
comparison
equal
deleted
inserted
replaced
288:e27f546f83ad | 289:4ca7562dd246 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2007 QMUL. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #include "IconLoader.h" | |
17 | |
18 #include <QPixmap> | |
19 #include <QApplication> | |
20 #include <QPainter> | |
21 #include <QPalette> | |
22 | |
23 static const char *autoInvertExceptions[] = { | |
24 // These are the icons that look OK in their default colours, even | |
25 // in a colour scheme with a black background. (They may also be | |
26 // icons that would look worse if we tried to auto-invert them.) | |
27 // If we have icons that look bad when auto-inverted but that are | |
28 // not suitable for use without being inverted, we'll need to | |
29 // supply inverted versions -- the loader will load xx_inverse.png | |
30 // in preference to xx.png if a dark background is found.) | |
31 "fileclose", | |
32 "filenew-22", | |
33 "filenew", | |
34 "fileopen-22", | |
35 "fileopen", | |
36 "fileopenaudio", | |
37 "fileopensession", | |
38 "filesave-22", | |
39 "filesave", | |
40 "filesaveas-22", | |
41 "filesaveas", | |
42 "help", | |
43 "editcut", | |
44 "editcopy", | |
45 "editpaste", | |
46 "editdelete", | |
47 "exit", | |
48 "zoom-fit", | |
49 "zoom-in", | |
50 "zoom-out", | |
51 "zoom" | |
52 }; | |
53 | |
54 QIcon | |
55 IconLoader::load(QString name) | |
56 { | |
57 QPixmap pmap(loadPixmap(name)); | |
58 if (pmap.isNull()) return QIcon(); | |
59 else return QIcon(pmap); | |
60 } | |
61 | |
62 QPixmap | |
63 IconLoader::loadPixmap(QString name) | |
64 { | |
65 QColor bg = QApplication::palette().window().color(); | |
66 if (bg.red() + bg.green() + bg.blue() > 384) { // light background | |
67 QPixmap pmap(QString(":icons/%1").arg(name)); | |
68 if (pmap.isNull()) { | |
69 pmap = QPixmap(QString(":icons/%1.png").arg(name)); | |
70 } | |
71 return pmap; | |
72 } | |
73 | |
74 QPixmap pmap(QString(":icons/%1").arg(name)); | |
75 if (pmap.isNull()) { | |
76 pmap = QPixmap(QString(":icons/%1_inverse.png").arg(name)); | |
77 if (pmap.isNull()) { | |
78 pmap = QPixmap(QString(":icons/%1.png").arg(name)); | |
79 } | |
80 } | |
81 if (pmap.isNull()) return pmap; | |
82 | |
83 for (int i = 0; i < sizeof(autoInvertExceptions)/ | |
84 sizeof(autoInvertExceptions[0]); ++i) { | |
85 if (autoInvertExceptions[i] == name) { | |
86 return pmap; | |
87 } | |
88 } | |
89 | |
90 // No suitable inverted icon found for black background; try to | |
91 // auto-invert the default one | |
92 | |
93 QImage img = pmap.toImage().convertToFormat(QImage::Format_ARGB32); | |
94 | |
95 for (int y = 0; y < img.height(); ++y) { | |
96 for (int x = 0; x < img.width(); ++x) { | |
97 | |
98 QRgb rgba = img.pixel(x, y); | |
99 QColor colour = QColor | |
100 (qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba)); | |
101 | |
102 int alpha = colour.alpha(); | |
103 if (colour.saturation() < 5 && colour.alpha() > 10) { | |
104 colour.setHsv(colour.hue(), | |
105 colour.saturation(), | |
106 255 - colour.value()); | |
107 colour.setAlpha(alpha); | |
108 img.setPixel(x, y, colour.rgba()); | |
109 } | |
110 } | |
111 } | |
112 | |
113 pmap = QPixmap::fromImage(img); | |
114 return pmap; | |
115 } | |
116 |