File: kcolorutilsdemo.cpp

package info (click to toggle)
kf6-kconfigwidgets 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,020 kB
  • sloc: cpp: 6,309; sh: 18; makefile: 7
file content (193 lines) | stat: -rw-r--r-- 4,499 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
    SPDX-FileCopyrightText: 2009 Matthew Woehlke <mw_triad@users.sourceforge.net>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "kcolorutilsdemo.h"
#include <KColorUtils>
#include <kcolorscheme.h>

#include <QApplication>

KColorUtilsDemo::KColorUtilsDemo(QWidget *parent)
    : QWidget(parent)
    , _leOutImg(128, 128, QImage::Format_RGB32)
    , _mtMixOutImg(128, 16, QImage::Format_RGB32)
    , _mtTintOutImg(128, 16, QImage::Format_RGB32)
{
    _noUpdate = true;
    setupUi(this);
    _noUpdate = false;

    inputSpinChanged();
    targetSpinChanged();
}

void KColorUtilsDemo::inputChanged()
{
    qreal hue;
    qreal chroma;
    qreal luma;
    KColorUtils::getHcy(inColor->color(), &hue, &chroma, &luma);
    ifHue->setValue(hue);
    ifChroma->setValue(chroma);
    ifLuma->setValue(luma);
    ifGray->setValue(qGray(inColor->color().rgb()));

    lumaChanged();
    mixChanged();
    shadeChanged();
}

void KColorUtilsDemo::lumaChanged()
{
    QColor base = inColor->color();

    for (int y = 0; y < 128; ++y) {
        qreal k = qreal(y - 64) / 64.0;

        for (int x = 0; x < 128; ++x) {
            qreal c;

            QColor r;
            if (leOpLighten->isChecked()) {
                c = qreal(128 - x) / 64.0;
                r = KColorUtils::lighten(base, k, c);
            } else if (leOpDarken->isChecked()) {
                c = qreal(x) / 64.0;
                r = KColorUtils::darken(base, -k, c);
            } else {
                c = qreal(x - 64) / 64.0;
                r = KColorUtils::shade(base, k, c);
            }
            _leOutImg.setPixel(x, y, r.rgb());
        }
    }

    leOut->setImage(_leOutImg);
}

void KColorUtilsDemo::mixChanged()
{
    QColor base = inColor->color();
    QColor target = mtTarget->color();

    for (int x = 0; x < 128; ++x) {
        qreal k = qreal(x) / 128.0;

        QRgb m = KColorUtils::mix(base, target, k).rgb();
        QRgb t = KColorUtils::tint(base, target, k).rgb();

        for (int y = 0; y < 16; ++y) {
            _mtMixOutImg.setPixel(x, y, m);
            _mtTintOutImg.setPixel(x, y, t);
        }
    }

    mtMixOut->setImage(_mtMixOutImg);
    mtTintOut->setImage(_mtTintOutImg);
}

void setBackground(QWidget *widget, const QColor &color)
{
    QPalette palette = widget->palette();
    palette.setColor(widget->backgroundRole(), color);
    widget->setPalette(palette);

    QString name = color.name();
    name += " (" + QString::number(color.red()) + ", " + QString::number(color.green()) + ", " + QString::number(color.blue()) + QLatin1Char(')');
    widget->setToolTip(name);
}

#define SET_SHADE(_n, _c, _cn, _ch) setBackground(ss##_n, KColorScheme::shade(_c, KColorScheme::_n##Shade, _cn, _ch));

void KColorUtilsDemo::shadeChanged()
{
    qreal cn = ssContrast->value();
    qreal ch = ssChroma->value();

    QColor base = inColor->color();
    setBackground(ssOut, base);
    setBackground(ssBase, base);
    SET_SHADE(Light, base, cn, ch);
    SET_SHADE(Midlight, base, cn, ch);
    SET_SHADE(Mid, base, cn, ch);
    SET_SHADE(Dark, base, cn, ch);
    SET_SHADE(Shadow, base, cn, ch);
}

void updateSwatch(KColorButton *s, const QSpinBox *r, const QSpinBox *g, const QSpinBox *b)
{
    s->setColor(QColor(r->value(), g->value(), b->value()));
}

void updateSpins(const QColor &c, QSpinBox *r, QSpinBox *g, QSpinBox *b)
{
    r->setValue(c.red());
    g->setValue(c.green());
    b->setValue(c.blue());
}

void KColorUtilsDemo::inputSpinChanged()
{
    if (_noUpdate) {
        return;
    }
    _noUpdate = true;

    updateSwatch(inColor, inRed, inGreen, inBlue);
    inputChanged();

    _noUpdate = false;
}

void KColorUtilsDemo::targetSpinChanged()
{
    if (_noUpdate) {
        return;
    }
    _noUpdate = true;

    updateSwatch(mtTarget, mtRed, mtGreen, mtBlue);
    mixChanged();

    _noUpdate = false;
}

void KColorUtilsDemo::inputSwatchChanged(const QColor &color)
{
    if (_noUpdate) {
        return;
    }
    _noUpdate = true;

    updateSpins(color, inRed, inGreen, inBlue);
    inputChanged();

    _noUpdate = false;
}

void KColorUtilsDemo::targetSwatchChanged(const QColor &color)
{
    if (_noUpdate) {
        return;
    }
    _noUpdate = true;

    updateSpins(color, mtRed, mtGreen, mtBlue);
    mixChanged();

    _noUpdate = false;
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    KColorUtilsDemo *d = new KColorUtilsDemo;
    d->show();
    return app.exec();
}

#include "moc_kcolorutilsdemo.cpp"