-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtcolortriangle.cpp
1512 lines (1298 loc) · 46.8 KB
/
qtcolortriangle.cpp
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation ([email protected])
**
** This file is part of a Qt Solutions component.
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Solutions Commercial License Agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** Please note Third Party Software included with Qt Solutions may impose
** additional restrictions and it is the user's responsibility to ensure
** that they have met the licensing requirements of the GPL, LGPL, or Qt
** Solutions Commercial license and the relevant license of the Third
** Party Software they are using.
**
** If you are unsure which license is appropriate for your use, please
** contact Nokia at [email protected].
**
****************************************************************************/
#include "qtcolortriangle.h"
#include <QEvent>
#include <QMap>
#include <QVarLengthArray>
#include <QConicalGradient>
#include <QFrame>
#include <QImage>
#include <QKeyEvent>
#include <QLayout>
#include <QMouseEvent>
#include <QPainter>
#include <QPainterPath>
#include <QPixmap>
#include <QResizeEvent>
#include <QToolTip>
#include <QVBoxLayout>
#include <math.h>
/*! \class QtColorTriangle
\brief The QtColorTriangle class provides a triangular color
selection widget.
This widget uses the HSV color model, and is therefore useful for
selecting colors by eye.
The triangle in the center of the widget is used for selecting
saturation and value, and the surrounding circle is used for
selecting hue.
Use setColor() and color() to set and get the current color.
\img colortriangle.png
*/
/*! \fn QtColorTriangle::colorChanged(const QColor &color)
Whenever the color triangles color changes this signal is emitted
with the new \a color.
*/
const double PI = 3.14159265358979323846264338327950288419717;
const double TWOPI = 2.0*PI;
/*
Used to store color values in the range 0..255 as doubles.
*/
struct DoubleColor
{
double r, g, b;
DoubleColor() : r(0.0), g(0.0), b(0.0) {}
DoubleColor(double red, double green, double blue) : r(red), g(green), b(blue) {}
DoubleColor(const DoubleColor &c) : r(c.r), g(c.g), b(c.b) {}
};
/*
Used to store pairs of DoubleColor and DoublePoint in one structure.
*/
struct Vertex {
DoubleColor color;
QPointF point;
Vertex(const DoubleColor &c, const QPointF &p) : color(c), point(p) {}
Vertex(const QColor &c, const QPointF &p)
: color(DoubleColor((double) c.red(), (double) c.green(),
(double) c.blue())), point(p) {}
};
/*! \internal
Swaps the Vertex at *a with the one at *b.
*/
static void swap(Vertex **a, Vertex **b)
{
Vertex *tmp = *a;
*a = *b;
*b = tmp;
}
/*!
Constructs a color triangle widget with the given \a parent.
*/
QtColorTriangle::QtColorTriangle(QWidget *parent)
: QWidget(parent), bg(sizeHint(), QImage::Format_RGB32), selMode(Idle)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
setFocusPolicy(Qt::StrongFocus);
mustGenerateBackground = true;
QColor tmp;
tmp.setHsv(76, 184, 206);
setColor(tmp);
}
/*!
Destructs the color triangle.
*/
QtColorTriangle::~QtColorTriangle()
{
}
/*!
\internal
Generates the first background image.
*/
void QtColorTriangle::polish()
{
outerRadius = (contentsRect().width() - 1) / 2;
if ((contentsRect().height() - 1) / 2 < outerRadius)
outerRadius = (contentsRect().height() - 1) / 2;
penWidth = (int) floor(outerRadius / 50.0);
ellipseSize = (int) floor(outerRadius / 12.5);
double cx = (double) contentsRect().center().x();
double cy = (double) contentsRect().center().y();
pa = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 5.0))));
pb = QPointF(cx + (cos(b) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(b) * (outerRadius - (outerRadius / 5.0))));
pc = QPointF(cx + (cos(c) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(c) * (outerRadius - (outerRadius / 5.0))));
pd = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 10.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 10.0))));
// Find the current position of the selector
selectorPos = pointFromColor(curColor);
update();
}
/*! \reimp
*/
QSize QtColorTriangle::sizeHint() const
{
return QSize(100, 100);
}
/*!
Forces the triangle widget to always be square. Returns the value
\a w.
*/
int QtColorTriangle::heightForWidth(int w) const
{
return w;
}
/*!
\internal
Generates a new static background image. This function is called
initially, and in resizeEvent.
*/
void QtColorTriangle::genBackground()
{
// Find the inner radius of the hue donut.
double innerRadius = outerRadius - outerRadius / 5;
// Create an image of the same size as the contents rect.
bg = QImage(contentsRect().size(), QImage::Format_RGB32);
QPainter p(&bg);
p.setRenderHint(QPainter::HighQualityAntialiasing);
p.fillRect(bg.rect(), /*palette().mid()*/QBrush(QColor(53,53,53)));
QConicalGradient gradient(bg.rect().center(), 90);
QColor color;
for (double i = 0; i <= 1.0; i += 0.1) {
#if QT_VERSION < 0x040100
color.setHsv(int(i * 360.0), 255, 255);
#else
color.setHsv(int(360.0 - (i * 360.0)), 255, 255);
#endif
gradient.setColorAt(i, color);
}
QRectF innerRadiusRect(bg.rect().center().x() - innerRadius, bg.rect().center().y() - innerRadius,
innerRadius * 2 + 1, innerRadius * 2 + 1);
QRectF outerRadiusRect(bg.rect().center().x() - outerRadius, bg.rect().center().y() - outerRadius,
outerRadius * 2 + 1, outerRadius * 2 + 1);
QPainterPath path;
path.addEllipse(innerRadiusRect);
path.addEllipse(outerRadiusRect);
p.save();
p.setClipPath(path);
p.fillRect(bg.rect(), gradient);
p.restore();
double penThickness = bg.width() / 400.0;
for (int f = 0; f <= 5760; f += 20) {
int value = int((0.5 + cos(((f - 1800) / 5760.0) * TWOPI) / 2) * 255.0);
color.setHsv(int((f / 5760.0) * 360.0), 128 + (255 - value)/2, 255 - (255 - value)/4);
p.setPen(QPen(color, penThickness));
p.drawArc(innerRadiusRect, 1440 - f, 20);
color.setHsv(int((f / 5760.0) * 360.0), 128 + value/2, 255 - value/4);
p.setPen(QPen(color, penThickness));
p.drawArc(outerRadiusRect, 2880 - 1440 - f, 20);
}
return;
}
/*!
\internal
Selects new hue or saturation/value values, depending on where the
mouse button was pressed initially.
*/
void QtColorTriangle::mouseMoveEvent(QMouseEvent *e)
{
if ((e->buttons() & Qt::LeftButton) == 0)
return;
QPointF depos((double) e->pos().x(), (double) e->pos().y());
bool newColor = false;
if (selMode == SelectingHue) {
// If selecting hue, find the new angles for the points a,b,c
// of the triangle. The following update() will then redraw
// the triangle.
a = angleAt(depos, contentsRect());
b = a + TWOPI / 3.0;
c = b + TWOPI / 3.0;
if (b > TWOPI) b -= TWOPI;
if (c > TWOPI) c -= TWOPI;
double am = a - PI/2;
if (am < 0) am += TWOPI;
curHue = 360 - (int) (((am) * 360.0) / TWOPI);
int h,s,v;
curColor.getHsv(&h, &s, &v);
if (curHue != h) {
newColor = true;
curColor.setHsv(curHue, s, v);
}
double cx = (double) contentsRect().center().x();
double cy = (double) contentsRect().center().y();
pa = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 5.0))));
pb = QPointF(cx + (cos(b) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(b) * (outerRadius - (outerRadius / 5.0))));
pc = QPointF(cx + (cos(c) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(c) * (outerRadius - (outerRadius / 5.0))));
pd = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 10.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 10.0))));
selectorPos = pointFromColor(curColor);
} else {
Vertex aa(Qt::black, pa);
Vertex bb(Qt::black, pb);
Vertex cc(Qt::black, pc);
Vertex *p1 = &aa;
Vertex *p2 = &bb;
Vertex *p3 = &cc;
if (p1->point.y() > p2->point.y()) swap(&p1, &p2);
if (p1->point.y() > p3->point.y()) swap(&p1, &p3);
if (p2->point.y() > p3->point.y()) swap(&p2, &p3);
selectorPos = movePointToTriangle(depos.x(), depos.y(), aa, bb, cc);
QColor col = colorFromPoint(selectorPos);
if (col != curColor) {
// Ensure that hue does not change when selecting
// saturation and value.
int h,s,v;
col.getHsv(&h, &s, &v);
curColor.setHsv(curHue, s, v);
newColor = true;
}
}
if (newColor)
emit colorChanged(curColor);
update();
}
/*!
\internal
When the left mouse button is pressed, this function determines
what part of the color triangle the cursor is, and from that it
initiates either selecting the hue (outside the triangle's area)
or the saturation/value (inside the triangle's area).
*/
void QtColorTriangle::mousePressEvent(QMouseEvent *e)
{
// Only respond to the left mouse button.
if (e->button() != Qt::LeftButton)
return;
QPointF depos((double) e->pos().x(), (double) e->pos().y());
double rad = radiusAt(depos, contentsRect());
bool newColor = false;
// As in mouseMoveEvent, either find the a,b,c angles or the
// radian position of the selector, then order an update.
if (rad > (outerRadius - (outerRadius / 5))) {
selMode = SelectingHue;
a = angleAt(depos, contentsRect());
b = a + TWOPI / 3.0;
c = b + TWOPI / 3.0;
if (b > TWOPI) b -= TWOPI;
if (c > TWOPI) c -= TWOPI;
double am = a - PI/2;
if (am < 0) am += TWOPI;
curHue = 360 - (int) ((am * 360.0) / TWOPI);
int h,s,v;
curColor.getHsv(&h, &s, &v);
if (h != curHue) {
newColor = true;
curColor.setHsv(curHue, s, v);
}
double cx = (double) contentsRect().center().x();
double cy = (double) contentsRect().center().y();
pa = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 5.0))));
pb = QPointF(cx + (cos(b) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(b) * (outerRadius - (outerRadius / 5.0))));
pc = QPointF(cx + (cos(c) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(c) * (outerRadius - (outerRadius / 5.0))));
pd = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 10.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 10.0))));
selectorPos = pointFromColor(curColor);
emit colorChanged(curColor);
} else {
selMode = SelectingSatValue;
Vertex aa(Qt::black, pa);
Vertex bb(Qt::black, pb);
Vertex cc(Qt::black, pc);
Vertex *p1 = &aa;
Vertex *p2 = &bb;
Vertex *p3 = &cc;
if (p1->point.y() > p2->point.y()) swap(&p1, &p2);
if (p1->point.y() > p3->point.y()) swap(&p1, &p3);
if (p2->point.y() > p3->point.y()) swap(&p2, &p3);
selectorPos = movePointToTriangle(depos.x(), depos.y(), aa, bb, cc);
QColor col = colorFromPoint(selectorPos);
if (col != curColor) {
curColor = col;
newColor = true;
}
}
if (newColor)
emit colorChanged(curColor);
update();
}
/*!
\internal
Stops selecting of colors with the mouse.
*/
void QtColorTriangle::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
selMode = Idle;
}
/*!
\internal
*/
void QtColorTriangle::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Left: {
--curHue;
if (curHue < 0) curHue += 360;
int h,s,v;
curColor.getHsv(&h, &s, &v);
QColor tmp;
tmp.setHsv(curHue, s, v);
setColor(tmp);
}
break;
case Qt::Key_Right: {
++curHue;
if (curHue > 359) curHue -= 360;
int h,s,v;
curColor.getHsv(&h, &s, &v);
QColor tmp;
tmp.setHsv(curHue, s, v);
setColor(tmp);
}
break;
case Qt::Key_Up: {
int h,s,v;
curColor.getHsv(&h, &s, &v);
QColor tmp;
if (e->modifiers() & Qt::ShiftModifier) {
if (s > 5) s -= 5;
else s = 0;
} else {
if (v > 5) v -= 5;
else v = 0;
}
tmp.setHsv(curHue, s, v);
setColor(tmp);
}
break;
case Qt::Key_Down: {
int h,s,v;
curColor.getHsv(&h, &s, &v);
QColor tmp;
if (e->modifiers() & Qt::ShiftModifier) {
if (s < 250) s += 5;
else s = 255;
} else {
if (v < 250) v += 5;
else v = 255;
}
tmp.setHsv(curHue, s, v);
setColor(tmp);
}
break;
};
}
/*!
\internal
Regenerates the background image and sends an update.
*/
void QtColorTriangle::resizeEvent(QResizeEvent *)
{
outerRadius = (contentsRect().width() - 1) / 2;
if ((contentsRect().height() - 1) / 2 < outerRadius)
outerRadius = (contentsRect().height() - 1) / 2;
penWidth = (int) floor(outerRadius / 50.0);
ellipseSize = (int) floor(outerRadius / 12.5);
double cx = (double) contentsRect().center().x();
double cy = (double) contentsRect().center().y();
pa = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 5.0))));
pb = QPointF(cx + (cos(b) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(b) * (outerRadius - (outerRadius / 5.0))));
pc = QPointF(cx + (cos(c) * (outerRadius - (outerRadius / 5.0))),
cy - (sin(c) * (outerRadius - (outerRadius / 5.0))));
pd = QPointF(cx + (cos(a) * (outerRadius - (outerRadius / 10.0))),
cy - (sin(a) * (outerRadius - (outerRadius / 10.0))));
// Find the current position of the selector
selectorPos = pointFromColor(curColor);
mustGenerateBackground = true;
update();
}
/*! \reimp
First copies a background image of the hue donut and its
background color onto the frame, then draws the color triangle,
and finally the selectors.
*/
void QtColorTriangle::paintEvent(QPaintEvent *e)
{
QPainter p(this);
if (e->rect().intersects(contentsRect()))
p.setClipRegion(e->region().intersected(contentsRect()));
if (mustGenerateBackground) {
genBackground();
mustGenerateBackground = false;
}
// Blit the static generated background with the hue gradient onto
// the double buffer.
QImage buf = bg.copy();
// Draw the trigon
int h,s,v;
curColor.getHsv(&h, &s, &v);
// Find the color with only the hue, and max value and saturation
QColor hueColor;
hueColor.setHsv(curHue, 255, 255);
// Draw the triangle
drawTrigon(&buf, pa, pb, pc, hueColor);
// Slow step: convert the image to a pixmap
QPixmap pix = QPixmap::fromImage(buf);
QPainter painter(&pix);
painter.setRenderHint(QPainter::Antialiasing);
// Draw an outline of the triangle
QColor halfAlpha(0, 0, 0, 128);
painter.setPen(QPen(halfAlpha, 0));
painter.drawLine(pa, pb);
painter.drawLine(pb, pc);
painter.drawLine(pc, pa);
int ri, gi, bi;
hueColor.getRgb(&ri, &gi, &bi);
if ((ri * 30) + (gi * 59) + (bi * 11) > 12800)
painter.setPen(QPen(Qt::black, penWidth));
else
painter.setPen(QPen(Qt::white, penWidth));
painter.drawEllipse((int) (pd.x() - ellipseSize / 2.0),
(int) (pd.y() - ellipseSize / 2.0),
ellipseSize, ellipseSize);
curColor.getRgb(&ri, &gi, &bi);
// Find a color for painting the selector based on the brightness
// value of the color.
if ((ri * 30) + (gi * 59) + (bi * 11) > 12800)
painter.setPen(QPen(Qt::black, penWidth));
else
painter.setPen(QPen(Qt::white, penWidth));
// Draw the selector ellipse.
painter.drawEllipse(QRectF(selectorPos.x() - ellipseSize / 2.0,
selectorPos.y() - ellipseSize / 2.0,
ellipseSize + 0.5, ellipseSize + 0.5));
// Blit
p.drawPixmap(contentsRect().topLeft(), pix);
}
/*! \internal
Draws a trigon (polygon with three corners \a pa, \a pb and \a pc
and three edges), using \a painter.
Fills the trigon with a gradient, where the \a pa point has the
color \a color, \a pb is black and \a bc is white. Bilinear
gradient.
*/
void QtColorTriangle::drawTrigon(QImage *buf, const QPointF &pa,
const QPointF &pb, const QPointF &pc,
const QColor &color)
{
// Create three Vertex objects. A Vertex contains a double-point
// coordinate and a color.
// pa is the tip of the arrow
// pb is the black corner
// pc is the white corner
Vertex aa(color, pa);
Vertex bb(Qt::black, pb);
Vertex cc(Qt::white, pc);
// Sort. Make p1 above p2, which is above p3 (using y coordinate).
// Bubble sorting is fastest here.
Vertex *p1 = &aa;
Vertex *p2 = &bb;
Vertex *p3 = &cc;
if (p1->point.y() > p2->point.y()) swap(&p1, &p2);
if (p1->point.y() > p3->point.y()) swap(&p1, &p3);
if (p2->point.y() > p3->point.y()) swap(&p2, &p3);
// All the three y deltas are >= 0
double p1p2ydist = p2->point.y() - p1->point.y();
double p1p3ydist = p3->point.y() - p1->point.y();
double p2p3ydist = p3->point.y() - p2->point.y();
double p1p2xdist = p2->point.x() - p1->point.x();
double p1p3xdist = p3->point.x() - p1->point.x();
double p2p3xdist = p3->point.x() - p2->point.x();
// The first x delta decides wether we have a lefty or a righty
// trigon.
bool lefty = p1p2xdist < 0;
// Left and right colors and X values. The key in this map is the
// y values. Our goal is to fill these structures with all the
// information needed to do a single pass top-to-bottom,
// left-to-right drawing of the trigon.
QVarLengthArray<DoubleColor, 2000> leftColors;
QVarLengthArray<DoubleColor, 2000> rightColors;
QVarLengthArray<double, 2000> leftX;
QVarLengthArray<double, 2000> rightX;
leftColors.resize(int(floor(p3->point.y() + 1)));
rightColors.resize(int(floor(p3->point.y() + 1)));
leftX.resize(int(floor(p3->point.y() + 1)));
rightX.resize(int(floor(p3->point.y() + 1)));
// Scan longy - find all left and right colors and X-values for
// the tallest edge (p1-p3).
DoubleColor source;
DoubleColor dest;
double r, g, b;
double rdelta, gdelta, bdelta;
double x;
double xdelta;
int y1, y2;
// Initialize with known values
x = p1->point.x();
source = p1->color;
dest = p3->color;
r = source.r;
g = source.g;
b = source.b;
y1 = (int) floor(p1->point.y());
y2 = (int) floor(p3->point.y());
// Find slopes (notice that if the y dists are 0, we don't care
// about the slopes)
xdelta = p1p3ydist == 0.0 ? 0.0 : p1p3xdist / p1p3ydist;
rdelta = p1p3ydist == 0.0 ? 0.0 : (dest.r - r) / p1p3ydist;
gdelta = p1p3ydist == 0.0 ? 0.0 : (dest.g - g) / p1p3ydist;
bdelta = p1p3ydist == 0.0 ? 0.0 : (dest.b - b) / p1p3ydist;
// Calculate gradients using linear approximation
int y;
for (y = y1; y < y2; ++y) {
if (lefty) {
rightColors[y] = DoubleColor(r, g, b);
rightX[y] = x;
} else {
leftColors[y] = DoubleColor(r, g, b);
leftX[y] = x;
}
r += rdelta;
g += gdelta;
b += bdelta;
x += xdelta;
}
// Scan top shorty - find all left and right colors and x-values
// for the topmost of the two not-tallest short edges.
x = p1->point.x();
source = p1->color;
dest = p2->color;
r = source.r;
g = source.g;
b = source.b;
y1 = (int) floor(p1->point.y());
y2 = (int) floor(p2->point.y());
// Find slopes (notice that if the y dists are 0, we don't care
// about the slopes)
xdelta = p1p2ydist == 0.0 ? 0.0 : p1p2xdist / p1p2ydist;
rdelta = p1p2ydist == 0.0 ? 0.0 : (dest.r - r) / p1p2ydist;
gdelta = p1p2ydist == 0.0 ? 0.0 : (dest.g - g) / p1p2ydist;
bdelta = p1p2ydist == 0.0 ? 0.0 : (dest.b - b) / p1p2ydist;
// Calculate gradients using linear approximation
for (y = y1; y < y2; ++y) {
if (lefty) {
leftColors[y] = DoubleColor(r, g, b);
leftX[y] = x;
} else {
rightColors[y] = DoubleColor(r, g, b);
rightX[y] = x;
}
r += rdelta;
g += gdelta;
b += bdelta;
x += xdelta;
}
// Scan bottom shorty - find all left and right colors and
// x-values for the bottommost of the two not-tallest short edges.
x = p2->point.x();
source = p2->color;
dest = p3->color;
r = source.r;
g = source.g;
b = source.b;
y1 = (int) floor(p2->point.y());
y2 = (int) floor(p3->point.y());
// Find slopes (notice that if the y dists are 0, we don't care
// about the slopes)
xdelta = p2p3ydist == 0.0 ? 0.0 : p2p3xdist / p2p3ydist;
rdelta = p2p3ydist == 0.0 ? 0.0 : (dest.r - r) / p2p3ydist;
gdelta = p2p3ydist == 0.0 ? 0.0 : (dest.g - g) / p2p3ydist;
bdelta = p2p3ydist == 0.0 ? 0.0 : (dest.b - b) / p2p3ydist;
// Calculate gradients using linear approximation
for (y = y1; y < y2; ++y) {
if (lefty) {
leftColors[y] = DoubleColor(r, g, b);
leftX[y] = x;
} else {
rightColors[y] = DoubleColor(r, g, b);
rightX[y] = x;
}
r += rdelta;
g += gdelta;
b += bdelta;
x += xdelta;
}
// Inner loop. For each y in the left map of x-values, draw one
// line from left to right.
const int p3yfloor = int(floor(p3->point.y()));
for (int y = int(floor(p1->point.y())); y < p3yfloor; ++y) {
double lx = leftX[y];
double rx = rightX[y];
int lxi = (int) floor(lx);
int rxi = (int) floor(rx);
DoubleColor rc = rightColors[y];
DoubleColor lc = leftColors[y];
// if the xdist is 0, don't draw anything.
double xdist = rx - lx;
if (xdist != 0.0) {
double r = lc.r;
double g = lc.g;
double b = lc.b;
double rdelta = (rc.r - r) / xdist;
double gdelta = (rc.g - g) / xdist;
double bdelta = (rc.b - b) / xdist;
QRgb *scanline = reinterpret_cast<QRgb *>(buf->scanLine(y));
scanline += lxi;
// Inner loop 2. Draws the line from left to right.
for (int i = lxi; i < rxi; ++i) {
*scanline++ = qRgb((int) r, (int) g, (int) b);
r += rdelta;
g += gdelta;
b += bdelta;
}
}
}
}
/*! \internal
Sets the color of the triangle to \a col.
*/
void QtColorTriangle::setColor(const QColor &col)
{
if (col == curColor)
return;
curColor = col;
int h, s, v;
curColor.getHsv(&h, &s, &v);
// Never use an invalid hue to display colors
if (h != -1)
curHue = h;
a = (((360 - curHue) * TWOPI) / 360.0);
a += PI / 2.0;
if (a > TWOPI) a -= TWOPI;
b = a + TWOPI/3;
c = b + TWOPI/3;
if (b > TWOPI) b -= TWOPI;
if (c > TWOPI) c -= TWOPI;
double cx = (double) contentsRect().center().x();
double cy = (double) contentsRect().center().y();
double innerRadius = outerRadius - (outerRadius / 5.0);
double pointerRadius = outerRadius - (outerRadius / 10.0);
pa = QPointF(cx + (cos(a) * innerRadius), cy - (sin(a) * innerRadius));
pb = QPointF(cx + (cos(b) * innerRadius), cy - (sin(b) * innerRadius));
pc = QPointF(cx + (cos(c) * innerRadius), cy - (sin(c) * innerRadius));
pd = QPointF(cx + (cos(a) * pointerRadius), cy - (sin(a) * pointerRadius));
selectorPos = pointFromColor(curColor);
update();
emit colorChanged(curColor);
}
/*! \internal
Returns the current color of the triangle.
*/
QColor QtColorTriangle::color() const
{
return curColor;
}
/*!
\internal
Returns the distance from \a pos to the center of \a rect.
*/
double QtColorTriangle::radiusAt(const QPointF &pos, const QRect &rect) const
{
double mousexdist = pos.x() - (double) rect.center().x();
double mouseydist = pos.y() - (double) rect.center().y();
return sqrt(mousexdist * mousexdist + mouseydist * mouseydist);
}
/*!
\internal
With origin set to the center of \a rect, this function returns
the angle in radians between the line that starts at (0,0) and
ends at (1,0) and the line that stars at (0,0) and ends at \a pos.
*/
double QtColorTriangle::angleAt(const QPointF &pos, const QRect &rect) const
{
double mousexdist = pos.x() - (double) rect.center().x();
double mouseydist = pos.y() - (double) rect.center().y();
double mouserad = sqrt(mousexdist * mousexdist + mouseydist * mouseydist);
if (mouserad == 0.0)
return 0.0;
double angle = acos(mousexdist / mouserad);
if (mouseydist >= 0)
angle = TWOPI - angle;
return angle;
}
/*! \internal
Returns a * a.
*/
inline double qsqr(double a)
{
return a * a;
}
/*! \internal
Returns the length of the vector (x,y).
*/
inline double vlen(double x, double y)
{
return sqrt(qsqr(x) + qsqr(y));
}
/*! \internal
Returns the vector product of (x1,y1) and (x2,y2).
*/
inline double vprod(double x1, double y1, double x2, double y2)
{
return x1 * x2 + y1 * y2;
}
/*! \internal
Returns true if the point cos(p),sin(p) is on the arc between
cos(a1),sin(a1) and cos(a2),sin(a2); otherwise returns false.
*/
bool angleBetweenAngles(double p, double a1, double a2)
{
if (a1 > a2) {
a2 += TWOPI;
if (p < PI) p += TWOPI;
}
return p >= a1 && p < a2;
}
/*! \internal
A line from a to b is one of several lines in an equilateral
polygon, and they are drawn counter clockwise. This line therefore
has one side facing in and one facing out of the polygon. This
function determines wether (x,y) is on the inside or outside of
the given line, defined by the "from" coordinate (ax,ay) and the
"to" coordinate (bx,by).
The point (px,py) is the intersection between the a-b line and the
perpendicular projection of (x,y) onto that line.
Returns true if (x,y) is above the line; otherwise returns false.
If ax and bx are equal and ay and by are equal (line is a point),
this function will return true if (x,y) is equal to this point.
*/
static bool pointAbovePoint(double x, double y, double px, double py,
double ax, double ay, double bx, double by)
{
bool result = false;
if (floor(ax) > floor(bx)) {
if (floor(ay) < floor(by)) {
// line is draw upright-to-downleft
if (floor(x) < floor(px) || floor(y) < floor(py))
result = true;
} else if (floor(ay) > floor(by)) {
// line is draw downright-to-upleft
if (floor(x) > floor(px) || floor(y) < floor(py))
result = true;
} else {
// line is flat horizontal
if (y < ay) result = true;
}
} else if (floor(ax) < floor(bx)) {
if (floor(ay) < floor(by)) {
// line is draw upleft-to-downright
if (floor(x) < floor(px) || floor(y) > floor(py))
result = true;
} else if (floor(ay) > floor(by)) {
// line is draw downleft-to-upright
if (floor(x) > floor(px) || floor(y) > floor(py))
result = true;
} else {
// line is flat horizontal
if (y > ay)
result = true;
}
} else {
// line is vertical
if (floor(ay) < floor(by)) {
if (x < ax) result = true;
} else if (floor(ay) > floor(by)) {
if (x > ax) result = true;
} else {
if (!(x == ax && y == ay))
result = true;
}
}
return result;
}
/*! \internal
if (ax,ay) to (bx,by) describes a line, and (x,y) is a point on
that line, returns -1 if (x,y) is outside the (ax,ay) bounds, 1 if
it is outside the (bx,by) bounds and 0 if (x,y) is within (ax,ay)
and (bx,by).
*/
static int pointInLine(double x, double y, double ax, double ay,
double bx, double by)
{
if (ax > bx) {