File indexing completed on 2024-04-21 03:53:24

0001 /*
0002     SPDX-FileCopyrightText: 2009 Matthew Woehlke <mw_triad@users.sourceforge.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kcolorutilsdemo.h"
0008 #include <KColorUtils>
0009 #include <kcolorscheme.h>
0010 
0011 #include <QApplication>
0012 
0013 KColorUtilsDemo::KColorUtilsDemo(QWidget *parent)
0014     : QWidget(parent)
0015     , _leOutImg(128, 128, QImage::Format_RGB32)
0016     , _mtMixOutImg(128, 16, QImage::Format_RGB32)
0017     , _mtTintOutImg(128, 16, QImage::Format_RGB32)
0018 {
0019     _noUpdate = true;
0020     setupUi(this);
0021     _noUpdate = false;
0022 
0023     inputSpinChanged();
0024     targetSpinChanged();
0025 }
0026 
0027 void KColorUtilsDemo::inputChanged()
0028 {
0029     qreal hue;
0030     qreal chroma;
0031     qreal luma;
0032     KColorUtils::getHcy(inColor->color(), &hue, &chroma, &luma);
0033     ifHue->setValue(hue);
0034     ifChroma->setValue(chroma);
0035     ifLuma->setValue(luma);
0036     ifGray->setValue(qGray(inColor->color().rgb()));
0037 
0038     lumaChanged();
0039     mixChanged();
0040     shadeChanged();
0041 }
0042 
0043 void KColorUtilsDemo::lumaChanged()
0044 {
0045     QColor base = inColor->color();
0046 
0047     for (int y = 0; y < 128; ++y) {
0048         qreal k = qreal(y - 64) / 64.0;
0049 
0050         for (int x = 0; x < 128; ++x) {
0051             qreal c;
0052 
0053             QColor r;
0054             if (leOpLighten->isChecked()) {
0055                 c = qreal(128 - x) / 64.0;
0056                 r = KColorUtils::lighten(base, k, c);
0057             } else if (leOpDarken->isChecked()) {
0058                 c = qreal(x) / 64.0;
0059                 r = KColorUtils::darken(base, -k, c);
0060             } else {
0061                 c = qreal(x - 64) / 64.0;
0062                 r = KColorUtils::shade(base, k, c);
0063             }
0064             _leOutImg.setPixel(x, y, r.rgb());
0065         }
0066     }
0067 
0068     leOut->setImage(_leOutImg);
0069 }
0070 
0071 void KColorUtilsDemo::mixChanged()
0072 {
0073     QColor base = inColor->color();
0074     QColor target = mtTarget->color();
0075 
0076     for (int x = 0; x < 128; ++x) {
0077         qreal k = qreal(x) / 128.0;
0078 
0079         QRgb m = KColorUtils::mix(base, target, k).rgb();
0080         QRgb t = KColorUtils::tint(base, target, k).rgb();
0081 
0082         for (int y = 0; y < 16; ++y) {
0083             _mtMixOutImg.setPixel(x, y, m);
0084             _mtTintOutImg.setPixel(x, y, t);
0085         }
0086     }
0087 
0088     mtMixOut->setImage(_mtMixOutImg);
0089     mtTintOut->setImage(_mtTintOutImg);
0090 }
0091 
0092 void setBackground(QWidget *widget, const QColor &color)
0093 {
0094     QPalette palette = widget->palette();
0095     palette.setColor(widget->backgroundRole(), color);
0096     widget->setPalette(palette);
0097 
0098     QString name = color.name();
0099     name += " (" + QString::number(color.red()) + ", " + QString::number(color.green()) + ", " + QString::number(color.blue()) + QLatin1Char(')');
0100     widget->setToolTip(name);
0101 }
0102 
0103 #define SET_SHADE(_n, _c, _cn, _ch) setBackground(ss##_n, KColorScheme::shade(_c, KColorScheme::_n##Shade, _cn, _ch));
0104 
0105 void KColorUtilsDemo::shadeChanged()
0106 {
0107     qreal cn = ssContrast->value();
0108     qreal ch = ssChroma->value();
0109 
0110     QColor base = inColor->color();
0111     setBackground(ssOut, base);
0112     setBackground(ssBase, base);
0113     SET_SHADE(Light, base, cn, ch);
0114     SET_SHADE(Midlight, base, cn, ch);
0115     SET_SHADE(Mid, base, cn, ch);
0116     SET_SHADE(Dark, base, cn, ch);
0117     SET_SHADE(Shadow, base, cn, ch);
0118 }
0119 
0120 void updateSwatch(KColorButton *s, const QSpinBox *r, const QSpinBox *g, const QSpinBox *b)
0121 {
0122     s->setColor(QColor(r->value(), g->value(), b->value()));
0123 }
0124 
0125 void updateSpins(const QColor &c, QSpinBox *r, QSpinBox *g, QSpinBox *b)
0126 {
0127     r->setValue(c.red());
0128     g->setValue(c.green());
0129     b->setValue(c.blue());
0130 }
0131 
0132 void KColorUtilsDemo::inputSpinChanged()
0133 {
0134     if (_noUpdate) {
0135         return;
0136     }
0137     _noUpdate = true;
0138 
0139     updateSwatch(inColor, inRed, inGreen, inBlue);
0140     inputChanged();
0141 
0142     _noUpdate = false;
0143 }
0144 
0145 void KColorUtilsDemo::targetSpinChanged()
0146 {
0147     if (_noUpdate) {
0148         return;
0149     }
0150     _noUpdate = true;
0151 
0152     updateSwatch(mtTarget, mtRed, mtGreen, mtBlue);
0153     mixChanged();
0154 
0155     _noUpdate = false;
0156 }
0157 
0158 void KColorUtilsDemo::inputSwatchChanged(const QColor &color)
0159 {
0160     if (_noUpdate) {
0161         return;
0162     }
0163     _noUpdate = true;
0164 
0165     updateSpins(color, inRed, inGreen, inBlue);
0166     inputChanged();
0167 
0168     _noUpdate = false;
0169 }
0170 
0171 void KColorUtilsDemo::targetSwatchChanged(const QColor &color)
0172 {
0173     if (_noUpdate) {
0174         return;
0175     }
0176     _noUpdate = true;
0177 
0178     updateSpins(color, mtRed, mtGreen, mtBlue);
0179     mixChanged();
0180 
0181     _noUpdate = false;
0182 }
0183 
0184 int main(int argc, char *argv[])
0185 {
0186     QApplication app(argc, argv);
0187 
0188     KColorUtilsDemo *d = new KColorUtilsDemo;
0189     d->show();
0190     return app.exec();
0191 }
0192 
0193 #include "moc_kcolorutilsdemo.cpp"