1 /***************************************************************************
2 * Copyright (C) 2005 by Jorge Cuadrado *
3 * kuadrosxx@gmail.com *
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. *
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. *
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"
24 #include <qdrawutil.h>
25 #include <dcore/debug.h>
29 struct LuminancePicker::Private
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;
60 // setAttribute(WA_NoErase, true);
63 LuminancePicker::~LuminancePicker()
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
)
83 d
->val
= qMax(0, qMin(v
,255));
84 delete d
->pix
; d
->pix
=0;
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
)
93 emit
newHsv(h
, s
, d
->val
);
96 void LuminancePicker::paintEvent(QPaintEvent
*)
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
) {
105 QImage
img(wi
, hi
, QImage::Format_RGB32
);
107 for (y
= 0; y
< hi
; y
++) {
109 c
.setHsv(d
->hue
, d
->sat
, y2val(y
+coff
));
112 for (x
= 0; x
< wi
; x
++)
113 img
.setPixel(x
, y
, r
);
115 d
->pix
= new QPixmap(QPixmap::fromImage(img
));
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());
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());
130 void LuminancePicker::setCol(int h
, int s
, int v
)
135 delete d
->pix
; d
->pix
=0;
139 int LuminancePicker::value()