- Save/load palettes properly
[dashstudio.git] / src / components / colorpalette / private / luminancepicker.cpp
blobea6b840d9912c3fbaa9c375431b925ce750cad2a
1 /***************************************************************************
2 * Copyright (C) 2005 by Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "luminancepicker.h"
23 #include <QPainter>
24 #include <qdrawutil.h>
25 #include <dcore/debug.h>
27 namespace Component {
29 struct LuminancePicker::Private
31 int val;
32 int hue;
33 int sat;
34 QPixmap *pix;
36 ~Private()
38 delete pix;
43 int LuminancePicker::y2val(int y)
45 int d = height() - 2*coff - 1;
46 return 255 - (y - coff)*255/d;
49 int LuminancePicker::val2y(int v)
51 int d = height() - 2*coff - 1;
52 return coff + (255-v)*d/255;
55 LuminancePicker::LuminancePicker(QWidget* parent)
56 :QWidget(parent), d( new Private)
58 d->hue = 100; d->val = 100; d->sat = 100;
59 d->pix = 0;
60 // setAttribute(WA_NoErase, true);
63 LuminancePicker::~LuminancePicker()
65 delete d;
66 D_END;
69 void LuminancePicker::mouseMoveEvent(QMouseEvent *m)
71 setVal(y2val(m->y()));
74 void LuminancePicker::mousePressEvent(QMouseEvent *m)
76 setVal(y2val(m->y()));
79 void LuminancePicker::setVal(int v)
81 if (d->val == v)
82 return;
83 d->val = qMax(0, qMin(v,255));
84 delete d->pix; d->pix=0;
85 repaint();
86 emit newHsv(d->hue, d->sat, d->val);
89 //receives from a d->hue,d->sat chooser and relays.
90 void LuminancePicker::setCol(int h, int s)
92 setCol(h, s, d->val);
93 emit newHsv(h, s, d->val);
96 void LuminancePicker::paintEvent(QPaintEvent *)
98 int w = width() - 5;
100 QRect r(0, foff, w, height() - 2*foff);
101 int wi = r.width() - 2;
102 int hi = r.height() - 2;
103 if (!d->pix || d->pix->height() != hi || d->pix->width() != wi) {
104 delete d->pix;
105 QImage img(wi, hi, QImage::Format_RGB32);
106 int y;
107 for (y = 0; y < hi; y++) {
108 QColor c;
109 c.setHsv(d->hue, d->sat, y2val(y+coff));
110 QRgb r = c.rgb();
111 int x;
112 for (x = 0; x < wi; x++)
113 img.setPixel(x, y, r);
115 d->pix = new QPixmap(QPixmap::fromImage(img));
117 QPainter p(this);
118 p.drawPixmap(1, coff, *d->pix);
119 const QPalette &g = palette();
120 qDrawShadePanel(&p, r, g, true);
121 p.setPen(g.foreground().color());
122 p.setBrush(g.foreground());
123 QPolygon a;
124 int y = val2y(d->val);
125 a.setPoints(3, w, y, w+5, y+5, w+5, y-5);
126 p.eraseRect(w, 0, 5, height());
127 p.drawPolygon(a);
130 void LuminancePicker::setCol(int h, int s , int v)
132 d->val = v;
133 d->hue = h;
134 d->sat = s;
135 delete d->pix; d->pix=0;
136 repaint();
139 int LuminancePicker::value()
141 return d->val;