Chris@289: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@289: 
Chris@289: /*
Chris@289:     Sonic Visualiser
Chris@289:     An audio file viewer and annotation editor.
Chris@289:     Centre for Digital Music, Queen Mary, University of London.
Chris@289:     This file copyright 2007 QMUL.
Chris@289:     
Chris@289:     This program is free software; you can redistribute it and/or
Chris@289:     modify it under the terms of the GNU General Public License as
Chris@289:     published by the Free Software Foundation; either version 2 of the
Chris@289:     License, or (at your option) any later version.  See the file
Chris@289:     COPYING included with this distribution for more information.
Chris@289: */
Chris@289: 
Chris@289: #include "IconLoader.h"
Chris@289: 
Chris@289: #include <QPixmap>
Chris@289: #include <QApplication>
Chris@289: #include <QPainter>
Chris@289: #include <QPalette>
Chris@289: 
Chris@289: static const char *autoInvertExceptions[] = {
Chris@289:     // These are the icons that look OK in their default colours, even
Chris@289:     // in a colour scheme with a black background.  (They may also be
Chris@289:     // icons that would look worse if we tried to auto-invert them.)
Chris@289:     // If we have icons that look bad when auto-inverted but that are
Chris@289:     // not suitable for use without being inverted, we'll need to
Chris@289:     // supply inverted versions -- the loader will load xx_inverse.png
Chris@289:     // in preference to xx.png if a dark background is found.)
Chris@289:     "fileclose",
Chris@289:     "filenew-22",
Chris@289:     "filenew",
Chris@289:     "fileopen-22",
Chris@289:     "fileopen",
Chris@289:     "fileopenaudio",
Chris@289:     "fileopensession",
Chris@289:     "filesave-22",
Chris@289:     "filesave",
Chris@289:     "filesaveas-22",
Chris@289:     "filesaveas",
Chris@289:     "help",
Chris@289:     "editcut",
Chris@289:     "editcopy",
Chris@289:     "editpaste",
Chris@289:     "editdelete",
Chris@289:     "exit",
Chris@289:     "zoom-fit",
Chris@289:     "zoom-in",
Chris@289:     "zoom-out",
Chris@289:     "zoom"
Chris@289: };
Chris@289: 
Chris@289: QIcon
Chris@289: IconLoader::load(QString name)
Chris@289: {
Chris@289:     QPixmap pmap(loadPixmap(name));
Chris@289:     if (pmap.isNull()) return QIcon();
Chris@289:     else return QIcon(pmap);
Chris@289: }
Chris@289: 
Chris@289: QPixmap
Chris@289: IconLoader::loadPixmap(QString name)
Chris@289: {
Chris@289:     QColor bg = QApplication::palette().window().color();
Chris@289:     if (bg.red() + bg.green() + bg.blue() > 384) { // light background
Chris@289:         QPixmap pmap(QString(":icons/%1").arg(name));
Chris@289:         if (pmap.isNull()) {
Chris@289:             pmap = QPixmap(QString(":icons/%1.png").arg(name));
Chris@289:         }
Chris@289:         return pmap;
Chris@289:     }
Chris@289: 
Chris@289:     QPixmap pmap(QString(":icons/%1").arg(name));
Chris@289:     if (pmap.isNull()) {
Chris@289:         pmap = QPixmap(QString(":icons/%1_inverse.png").arg(name));
Chris@289:         if (pmap.isNull()) {
Chris@289:             pmap = QPixmap(QString(":icons/%1.png").arg(name));
Chris@289:         }
Chris@289:     }
Chris@289:     if (pmap.isNull()) return pmap;
Chris@289: 
Chris@289:     for (int i = 0; i < sizeof(autoInvertExceptions)/
Chris@289:                         sizeof(autoInvertExceptions[0]); ++i) {
Chris@289:         if (autoInvertExceptions[i] == name) {
Chris@289:             return pmap;
Chris@289:         }
Chris@289:     }
Chris@289: 
Chris@289:     // No suitable inverted icon found for black background; try to
Chris@289:     // auto-invert the default one
Chris@289: 
Chris@289:     QImage img = pmap.toImage().convertToFormat(QImage::Format_ARGB32);
Chris@289: 
Chris@289:     for (int y = 0; y < img.height(); ++y) {
Chris@289:         for (int x = 0; x < img.width(); ++x) {
Chris@289: 
Chris@289:             QRgb rgba = img.pixel(x, y);
Chris@289:             QColor colour = QColor
Chris@289:                 (qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
Chris@289: 
Chris@289:             int alpha = colour.alpha();
Chris@289:             if (colour.saturation() < 5 && colour.alpha() > 10) {
Chris@289:                 colour.setHsv(colour.hue(),
Chris@289:                               colour.saturation(),
Chris@289:                               255 - colour.value());
Chris@289:                 colour.setAlpha(alpha);
Chris@289:                 img.setPixel(x, y, colour.rgba());
Chris@289:             }
Chris@289:         }
Chris@289:     }
Chris@289: 
Chris@289:     pmap = QPixmap::fromImage(img);
Chris@289:     return pmap;
Chris@289: }
Chris@289: