-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsvcolordial.cpp
More file actions
350 lines (298 loc) · 9.89 KB
/
hsvcolordial.cpp
File metadata and controls
350 lines (298 loc) · 9.89 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "hsvcolordial.h"
#include <QDebug>
#include <QPainter>
#include <QImage>
#include <QRgb>
#include <qmath.h>
#include <QMouseEvent>
static const double BOX_VALUE_FACTOR = 0.75;
class HSVColorDialPrivate
{
public:
HSVColorDialPrivate() :
h(0), s(1), v(1),
inDialDrag(false),
inBoxDrag(false)
{
}
float h;
float s;
float v;
bool inDialDrag;
bool inBoxDrag;
QRect boxRect;
QImage renderedInnerBox;
QImage renderedDial;
};
namespace {
struct ColorDialMetrics {
ColorDialMetrics(int width, int height) {
size = (qMin(width, height) / 2) * 2;
offsetX = size < width ? (width - size) / 2 : 0;
outerRadius = size / 2 - size * .03;
innerRadius = outerRadius - size * .1;
}
int size;
int offsetX;
float outerRadius;
float innerRadius;
};
// Hue rotates counter-clockwise with 0 (red) on the right hand side
float hueFromXY(float x, float y)
{
return atan2(y, -x) / (2 * M_PI) + 0.5;
}
}
HSVColorDial::HSVColorDial(QWidget *parent) :
QWidget(parent),
d_ptr(new HSVColorDialPrivate)
{
}
HSVColorDial::~HSVColorDial()
{
}
bool HSVColorDial::hasHeightForWidth() const
{
return true;
}
int HSVColorDial::heightForWidth(int w) const
{
return w;
}
QSizePolicy HSVColorDial::sizePolicy() const
{
QSizePolicy result;
result.setHeightForWidth(true);
return result;
}
void HSVColorDial::setColor(const QColor &color)
{
Q_D(HSVColorDial);
d->h = color.hueF();
d->s = color.saturationF();
d->v = color.valueF();
while (d->h < 0.0f)
d->h += 1.0f;
while (d->h > 1.0f)
d->h -= 1.0f;
emit updateColor(QColor::fromHsvF(d->h, d->s, d->v));
d->renderedInnerBox = QImage();
update();
}
QColor HSVColorDial::getColor() const
{
return QColor::fromHsvF(d_ptr->h, d_ptr->s, d_ptr->v);
}
void HSVColorDial::resizeEvent(QResizeEvent *event)
{
Q_D(HSVColorDial);
d->renderedDial = QImage();
d->renderedInnerBox = QImage();
}
void HSVColorDial::paintEvent(QPaintEvent *event)
{
Q_D(HSVColorDial);
const auto metrics = ColorDialMetrics(width(), height());
const int pixelRatio = devicePixelRatio();
// QRgb widgetBackgroundPixel = QWidget::palette().color(QWidget::backgroundRole()).rgba();
QRgb widgetBackgroundPixel = qRgba(0, 0, 0, 0);
if (d->renderedDial.isNull())
{
int dialSize = metrics.size;
float rFactor = 1.0f;
if (pixelRatio > 1)
{
dialSize *= pixelRatio;
rFactor = 1.0f / pixelRatio;
d->renderedDial = QImage(dialSize, dialSize, QImage::Format_ARGB32);
d->renderedDial.setDevicePixelRatio(pixelRatio);
}
else
{
d->renderedDial = QImage(dialSize, dialSize, QImage::Format_ARGB32);
}
for (int row = 0; row < dialSize; row++)
{
QRgb *pixel = (QRgb *)d->renderedDial.scanLine(row);
for (int col = 0; col < dialSize; col++)
{
float dx = col - dialSize / 2.0f;
float dy = row - dialSize / 2.0f;
float r = sqrtf(dx*dx + dy*dy) * rFactor;
if (r <= metrics.outerRadius && r >= metrics.innerRadius)
{
float alpha;
if (metrics.outerRadius - 1 < r)
alpha = metrics.outerRadius - r;
else if (metrics.innerRadius + 1 > r)
alpha = r - metrics.innerRadius;
else
alpha = 1.0f;
float angle = hueFromXY(dx, dy);
QColor pixelColor = QColor::fromHsvF(angle, 1.0, 1.0, alpha);
*pixel++ = pixelColor.rgba();
}
else
*pixel++ = widgetBackgroundPixel;
}
}
}
if (d->renderedInnerBox.isNull())
{
int boxInnerRadius = (metrics.innerRadius - 2) * 0.7071;
int boxOrigin = metrics.size / 2 - boxInnerRadius;
int boxSize = boxInnerRadius * 2;
d->boxRect = QRect(boxOrigin, boxOrigin, boxSize, boxSize);
if (pixelRatio > 1)
{
boxSize *= pixelRatio;
d->renderedInnerBox = QImage(boxSize, boxSize, QImage::Format_ARGB32);
d->renderedInnerBox.setDevicePixelRatio(pixelRatio);
}
else
{
d->renderedInnerBox = QImage(boxSize, boxSize, QImage::Format_ARGB32);
}
for (int row = 0; row < boxSize; row++)
{
QRgb *pixel = (QRgb *)d->renderedInnerBox.scanLine(row);
float v = float(boxSize - row) / boxSize;
v = powf(v, BOX_VALUE_FACTOR);
for (int col = 0; col < boxSize; col++)
{
float s = float(boxSize - col) / boxSize;
s = powf(s, BOX_VALUE_FACTOR);
QColor pixelColor = QColor::fromHsvF(d->h, s, v);
*pixel++ = pixelColor.rgba();
}
}
}
QPainter painter(this);
painter.drawImage(QPoint(metrics.offsetX,0), d->renderedDial);
painter.drawImage(d->boxRect.topLeft() + QPoint(metrics.offsetX, 0), d->renderedInnerBox);
{ /* Selected hue */
QPointF center = QPointF(metrics.size / 2 + metrics.offsetX, metrics.size / 2) + QPointF(0.5f, 0.5f);
QPointF vector = QPointF(cos(d->h * -2.0 * M_PI), sin(d->h * -2.0 * M_PI));
QPointF p1 = vector * (metrics.innerRadius + 2) + center;
QPointF p2 = vector * (metrics.outerRadius - 2) + center;
QPen pen(QColor::fromRgb(0xFF, 0xFF, 0xFF));
pen.setWidth(2);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawLine(p1, p2);
}
{ /* Selected saturation, value */
float yFactor = 1.0f - powf(d->v, 1.0 / BOX_VALUE_FACTOR);
float xFactor = 1.0f - powf(d->s, 1.0 / BOX_VALUE_FACTOR);
const int SELECT_CIRCLE_RADIUS = 3;
QPoint selectedPoint = QPoint(round(xFactor * d->boxRect.width()), round(yFactor * d->boxRect.height())) + d->boxRect.topLeft();
QRect selectedBox = QRect(selectedPoint.x() - SELECT_CIRCLE_RADIUS - 1 + metrics.offsetX,
selectedPoint.y() - SELECT_CIRCLE_RADIUS - 1,
SELECT_CIRCLE_RADIUS * 2 + 1,
SELECT_CIRCLE_RADIUS * 2 + 1);
QPen pen;
if (d->s < 0.2 && d->v > 0.5)
pen.setColor(QColor::fromRgb(0x00, 0x00, 0x00));
else
pen.setColor(QColor::fromRgb(0xFF, 0xFF, 0xFF));
pen.setWidth(2);
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawEllipse(selectedBox);
}
}
void HSVColorDial::mouseMoveEvent(QMouseEvent *event)
{
Q_D(HSVColorDial);
const auto metrics = ColorDialMetrics(width(), height());
float dx = event->x() - metrics.offsetX - metrics.size / 2.0f;
float dy = event->y() - metrics.size / 2.0f;
if (d->inDialDrag)
{
float h = hueFromXY(dx, dy);
d->h = h;
d->renderedInnerBox = QImage();
update();
QColor value = QColor::fromHsvF(d->h, d->s, d->v);
emit dragColor(value);
emit updateColor(value);
}
else if(d->inBoxDrag)
{
float s = event->x() - metrics.offsetX - d->boxRect.x();
s = 1.0f - qBound(0.0f, s, float(d->boxRect.width())) / d->boxRect.width();
s = powf(s, BOX_VALUE_FACTOR);
float v = event->y() - d->boxRect.y();
v = 1.0f - qBound(0.0f, v, float(d->boxRect.height())) / d->boxRect.height();
v = powf(v, BOX_VALUE_FACTOR);
d->s = s;
d->v = v;
update();
QColor value = QColor::fromHsvF(d->h, d->s, d->v);
emit dragColor(value);
emit updateColor(value);
}
}
void HSVColorDial::mousePressEvent(QMouseEvent *event)
{
Q_D(HSVColorDial);
const auto metrics = ColorDialMetrics(width(), height());
float dx = event->x() - metrics.offsetX - metrics.size / 2.0f;
float dy = event->y() - metrics.size / 2.0f;
float r = sqrtf(dx*dx + dy*dy);
if (r <= metrics.outerRadius && r >= metrics.innerRadius)
{
float h = hueFromXY(dx, dy);
d->h = h;
d->renderedInnerBox = QImage();
update();
d->inDialDrag = true;
d->inBoxDrag = false;
QColor value = QColor::fromHsvF(d->h, d->s, d->v);
emit dragColor(value);
emit updateColor(value);
}
else if (d->boxRect.contains(event->pos()))
{
float s = event->x() - metrics.offsetX - d->boxRect.x();
s = 1.0f - qBound(0.0f, s, float(d->boxRect.width())) / d->boxRect.width();
s = powf(s, BOX_VALUE_FACTOR);
float v = event->y() - d->boxRect.y();
v = 1.0f - qBound(0.0f, v, float(d->boxRect.height())) / d->boxRect.height();
v = powf(v, BOX_VALUE_FACTOR);
d->s = s;
d->v = v;
update();
d->inDialDrag = false;
d->inBoxDrag = true;
QColor value = QColor::fromHsvF(d->h, d->s, d->v);
emit dragColor(value);
emit updateColor(value);
}
}
void HSVColorDial::mouseReleaseEvent(QMouseEvent *event)
{
Q_D(HSVColorDial);
const auto metrics = ColorDialMetrics(width(), height());
float dx = event->x() - metrics.offsetX - metrics.size / 2.0f;
float dy = event->y() - metrics.size / 2.0f;
if (d->inDialDrag)
{
float h = hueFromXY(dx, dy);
// qDebug() << h << QColor::fromHsvF(h, 1, 1);
d->h = h;
d->renderedInnerBox = QImage();
update();
QColor value = QColor::fromHsvF(d->h, d->s, d->v);
emit releaseColor(value);
emit updateColor(value);
}
else if (d->inBoxDrag)
{
QColor value = QColor::fromHsvF(d->h, d->s, d->v);
emit releaseColor(value);
emit updateColor(value);
}
d->inDialDrag = false;
d->inBoxDrag = false;
}