File indexing completed on 2024-04-14 14:20:20

0001 /* This file is part of the KDE libraries
0002   Copyright (C) 1997 Martin Jones (mjones@kde.org)
0003 
0004   This library is free software; you can redistribute it and/or
0005   modify it under the terms of the GNU Library General Public
0006   License as published by the Free Software Foundation; either
0007   version 2 of the License, or (at your option) any later version.
0008 
0009   This library is distributed in the hope that it will be useful,
0010   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012   Library General Public License for more details.
0013 
0014   You should have received a copy of the GNU Library General Public License
0015   along with this library; see the file COPYING.LIB.  If not, write to
0016   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017   Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "khuesaturationselect.h"
0021 
0022 #include <QPainter>
0023 
0024 #include "kcolorchoosermode_p.h"
0025 
0026 using namespace KDEPrivate;
0027 
0028 class Q_DECL_HIDDEN KHueSaturationSelector::Private
0029 {
0030 public:
0031     Private(KHueSaturationSelector *q): q(q) {}
0032 
0033     KHueSaturationSelector *q;
0034     QPixmap pixmap;
0035 
0036     /**
0037      * Stores the chooser mode
0038      */
0039     KColorChooserMode _mode;
0040 
0041     /**
0042      * Stores the values for hue, saturation and lumniousity
0043      */
0044     int _hue, _sat, _colorValue;
0045 };
0046 
0047 KHueSaturationSelector::KHueSaturationSelector(QWidget *parent)
0048     : KXYSelector(parent), d(new Private(this))
0049 {
0050     setChooserMode(ChooserClassic);
0051 }
0052 
0053 KColorChooserMode KHueSaturationSelector::chooserMode() const
0054 {
0055     return d->_mode;
0056 }
0057 
0058 void KHueSaturationSelector::setChooserMode(KColorChooserMode chooserMode)
0059 {
0060     int x;
0061     int y = 255;
0062 
0063     switch (chooserMode) {
0064     case ChooserSaturation:
0065     case ChooserValue:
0066         x = 359;
0067         break;
0068     default:
0069         x = 255;
0070         break;
0071     }
0072 
0073     setRange(0, 0, x, y);
0074     d->_mode = chooserMode;
0075 }
0076 
0077 int KHueSaturationSelector::hue() const
0078 {
0079     return d->_hue;
0080 }
0081 
0082 void KHueSaturationSelector::setHue(int hue)
0083 {
0084     d->_hue = hue;
0085 }
0086 
0087 int KHueSaturationSelector::saturation() const
0088 
0089 {
0090     return d->_sat;
0091 }
0092 
0093 void KHueSaturationSelector::setSaturation(int saturation)
0094 {
0095     d->_sat = saturation;
0096 }
0097 
0098 int KHueSaturationSelector::colorValue() const
0099 {
0100     return d->_colorValue;
0101 }
0102 
0103 void KHueSaturationSelector::setColorValue(int colorValue)
0104 {
0105     d->_colorValue = colorValue;
0106 }
0107 
0108 KHueSaturationSelector::~KHueSaturationSelector()
0109 {
0110     delete d;
0111 }
0112 
0113 void KHueSaturationSelector::updateContents()
0114 {
0115     drawPalette(&d->pixmap);
0116 }
0117 
0118 void KHueSaturationSelector::resizeEvent(QResizeEvent *)
0119 {
0120     updateContents();
0121 }
0122 
0123 void KHueSaturationSelector::drawContents(QPainter *painter)
0124 {
0125     painter->drawPixmap(contentsRect().x(), contentsRect().y(), d->pixmap);
0126 }
0127 
0128 void KHueSaturationSelector::drawPalette(QPixmap *pixmap)
0129 {
0130     int xSteps = componentXSteps(chooserMode());
0131     int ySteps = componentYSteps(chooserMode());
0132 
0133     QColor color;
0134     color.setHsv(hue(), saturation(), chooserMode() == ChooserClassic ? 192 : colorValue());
0135 
0136     QImage image(QSize(xSteps + 1, ySteps + 1), QImage::Format_RGB32);
0137     for (int y = 0; y <= ySteps; ++y) {
0138         setComponentY(color, chooserMode(), y * (1.0 / ySteps));
0139         for (int x = 0; x <= xSteps; ++x) {
0140             setComponentX(color, chooserMode(), x * (1.0 / xSteps));
0141             image.setPixel(x, ySteps - y, color.rgb());
0142         }
0143     }
0144 
0145     QPixmap pix(contentsRect().size());
0146     QPainter painter(&pix);
0147     // Bilinear filtering
0148     painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
0149     QRectF srcRect(0.5, 0.5, xSteps, ySteps);
0150     QRectF destRect(QPointF(0, 0), contentsRect().size());
0151     painter.drawImage(destRect, image, srcRect);
0152     painter.end();
0153 
0154     *pixmap = pix;
0155 }
0156 
0157 #include "moc_khuesaturationselect.cpp"