aboutsummaryrefslogtreecommitdiff
path: root/src/qt/scicon.cpp
blob: a0ffcd82a924240fc6991b450a5b3bab66bce7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2014 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "scicon.h"

#include <QApplication>
#include <QColor>
#include <QIcon>
#include <QImage>
#include <QPalette>
#include <QPixmap>

static void MakeSingleColorImage(QImage& img, const QColor& colorbase)
{
    img = img.convertToFormat(QImage::Format_ARGB32);
    for (int x = img.width(); x--; )
    {
        for (int y = img.height(); y--; )
        {
            const QRgb rgb = img.pixel(x, y);
            img.setPixel(x, y, qRgba(colorbase.red(), colorbase.green(), colorbase.blue(), qAlpha(rgb)));
        }
    }
}

QImage SingleColorImage(const QString& filename, const QColor& colorbase)
{
    QImage img(filename);
    MakeSingleColorImage(img, colorbase);
    return img;
}

QIcon SingleColorIcon(const QIcon& ico, const QColor& colorbase)
{
    QIcon new_ico;
    QSize sz;
    Q_FOREACH(sz, ico.availableSizes())
    {
        QImage img(ico.pixmap(sz).toImage());
        MakeSingleColorImage(img, colorbase);
        new_ico.addPixmap(QPixmap::fromImage(img));
    }
    return new_ico;
}

QIcon SingleColorIcon(const QString& filename, const QColor& colorbase)
{
    return QIcon(QPixmap::fromImage(SingleColorImage(filename, colorbase)));
}

QColor SingleColor()
{
    const QColor colorHighlightBg(QApplication::palette().color(QPalette::Highlight));
    const QColor colorHighlightFg(QApplication::palette().color(QPalette::HighlightedText));
    const QColor colorText(QApplication::palette().color(QPalette::WindowText));
    const int colorTextLightness = colorText.lightness();
    QColor colorbase;
    if (abs(colorHighlightBg.lightness() - colorTextLightness) < abs(colorHighlightFg.lightness() - colorTextLightness))
        colorbase = colorHighlightBg;
    else
        colorbase = colorHighlightFg;
    return colorbase;
}

QIcon SingleColorIcon(const QString& filename)
{
    return SingleColorIcon(filename, SingleColor());
}

static QColor TextColor()
{
    return QColor(QApplication::palette().color(QPalette::WindowText));
}

QIcon TextColorIcon(const QString& filename)
{
    return SingleColorIcon(filename, TextColor());
}

QIcon TextColorIcon(const QIcon& ico)
{
    return SingleColorIcon(ico, TextColor());
}