-
-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathColorWheel.cpp
More file actions
568 lines (477 loc) · 17.2 KB
/
ColorWheel.cpp
File metadata and controls
568 lines (477 loc) · 17.2 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*-----------------------------------------------------*\
| ColorWheel.cpp |
| |
| Color wheel selector widget for Qt |
| |
| Original: https://github.com/liuyanghejerry/Qt-Plus |
| |
| Modified by Adam Honse (calcprogrammer1@gmail.com) |
\*-----------------------------------------------------*/
#include "ColorWheel.h"
#include <QPainter>
#include <QResizeEvent>
#include <QStyleOption>
#include <QtCore/qmath.h>
#include <QDebug>
#include <QPainterPath>
ColorWheel::ColorWheel(QWidget *parent) :
QWidget(parent),
initSize(128,128),
mouseDown(false),
margin(0),
wheelWidth(10),
current(Qt::red),
inWheel(false),
inSquare(false)
{
current = current.toHsv();
}
QColor ColorWheel::color()
{
return current;
}
void ColorWheel::setColor(const QColor &color)
{
if(color == current) return;
if(color.hue() != current.hue())
{
hueChanged(color.hue());
}
if((color.saturation() != current.saturation()) || (color.value() != current.value()))
{
svChanged(color);
}
update();
emit colorChanged(color);
}
QColor ColorWheel::posColor(const QPoint &point)
{
/*-----------------------------------------------------*\
| Subtract offsets from point value |
\*-----------------------------------------------------*/
int point_x = point.x() - x_offset;
int point_y = point.y() - y_offset;
/*-----------------------------------------------------*\
| If point is not within widget, don't process |
\*-----------------------------------------------------*/
if(!wheel.rect().contains(point))
{
return QColor();
}
/*-----------------------------------------------------*\
| If within wheel region, update hue from point |
| position |
\*-----------------------------------------------------*/
if(inWheel)
{
qreal hue = 0;
int r = qMin(width() - x_offset, height() - y_offset) / 2;
if( point_x > r )
{
if(point_y < r )
{
//1
hue = 90 - (qAtan2( (point_x - r) , (r - point_y) ) / 3.14 / 2 * 360);
}
else
{
//4
hue = 270 + (qAtan2( (point_x - r) , (point_y - r ) ) / 3.14 / 2 * 360);
}
}
else
{
if(point_y < r )
{
//2
hue = 90 + (qAtan2( (r - point_x) , (r - point_y) ) / 3.14 / 2 * 360);
}
else
{
//3
hue = 270 - (qAtan2( (r - point_x) , (point_y - r )) / 3.14 / 2 * 360);
}
}
/*-----------------------------------------------------*\
| Restrict hue to range 0-359 |
\*-----------------------------------------------------*/
hue = (hue > 359) ? 359 : hue;
hue = hue < 0 ? 0 : hue;
return QColor::fromHsv(hue,
current.saturation(),
current.value());
}
/*-----------------------------------------------------*\
| If within square region, update saturation and value |
| from point position |
\*-----------------------------------------------------*/
if(inSquare)
{
// region of the widget
int w = qMin(width() - x_offset, height() - y_offset);
// radius of outer circle
qreal r = w/2 - margin;
// radius of inner circle
qreal ir = r - wheelWidth;
// left corner of square
qreal m = w/2.0 - ir/qSqrt(2);
QPoint p = point - QPoint(x_offset, y_offset) - QPoint(m, m);
qreal SquareWidth = 2*ir/qSqrt(2);
return QColor::fromHsvF( current.hueF(),
p.x()/SquareWidth,
p.y()/SquareWidth);
}
return QColor();
}
QSize ColorWheel::sizeHint () const
{
return QSize(height(),height());
}
QSize ColorWheel::minimumSizeHint () const
{
return initSize;
}
void ColorWheel::mousePressEvent(QMouseEvent *event)
{
/*-----------------------------------------------------*\
| Update last position |
\*-----------------------------------------------------*/
lastPos = event->pos();
/*-----------------------------------------------------*\
| If mouse is within wheel region, process wheel (hue) |
\*-----------------------------------------------------*/
if(wheelRegion.contains(lastPos))
{
inWheel = true;
inSquare = false;
QColor color = posColor(lastPos);
hueChanged(color.hue());
}
/*-----------------------------------------------------*\
| If mouse is within square region, process square |
| (saturation and value) |
\*-----------------------------------------------------*/
else if(squareRegion.contains(lastPos))
{
inWheel = false;
inSquare = true;
QColor color = posColor(lastPos);
svChanged(color);
}
/*-----------------------------------------------------*\
| Set the mouse down flag |
\*-----------------------------------------------------*/
mouseDown = true;
}
void ColorWheel::mouseMoveEvent(QMouseEvent *event)
{
/*-----------------------------------------------------*\
| Update last position |
\*-----------------------------------------------------*/
lastPos = event->pos();
/*-----------------------------------------------------*\
| Don't process if mouse button is not down |
\*-----------------------------------------------------*/
if(!mouseDown)
{
return;
}
/*-----------------------------------------------------*\
| If mouse is within wheel region, process wheel (hue) |
\*-----------------------------------------------------*/
if(wheelRegion.contains(lastPos) && inWheel)
{
QColor color = posColor(lastPos);
hueChanged(color.hue());
}
/*-----------------------------------------------------*\
| If mouse is within square region, process square |
| (saturation and value) |
\*-----------------------------------------------------*/
else if(squareRegion.contains(lastPos) && inSquare)
{
QColor color = posColor(lastPos);
svChanged(color);
}
else
{
/*-----------------------------------------------------*\
| If mouse is outside of wheel region, set lastPos to |
| corresponding values inside the wheel region |
\*-----------------------------------------------------*/
if(inWheel)
{
QPoint center = wheelRegion.boundingRect().center();
int radius = center.x() - wheelRegion.boundingRect().bottomLeft().x();
int xInWheel = lastPos.x() - center.x();
int yInWheel = lastPos.y() - center.y();
int centerToMouse = sqrt
(
qPow((int)xInWheel, 2) +
qPow((int)yInWheel, 2)
+1 //the sqrt function causes a crash when it's 0, so +1 prevents this with no noticeable precision difference and no performance penalty
);
xInWheel = radius * xInWheel / centerToMouse;
yInWheel = radius * yInWheel / centerToMouse;
lastPos.setX(xInWheel + center.x());
lastPos.setY(yInWheel + center.y());
QColor color = posColor(lastPos);
hueChanged(color.hue());
}
else
/*-----------------------------------------------------*\
| If mouse is outside of square region, set lastPos to |
| corresponding values inside the square region |
\*-----------------------------------------------------*/
if(inSquare)
{
int xInSquare = lastPos.x() - squareRegion.boundingRect().bottomLeft().x();
int maxX = squareRegion.boundingRect().topRight().x() - squareRegion.boundingRect().bottomLeft().x();
int yInSquare = lastPos.y() - squareRegion.boundingRect().topRight().y();
int maxY = squareRegion.boundingRect().bottomLeft().y() - squareRegion.boundingRect().topRight().y();
if(xInSquare < 0)
{
lastPos.setX(squareRegion.boundingRect().bottomLeft().x());
}
else if(xInSquare >= maxX)
{
lastPos.setX(squareRegion.boundingRect().topRight().x());
}
if(yInSquare < 0)
{
lastPos.setY(squareRegion.boundingRect().topRight().y());
}
else if (yInSquare >= maxY)
{
lastPos.setY(squareRegion.boundingRect().bottomLeft().y());
}
QColor color = posColor(lastPos);
svChanged(color);
}
}
}
void ColorWheel::mouseReleaseEvent(QMouseEvent *)
{
/*-----------------------------------------------------*\
| Clear mouse down and in-region flags |
\*-----------------------------------------------------*/
mouseDown = false;
inWheel = false;
inSquare = false;
}
void ColorWheel::resizeEvent(QResizeEvent *event)
{
unsigned int size = 0;
if(event->size().width() < event->size().height())
{
size = event->size().width();
}
else
{
size = event->size().height();
}
wheelWidth = 0.1 * size;
wheel = QPixmap(event->size());
wheel.fill(Qt::transparent);
drawWheelImage(event->size());
drawSquareImage(current.hue());
update();
}
void ColorWheel::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QStyleOption opt;
opt.initFrom(this);
composeWheel();
painter.drawPixmap(0,0,wheel);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}
void ColorWheel::drawWheelImage(const QSize &newSize)
{
/*-----------------------------------------------------*\
| Create image canvas |
\*-----------------------------------------------------*/
wheelImage = QImage(newSize, QImage::Format_ARGB32_Premultiplied);
/*-----------------------------------------------------*\
| Paint the background |
\*-----------------------------------------------------*/
wheelImage.fill(Qt::transparent);
/*-----------------------------------------------------*\
| Create rainbow gradient for wheel |
\*-----------------------------------------------------*/
QConicalGradient conicalGradient(0, 0, 0);
conicalGradient.setColorAt(0.0, Qt::red);
conicalGradient.setColorAt(60.0 / 360.0, Qt::yellow);
conicalGradient.setColorAt(120.0 / 360.0, Qt::green);
conicalGradient.setColorAt(180.0 / 360.0, Qt::cyan);
conicalGradient.setColorAt(240.0 / 360.0, Qt::blue);
conicalGradient.setColorAt(300.0 / 360.0, Qt::magenta);
conicalGradient.setColorAt(1.0, Qt::red);
/*-----------------------------------------------------*\
| Set up painter with antialiasing |
\*-----------------------------------------------------*/
QPainter painter(&wheelImage);
painter.setRenderHint(QPainter::Antialiasing);
/*-----------------------------------------------------*\
| Paint the wheel |
\*-----------------------------------------------------*/
int size = qMin(newSize.width(), newSize.height());
x_offset = (newSize.width() - size) / 2;
y_offset = (newSize.height() - size) / 2;
int r = size;
QPainterPath painterpath;
painterpath.addEllipse(QPoint(0,0),r/2-margin,r/2-margin);
painterpath.addEllipse(QPoint(0,0),r/2-margin-wheelWidth,r/2-margin-wheelWidth);
painter.translate(x_offset + (size / 2), y_offset + (size / 2));
QBrush brush(conicalGradient);
painter.setPen(Qt::NoPen);
painter.setBrush(brush);
painter.drawPath(painterpath);
/*-----------------------------------------------------*\
| Calculate wheel region and subtract out the inner |
| region |
\*-----------------------------------------------------*/
wheelRegion = QRegion(r/2, r/2, r-2*margin, r-2*margin, QRegion::Ellipse);
wheelRegion.translate(x_offset - (r-2*margin)/2, y_offset - (r-2*margin)/2);
int tmp = 2*(margin+wheelWidth);
QRegion subRe( r/2, r/2, r-tmp, r-tmp, QRegion::Ellipse );
subRe.translate( x_offset - (r-tmp)/2, y_offset - (r-tmp)/2);
wheelRegion -= subRe;
CleanWheel = QPixmap().fromImage(wheelImage);
}
void ColorWheel::drawSquareImage(const int &hue)
{
// QPainter painter(&squarePixmap);
// painter.setRenderHint(QPainter::Antialiasing);
/*-----------------------------------------------------*\
| Calculate dimensions |
\*-----------------------------------------------------*/
int w = qMin(width(), height());
// radius of outer circle
qreal r = w/2-margin;
// radius of inner circle
qreal ir = r-wheelWidth;
// left corner of square
qreal m = w/2.0-ir/qSqrt(2);
/*-----------------------------------------------------*\
| Create image canvas |
\*-----------------------------------------------------*/
QImage square(255,255, QImage::Format_ARGB32_Premultiplied);
/*-----------------------------------------------------*\
| Paint the square. X axis is saturation and Y axis is |
| value |
\*-----------------------------------------------------*/
QColor color;
QRgb qrgb;
for(int x = 0; x < 255; x++)
{
for(int y = 0; y < 255; y++)
{
color = QColor::fromHsv(hue, x, y);
qrgb = qRgb(color.red(),color.green(),color.blue());
square.setPixel(x, y, qrgb);
}
}
/*-----------------------------------------------------*\
| Copy the fixed-size square image on to the scaled |
| canvas |
\*-----------------------------------------------------*/
qreal SquareWidth = 2*ir/qSqrt(2);
squareImage = square.scaled(SquareWidth, SquareWidth);
/*-----------------------------------------------------*\
| Calculate square region |
\*-----------------------------------------------------*/
squareRegion = QRegion(x_offset + m, y_offset + m, SquareWidth, SquareWidth);
CleanSquare = squareImage;
}
void ColorWheel::drawIndicator(const int &hue)
{
QPainter painter(&wheel);
painter.setRenderHint(QPainter::Antialiasing);
if(hue > 20 && hue < 200)
{
painter.setPen(Qt::black);
}
else
{
painter.setPen(Qt::white);
}
painter.setBrush(Qt::NoBrush);
QPen pen = painter.pen();
pen.setWidth(3);
painter.setPen(pen);
qreal r = qMin(height(), width()) / 2.0;
painter.translate(x_offset + r, y_offset + r);
painter.rotate( -hue );
r = qMin(height(), width()) / 2.0 - margin - wheelWidth/2;
painter.drawEllipse(QPointF(r,0.0),5,5);
}
void ColorWheel::drawPicker(const QColor &color)
{
QPainter painter(&wheel);
painter.setRenderHint(QPainter::Antialiasing);
QPen pen;
// region of the widget
int w = qMin(width(), height());
// radius of outer circle
qreal r = w/2-margin;
// radius of inner circle
qreal ir = r-wheelWidth;
// left corner of square
qreal m = w/2.0-ir/qSqrt(2);
painter.translate(x_offset + m-5, y_offset + m-5);
qreal SquareWidth = 2*ir/qSqrt(2);
qreal S = color.saturationF()*SquareWidth;
qreal V = color.valueF()*SquareWidth;
if(color.saturation() > 30 ||color.value() < 50)
{
pen.setColor(Qt::white);
}
pen.setWidth(3);
painter.setPen(pen);
painter.drawEllipse(S,V,10,10);
}
void ColorWheel::composeWheel()
{
wheel = CleanWheel;
squareImage = CleanSquare;
QPainter composePainter(&wheel);
composePainter.drawImage(0, 0, wheelImage);
composePainter.drawImage(squareRegion.boundingRect().topLeft(), squareImage);
composePainter.end();
drawIndicator(current.hue());
drawPicker(current);
}
void ColorWheel::hueChanged(const int &hue)
{
if((hue < 0) || (hue > 359))
{
return;
}
int s = current.saturation();
int v = current.value();
current.setHsv(hue, s, v);
drawSquareImage(hue);
if(!isVisible())
{
return;
}
repaint();
emit colorChanged(current);
}
void ColorWheel::svChanged(const QColor &newcolor)
{
int hue = current.hue();
current.setHsv
(
hue,
newcolor.saturation(),
newcolor.value()
);
if(!isVisible())
{
return;
}
repaint();
emit colorChanged(current);
}