-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_triangle.py
1431 lines (1236 loc) · 48.6 KB
/
color_triangle.py
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
from enum import Enum
from math import floor, sqrt, sin, cos, acos, pi as PI
from Qt import QtWidgets, QtCore, QtGui
TWOPI = PI * 2
class TriangleState(Enum):
IdleState = object()
SelectingHueState = object()
SelectingSatValueState = object()
class DoubleColor:
def __init__(self, r, g=None, b=None):
if g is None:
g = r.g
b = r.b
r = r.r
self.r = float(r)
self.g = float(g)
self.b = float(b)
class Vertex:
def __init__(self, color, point):
# Convert GlobalColor to QColor as globals don't have red, green, blue
if isinstance(color, QtCore.Qt.GlobalColor):
color = QtGui.QColor(color)
# Convert QColor to DoubleColor
if isinstance(color, QtGui.QColor):
color = DoubleColor(color.red(), color.green(), color.blue())
self.color = color
self.point = point
class QtColorTriangle(QtWidgets.QWidget):
"""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 set_color() and color() to set and get the current color.
"""
color_changed = QtCore.Signal(QtGui.QColor)
# Thick of color wheel ratio where 1 is fully filled circle
inner_radius_ratio = 5.0
# Ratio where hue selector on wheel is relative to `inner_radius_ratio`
# - middle of the wheel is twice `inner_radius_ratio`
selector_radius_ratio = inner_radius_ratio * 2
# Size ratio of selectors on wheel and in triangle
ellipse_size_ratio = 10.0
# Ration of selectors thickness
ellipse_thick_ratio = 50.0
# Hue offset on color wheel (0 - 359)
# - red on top if set to "0"
hue_offset = 90
def __init__(self, parent=None):
super(QtColorTriangle, self).__init__(parent)
self.setSizePolicy(
QtWidgets.QSizePolicy.Minimum,
QtWidgets.QSizePolicy.Minimum
)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.angle_a = float()
self.angle_b = float()
self.angle_c = float()
self.bg_image = QtGui.QImage(
self.sizeHint(), QtGui.QImage.Format_RGB32
)
self.cur_color = QtGui.QColor()
self.point_a = QtCore.QPointF()
self.point_b = QtCore.QPointF()
self.point_c = QtCore.QPointF()
self.point_d = QtCore.QPointF()
self.cur_hue = int()
self.pen_width = int()
self.ellipse_size = int()
self.outer_radius = int()
self.selector_pos = QtCore.QPointF()
self.sel_mode = TriangleState.IdleState
self._triangle_outline_pen = QtGui.QPen(
QtGui.QColor(40, 40, 40, 128),
2
)
# Prepare hue numbers for color circle
_hue_circle_range = []
for idx in range(11):
# Some Qt versions may require:
# hue = int(idx * 360.0)
percent_idx = idx * 0.1
hue = int(360.0 - (percent_idx * 360.0))
_hue_circle_range.append((percent_idx, hue))
self._hue_circle_range = tuple(_hue_circle_range)
color = QtGui.QColor()
color.setHsv(0, 255, 255)
self.set_color(color)
def set_color(self, col):
if (
col.red() == self.cur_color.red()
and col.green() == self.cur_color.green()
and col.blue() == self.cur_color.blue()
):
return
self.cur_color = col
hue, *_ = self.cur_color.getHsv()
# Never use an invalid hue to display colors
if hue != -1:
self.cur_hue = hue
angle_with_offset = (360 - self.cur_hue - self.hue_offset) % 360
self.angle_a = (angle_with_offset * TWOPI) / 360.0
self.angle_a += PI / 2.0
if self.angle_a > TWOPI:
self.angle_a -= TWOPI
self.angle_b = self.angle_a + TWOPI / 3
self.angle_c = self.angle_b + TWOPI / 3
if self.angle_b > TWOPI:
self.angle_b -= TWOPI
if self.angle_c > TWOPI:
self.angle_c -= TWOPI
cx = float(self.contentsRect().center().x())
cy = float(self.contentsRect().center().y())
inner_radius = (
self.outer_radius
- (self.outer_radius / self.inner_radius_ratio)
)
selector_radius = (
self.outer_radius
- (self.outer_radius / self.selector_radius_ratio)
)
self.point_a = QtCore.QPointF(
cx + (cos(self.angle_a) * inner_radius),
cy - (sin(self.angle_a) * inner_radius)
)
self.point_b = QtCore.QPointF(
cx + (cos(self.angle_b) * inner_radius),
cy - (sin(self.angle_b) * inner_radius)
)
self.point_c = QtCore.QPointF(
cx + (cos(self.angle_c) * inner_radius),
cy - (sin(self.angle_c) * inner_radius)
)
self.point_d = QtCore.QPointF(
cx + (cos(self.angle_a) * selector_radius),
cy - (sin(self.angle_a) * selector_radius)
)
self.selector_pos = self._point_from_color(self.cur_color)
self.update()
self.color_changed.emit(self.cur_color)
def heightForWidth(self, width):
return width
def polish(self):
size_w = self.contentsRect().width()
size_h = self.contentsRect().height()
if size_w < size_h:
size = size_w
else:
size = size_h
self.outer_radius = (size - 1) / 2
self.pen_width = int(
floor(self.outer_radius / self.ellipse_thick_ratio)
)
self.ellipse_size = int(
floor(self.outer_radius / self.ellipse_size_ratio)
)
cx = float(self.contentsRect().center().x())
cy = float(self.contentsRect().center().y())
inner_radius = (
self.outer_radius
- (self.outer_radius / self.inner_radius_ratio)
)
selector_radius = (
self.outer_radius
- (self.outer_radius / self.selector_radius_ratio)
)
self.point_a = QtCore.QPointF(
cx + (cos(self.angle_a) * inner_radius),
cy - (sin(self.angle_a) * inner_radius)
)
self.point_b = QtCore.QPointF(
cx + (cos(self.angle_b) * inner_radius),
cy - (sin(self.angle_b) * inner_radius)
)
self.point_c = QtCore.QPointF(
cx + (cos(self.angle_c) * inner_radius),
cy - (sin(self.angle_c) * inner_radius)
)
self.point_d = QtCore.QPointF(
cx + (cos(self.angle_a) * selector_radius),
cy - (sin(self.angle_a) * selector_radius)
)
self.selector_pos = self._point_from_color(self.cur_color)
self.update()
def paintEvent(self, event):
painter = QtGui.QPainter(self)
if event.rect().intersects(self.contentsRect()):
event_region = event.region()
if hasattr(event_region, "intersect"):
clip_region = event_region.intersect(self.contentsRect())
else:
clip_region = event_region.intersected(
self.contentsRect()
)
painter.setClipRegion(clip_region)
self.paint_bg()
# Blit the static generated background with the hue gradient onto
# the double buffer.
buf = QtGui.QImage(self.bg_image.copy())
# Draw the trigon
# Find the color with only the hue, and max value and saturation
hue_color = QtGui.QColor()
hue_color.setHsv(self.cur_hue, 255, 255)
# Draw the triangle
self.drawTrigon(
buf, self.point_a, self.point_b, self.point_c, hue_color
)
# Slow step: convert the image to a pixmap
pix = QtGui.QPixmap.fromImage(buf)
pix_painter = QtGui.QPainter(pix)
pix_painter.setRenderHint(QtGui.QPainter.Antialiasing)
# Draw an outline of the triangle
pix_painter.setPen(self._triangle_outline_pen)
pix_painter.drawLine(self.point_a, self.point_b)
pix_painter.drawLine(self.point_b, self.point_c)
pix_painter.drawLine(self.point_c, self.point_a)
# Draw the color wheel selector
pix_painter.setPen(QtGui.QPen(QtCore.Qt.white, self.pen_width))
pix_painter.drawEllipse(
int(self.point_d.x() - self.ellipse_size / 2.0),
int(self.point_d.y() - self.ellipse_size / 2.0),
self.ellipse_size, self.ellipse_size
)
# Draw the triangle selector
pix_painter.setBrush(self.cur_color)
pix_painter.drawEllipse(
QtCore.QRectF(
self.selector_pos.x() - self.ellipse_size / 2.0,
self.selector_pos.y() - self.ellipse_size / 2.0,
self.ellipse_size + 0.5,
self.ellipse_size + 0.5
)
)
pix_painter.end()
# Blit
painter.drawPixmap(self.contentsRect().topLeft(), pix)
painter.end()
def mouseMoveEvent(self, event):
if (event.buttons() & QtCore.Qt.LeftButton) == 0:
return
depos = QtCore.QPointF(
event.pos().x(),
event.pos().y()
)
new_color = False
if self.sel_mode is TriangleState.SelectingHueState:
self.angle_a = self._angle_at(depos, self.contentsRect())
self.angle_b = self.angle_a + (TWOPI / 3.0)
self.angle_c = self.angle_b + (TWOPI / 3.0)
if self.angle_b > TWOPI:
self.angle_b -= TWOPI
if self.angle_c > TWOPI:
self.angle_c -= TWOPI
am = self.angle_a - (PI / 2)
if am < 0:
am += TWOPI
self.cur_hue = (
360 - int((am * 360.0) / TWOPI) - self.hue_offset
) % 360
hue, sat, val, _ = self.cur_color.getHsv()
if self.cur_hue != hue:
new_color = True
self.cur_color.setHsv(self.cur_hue, sat, val)
cx = float(self.contentsRect().center().x())
cy = float(self.contentsRect().center().y())
inner_radius = (
self.outer_radius
- (self.outer_radius / self.inner_radius_ratio)
)
selector_radius = (
self.outer_radius
- (self.outer_radius / self.selector_radius_ratio)
)
self.point_a = QtCore.QPointF(
cx + (cos(self.angle_a) * inner_radius),
cy - (sin(self.angle_a) * inner_radius)
)
self.point_b = QtCore.QPointF(
cx + (cos(self.angle_b) * inner_radius),
cy - (sin(self.angle_b) * inner_radius)
)
self.point_c = QtCore.QPointF(
cx + (cos(self.angle_c) * inner_radius),
cy - (sin(self.angle_c) * inner_radius)
)
self.point_d = QtCore.QPointF(
cx + (cos(self.angle_a) * selector_radius),
cy - (sin(self.angle_a) * selector_radius)
)
self.selector_pos = self._point_from_color(self.cur_color)
else:
aa = Vertex(QtCore.Qt.transparent, self.point_a)
bb = Vertex(QtCore.Qt.transparent, self.point_b)
cc = Vertex(QtCore.Qt.transparent, self.point_c)
self.selector_pos = self._move_point_to_triangle(
depos.x(), depos.y(), aa, bb, cc
)
col = self._color_from_point(self.selector_pos)
if col != self.cur_color:
# Ensure that hue does not change when selecting
# saturation and value.
_, sat, val, _ = col.getHsv()
self.cur_color.setHsv(self.cur_hue, sat, val)
new_color = True
if new_color:
self.color_changed.emit(self.cur_color)
self.update()
def mousePressEvent(self, event):
# Only respond to the left mouse button.
if event.button() != QtCore.Qt.LeftButton:
return
depos = QtCore.QPointF(
event.pos().x(),
event.pos().y()
)
rad = self._radius_at(depos, self.contentsRect())
new_color = False
# As in mouseMoveEvent, either find the a, b, c angles or the
# radian position of the selector, then order an update.
inner_radius = (
self.outer_radius - (self.outer_radius / self.inner_radius_ratio)
)
if rad > inner_radius:
self.sel_mode = TriangleState.SelectingHueState
self.angle_a = self._angle_at(depos, self.contentsRect())
self.angle_b = self.angle_a + TWOPI / 3.0
self.angle_c = self.angle_b + TWOPI / 3.0
if self.angle_b > TWOPI:
self.angle_b -= TWOPI
if self.angle_c > TWOPI:
self.angle_c -= TWOPI
am = self.angle_a - PI / 2
if am < 0:
am += TWOPI
self.cur_hue = (
360 - int((am * 360.0) / TWOPI) - self.hue_offset
) % 360
hue, sat, val, _ = self.cur_color.getHsv()
if hue != self.cur_hue:
new_color = True
self.cur_color.setHsv(self.cur_hue, sat, val)
cx = float(self.contentsRect().center().x())
cy = float(self.contentsRect().center().y())
self.point_a = QtCore.QPointF(
cx + (cos(self.angle_a) * inner_radius),
cy - (sin(self.angle_a) * inner_radius)
)
self.point_b = QtCore.QPointF(
cx + (cos(self.angle_b) * inner_radius),
cy - (sin(self.angle_b) * inner_radius)
)
self.point_c = QtCore.QPointF(
cx + (cos(self.angle_c) * inner_radius),
cy - (sin(self.angle_c) * inner_radius)
)
selector_radius = (
self.outer_radius
- (self.outer_radius / self.selector_radius_ratio)
)
self.point_d = QtCore.QPointF(
cx + (cos(self.angle_a) * selector_radius),
cy - (sin(self.angle_a) * selector_radius)
)
self.selector_pos = self._point_from_color(self.cur_color)
self.color_changed.emit(self.cur_color)
else:
self.sel_mode = TriangleState.SelectingSatValueState
aa = Vertex(QtCore.Qt.transparent, self.point_a)
bb = Vertex(QtCore.Qt.transparent, self.point_b)
cc = Vertex(QtCore.Qt.transparent, self.point_c)
self.selector_pos = self._move_point_to_triangle(
depos.x(), depos.y(), aa, bb, cc
)
col = self._color_from_point(self.selector_pos)
if col != self.cur_color:
self.cur_color = col
new_color = True
if new_color:
self.color_changed.emit(self.cur_color)
self.update()
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.sel_mode = TriangleState.IdleState
def keyPressEvent(self, event):
key = event.key()
if key == QtCore.Qt.Key_Left:
self.cur_hue -= 1
if self.cur_hue < 0:
self.cur_hue += 360
_, sat, val, _ = self.cur_color.getHsv()
tmp = QtGui.QColor()
tmp.setHsv(self.cur_hue, sat, val)
self.set_color(tmp)
elif key == QtCore.Qt.Key_Right:
self.cur_hue += 1
if (self.cur_hue > 359):
self.cur_hue -= 360
_, sat, val, _ = self.cur_color.getHsv()
tmp = QtGui.QColor()
tmp.setHsv(self.cur_hue, sat, val)
self.set_color(tmp)
elif key == QtCore.Qt.Key_Up:
_, sat, val, _ = self.cur_color.getHsv()
if event.modifiers() & QtCore.Qt.ShiftModifier:
if sat > 5:
sat -= 5
else:
sat = 0
else:
if val > 5:
val -= 5
else:
val = 0
tmp = QtGui.QColor()
tmp.setHsv(self.cur_hue, sat, val)
self.set_color(tmp)
elif key == QtCore.Qt.Key_Down:
_, sat, val, _ = self.cur_color.getHsv()
if event.modifiers() & QtCore.Qt.ShiftModifier:
if sat < 250:
sat += 5
else:
sat = 255
else:
if val < 250:
val += 5
else:
val = 255
tmp = QtGui.QColor()
tmp.setHsv(self.cur_hue, sat, val)
self.set_color(tmp)
def resizeEvent(self, _event):
size_w = self.contentsRect().width()
size_h = self.contentsRect().height()
if size_w < size_h:
size = size_w
else:
size = size_h
self.outer_radius = (size - 1) / 2
self.pen_width = int(
floor(self.outer_radius / self.ellipse_thick_ratio)
)
self.ellipse_size = int(
floor(self.outer_radius / self.ellipse_size_ratio)
)
cx = float(self.contentsRect().center().x())
cy = float(self.contentsRect().center().y())
inner_radius = (
self.outer_radius
- (self.outer_radius / self.inner_radius_ratio)
)
selector_radius = (
self.outer_radius
- (self.outer_radius / self.selector_radius_ratio)
)
self.point_a = QtCore.QPointF(
cx + (cos(self.angle_a) * inner_radius),
cy - (sin(self.angle_a) * inner_radius)
)
self.point_b = QtCore.QPointF(
cx + (cos(self.angle_b) * inner_radius),
cy - (sin(self.angle_b) * inner_radius)
)
self.point_c = QtCore.QPointF(
cx + (cos(self.angle_c) * inner_radius),
cy - (sin(self.angle_c) * inner_radius)
)
self.point_d = QtCore.QPointF(
cx + (cos(self.angle_a) * selector_radius),
cy - (sin(self.angle_a) * selector_radius)
)
# Find the current position of the selector
self.selector_pos = self._point_from_color(self.cur_color)
self.update()
def drawTrigon(self, buf, pa, pb, pc, 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
p1 = Vertex(color, pa)
p2 = Vertex(QtCore.Qt.black, pb)
p3 = Vertex(QtCore.Qt.white, pc)
# Sort. Make p1 above p2, which is above p3 (using y coordinate).
# Bubble sorting is fastest here.
if p1.point.y() > p2.point.y():
p1, p2 = p2, p1
if p1.point.y() > p3.point.y():
p1, p3 = p3, p1
if p2.point.y() > p3.point.y():
p2, p3 = p3, p2
# All the three y deltas are >= 0
p1p2ydist = float(p2.point.y() - p1.point.y())
p1p3ydist = float(p3.point.y() - p1.point.y())
p2p3ydist = float(p3.point.y() - p2.point.y())
p1p2xdist = float(p2.point.x() - p1.point.x())
p1p3xdist = float(p3.point.x() - p1.point.x())
p2p3xdist = float(p3.point.x() - p2.point.x())
# The first x delta decides wether we have a lefty or a righty
# trigon.
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.
leftColors = {}
rightColors = {}
leftX = {}
rightX = {}
# Scan longy - find all left and right colors and X-values for
# the tallest edge (p1-p3).
# 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 = 0.0
rdelta = 0.0
gdelta = 0.0
bdelta = 0.0
if p1p3ydist != 0.0:
xdelta = p1p3xdist / p1p3ydist
rdelta = (dest.r - r) / p1p3ydist
gdelta = (dest.g - g) / p1p3ydist
bdelta = (dest.b - b) / p1p3ydist
# Calculate gradients using linear approximation
for y in range(y1, y2):
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 = 0.0
rdelta = 0.0
gdelta = 0.0
bdelta = 0.0
if p1p2ydist != 0.0:
xdelta = p1p2xdist / p1p2ydist
rdelta = (dest.r - r) / p1p2ydist
gdelta = (dest.g - g) / p1p2ydist
bdelta = (dest.b - b) / p1p2ydist
# Calculate gradients using linear approximation
for y in range(y1, y2):
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 = 0.0
rdelta = 0.0
gdelta = 0.0
bdelta = 0.0
if p2p3ydist != 0.0:
xdelta = p2p3xdist / p2p3ydist
rdelta = (dest.r - r) / p2p3ydist
gdelta = (dest.g - g) / p2p3ydist
bdelta = (dest.b - b) / p2p3ydist
# Calculate gradients using linear approximation
for y in range(y1, y2):
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.
p3yfloor = int(floor(p3.point.y()))
p1yfloor = int(floor(p1.point.y()))
for y in range(p1yfloor, p3yfloor):
lx = leftX[y]
rx = rightX[y]
lxi = int(floor(lx))
rxi = int(floor(rx))
rc = rightColors[y]
lc = leftColors[y]
# if the xdist is 0, don't draw anything.
xdist = rx - lx
if xdist != 0.0:
r = lc.r
g = lc.g
b = lc.b
rdelta = (rc.r - r) / xdist
gdelta = (rc.g - g) / xdist
bdelta = (rc.b - b) / xdist
# Inner loop 2. Draws the line from left to right.
for x in range(lxi, rxi + 1):
buf.setPixel(x, y, QtGui.qRgb(int(r), int(g), int(b)))
r += rdelta
g += gdelta
b += bdelta
def _radius_at(self, pos, rect):
mousexdist = pos.x() - float(rect.center().x())
mouseydist = pos.y() - float(rect.center().y())
return sqrt(mousexdist ** 2 + mouseydist ** 2)
def _angle_at(self, pos, rect):
mousexdist = pos.x() - float(rect.center().x())
mouseydist = pos.y() - float(rect.center().y())
mouserad = sqrt(mousexdist ** 2 + mouseydist ** 2)
if mouserad == 0.0:
return 0.0
angle = acos(mousexdist / mouserad)
if mouseydist >= 0:
angle = TWOPI - angle
return angle
def _point_from_color(self, col):
# Simplifications for the corner cases.
if col == QtCore.Qt.black:
return self.point_b
elif col == QtCore.Qt.white:
return self.point_c
# Find the x and y slopes
ab_deltax = self.point_b.x() - self.point_a.x()
ab_deltay = self.point_b.y() - self.point_a.y()
bc_deltax = self.point_c.x() - self.point_b.x()
bc_deltay = self.point_c.y() - self.point_b.y()
ac_deltax = self.point_c.x() - self.point_a.x()
ac_deltay = self.point_c.y() - self.point_a.y()
# Extract the h,s,v values of col.
_, sat, val, _ = col.getHsv()
# Find the line that passes through the triangle where the value
# is equal to our color's value.
p1 = self.point_a.x() + (ab_deltax * float(255 - val)) / 255.0
q1 = self.point_a.y() + (ab_deltay * float(255 - val)) / 255.0
p2 = self.point_b.x() + (bc_deltax * float(val)) / 255.0
q2 = self.point_b.y() + (bc_deltay * float(val)) / 255.0
# Find the line that passes through the triangle where the
# saturation is equal to our color's value.
p3 = self.point_a.x() + (ac_deltax * float(255 - sat)) / 255.0
q3 = self.point_a.y() + (ac_deltay * float(255 - sat)) / 255.0
p4 = self.point_b.x()
q4 = self.point_b.y()
# Find the intersection between these lines.
if p1 != p2:
a = (q2 - q1) / (p2 - p1)
c = (q4 - q3) / (p4 - p3)
b = q1 - a * p1
d = q3 - c * p3
x = (d - b) / (a - c)
y = a * x + b
else:
x = p1
p4_p3 = p4 - p3
if p4_p3 == 0:
y = 0
else:
y = q3 + (x - p3) * (q4 - q3) / p4_p3
return QtCore.QPointF(x, y)
def _color_from_point(self, p):
# Find the outer radius of the hue gradient.
size_w = self.contentsRect().width()
size_h = self.contentsRect().height()
if size_w < size_h:
size = size_w
else:
size = size_h
outer_radius = (size - 1) / 2
# Find the center coordinates
cx = float(self.contentsRect().center().x())
cy = float(self.contentsRect().center().y())
# Find the a, b and c from their angles, the center of the rect
# and the radius of the hue gradient donut.
inner_radius = outer_radius - (outer_radius / self.inner_radius_ratio)
pa = QtCore.QPointF(
cx + (cos(self.angle_a) * inner_radius),
cy - (sin(self.angle_a) * inner_radius)
)
pb = QtCore.QPointF(
cx + (cos(self.angle_b) * inner_radius),
cy - (sin(self.angle_b) * inner_radius)
)
pc = QtCore.QPointF(
cx + (cos(self.angle_c) * inner_radius),
cy - (sin(self.angle_c) * inner_radius)
)
# Find the hue value from the angle of the 'a' point.
angle = self.angle_a - PI / 2.0
if angle < 0:
angle += TWOPI
hue = (
360
- int(floor((360.0 * angle) / TWOPI))
- self.hue_offset
) % 360
# Create the color of the 'a' corner point. We know that b is
# black and c is white.
color = QtGui.QColor()
color.setHsv(hue, 255, 255)
# See also drawTrigon(), which basically does exactly the same to
# determine all colors in the trigon.
p1 = Vertex(color, pa)
p2 = Vertex(QtCore.Qt.black, pb)
p3 = Vertex(QtCore.Qt.white, pc)
# Make sure p1 is above p2, which is above p3.
if p1.point.y() > p2.point.y():
p1, p2 = p2, p1
if p1.point.y() > p3.point.y():
p1, p3 = p3, p1
if p2.point.y() > p3.point.y():
p2, p3 = p3, p2
# Find the slopes of all edges in the trigon. All the three y
# deltas here are positive because of the above sorting.
p1p2ydist = p2.point.y() - p1.point.y()
p1p3ydist = p3.point.y() - p1.point.y()
p2p3ydist = p3.point.y() - p2.point.y()
p1p2xdist = p2.point.x() - p1.point.x()
p1p3xdist = p3.point.x() - p1.point.x()
p2p3xdist = p3.point.x() - p2.point.x()
# The first x delta decides wether we have a lefty or a righty
# trigon. A lefty trigon has its tallest edge on the right hand
# side of the trigon. The righty trigon has it on its left side.
# This property determines wether the left or the right set of x
# coordinates will be continuous.
lefty = p1p2xdist < 0
# Find whether the selector's y is in the first or second shorty,
# counting from the top and downwards. This is used to find the
# color at the selector point.
firstshorty = (p.y() >= p1.point.y() and p.y() < p2.point.y())
# From the y value of the selector's position, find the left and
# right x values.
if lefty:
if firstshorty:
if (floor(p1p2ydist) != 0.0):
leftx = (
p1.point.x()
+ ((p1p2xdist * (p.y() - p1.point.y())) / p1p2ydist)
)
else:
leftx = min(p1.point.x(), p2.point.x())
else:
if (floor(p2p3ydist) != 0.0):
leftx = (
p2.point.x()
+ (p2p3xdist * (p.y() - p2.point.y())) / p2p3ydist
)
else:
leftx = min(p2.point.x(), p3.point.x())
rightx = (
p1.point.x()
+ ((p1p3xdist * (p.y() - p1.point.y())) / p1p3ydist)
)
else:
leftx = (
p1.point.x()
+ ((p1p3xdist * (p.y() - p1.point.y())) / p1p3ydist)
)
if firstshorty:
if floor(p1p2ydist) != 0.0:
rightx = (
p1.point.x()
+ ((p1p2xdist * (p.y() - p1.point.y())) / p1p2ydist)
)
else:
rightx = max(p1.point.x(), p2.point.x())
else:
if floor(p2p3ydist) != 0.0:
rightx = (
p2.point.x()
+ ((p2p3xdist * (p.y() - p2.point.y())) / p2p3ydist)
)
else:
rightx = max(p2.point.x(), p3.point.x())
# Find the r,g,b values of the points on the trigon's edges that
# are to the left and right of the selector.
if firstshorty:
if floor(p1p2ydist) != 0.0:
p_p1_ratio = (p.y() - p1.point.y()) / p1p2ydist
p2_p_ratio = (p2.point.y() - p.y()) / p1p2ydist
rshort = (p2.color.r * p_p1_ratio) + (p1.color.r * p2_p_ratio)
gshort = (p2.color.g * p_p1_ratio) + (p1.color.g * p2_p_ratio)
bshort = (p2.color.b * p_p1_ratio) + (p1.color.b * p2_p_ratio)
elif lefty:
if p1.point.x() <= p2.point.x():
rshort = p1.color.r
gshort = p1.color.g
bshort = p1.color.b
else:
rshort = p2.color.r
gshort = p2.color.g
bshort = p2.color.b
else:
if p1.point.x() > p2.point.x():
rshort = p1.color.r
gshort = p1.color.g
bshort = p1.color.b
else:
rshort = p2.color.r
gshort = p2.color.g
bshort = p2.color.b
else:
if floor(p2p3ydist) != 0.0:
p_p2_ratio = (p.y() - p2.point.y()) / p2p3ydist
p3_p_ratio = (p3.point.y() - p.y()) / p2p3ydist
rshort = (p3.color.r * p_p2_ratio) + (p2.color.r * p3_p_ratio)
gshort = (p3.color.g * p_p2_ratio) + (p2.color.g * p3_p_ratio)
bshort = (p3.color.b * p_p2_ratio) + (p2.color.b * p3_p_ratio)
elif lefty:
if p2.point.x() <= p3.point.x():
rshort = p2.color.r
gshort = p2.color.g
bshort = p2.color.b
else:
rshort = p3.color.r
gshort = p3.color.g
bshort = p3.color.b
else:
if p2.point.x() > p3.point.x():
rshort = p2.color.r
gshort = p2.color.g
bshort = p2.color.b
else:
rshort = p3.color.r
gshort = p3.color.g
bshort = p3.color.b