forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorPicker.pb
2076 lines (1674 loc) · 67 KB
/
ColorPicker.pb
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) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
;- ----- Common stuff -----
Declare RGBToHSV(Color, *h.FLOAT, *s.FLOAT, *v.FLOAT)
Declare RGBToHSL(Color, *h.FLOAT, *s.FLOAT, *l.FLOAT)
Declare HueToRGB(h.f)
Declare HSVToRGB(h.f, s.f, v.f)
Declare HSLToRGB(h.f, s.f, l.f)
Prototype ColorPicker_Mode_Setup(*Entry, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
Prototype ColorPicker_Mode_Event(*Entry, Gadget, Type)
Structure ColorPickerData Extends ToolsPanelEntry
Mode$ ; current mode
RGBColor.l ; current RGB color (no alpha component)
; for alpha mode
UseAlpha.l
VerticalAlpha.l ; if true, draw alpha above solid in the color field
a.f
IgnoreInputField.l ; see ColorPicker_UpdateColorField
; callback for current mode
SetupFunction.ColorPicker_Mode_Setup
EventFunction.ColorPicker_Mode_Event
; for RGB mode
r.f
g.f
b.f
; for HSL/HSV mode
h.f
s.f
l.f
v.f
; for color wheel
TriangleRadius.l
TriangleSide.f
TriangleHeight.f
; for Palette/Name
First.l
Rows.l
Cols.l
RowHeight.l
BoxWidth.l
Palette$
CompilerIf #CompileWindows
ScrollCallback.i
CompilerEndIf
EndStructure
Structure ColorEntry
Name$
Color.l
EndStructure
Structure Palette
ID$
Name$
Count.l
Array Entry.ColorEntry(0)
EndStructure
Structure ColorHistory
Color.l
Alpha.l ; -1 if not used
EndStructure
#ColorPicker_History = 24
Global Dim ColorPicker_History.ColorHistory(#ColorPicker_History-1)
Global *ColorPicker.ColorPickerData ; needed for filter access
Global NewList PaletteList.Palette()
Global *Palette.Palette
Global FilteredPalette.Palette ; this is always a filtered copy of the current palette
Declare ColorPicker_ResizeHandler(*Entry.ColorPickerData, PanelWidth, PanelHeight)
Declare ColorPicker_UpdateColor(*Entry.ColorPickerData, Color)
Declare ColorPicker_TriggerResize(*Entry.ColorPickerData)
CompilerIf #CompileWindows
Global ScrollBarOldCallback ; for scrollbar updates
CompilerEndIf
Procedure ColorPicker_LoadPalettes(*Entry.ColorPickerData)
Select UCase(CurrentLanguage$)
Case "DEUTSCH": Lang$ = "de"
Case "FRANCAIS": Lang$ = "fr"
Default: Lang$ = "en"
EndSelect
If ListSize(PaletteList()) = 0
If LoadXML(#XML_ColorTable, PureBasicPath$ + #DEFAULT_CatalogPath + "ColorTable.xml") And XMLStatus(#XML_ColorTable) = #PB_XML_Success
*Main = MainXMLNode(#XML_ColorTable)
If *Main And GetXMLNodeName(*Main) = "table"
*Palette = ChildXMLNode(*Main)
While *Palette
If XMLNodeType(*Palette) = #PB_XML_Normal And GetXMLNodeName(*Palette) = "palette"
AddElement(PaletteList())
PaletteList()\Count = 0
PaletteList()\ID$ = GetXMLAttribute(*Palette, "id")
PaletteList()\Name$ = GetXMLAttribute(*Palette, Lang$)
If PaletteList()\Name$ = ""
PaletteList()\Name$ = GetXMLAttribute(*Palette, "en") ; always the fallback
EndIf
; do a count
*Color = ChildXMLNode(*Palette)
While *Color
If XMLNodeType(*Color) = #PB_XML_Normal And GetXMLNodeName(*Color) = "color"
PaletteList()\Count + 1
EndIf
*Color = NextXMLNode(*Color)
Wend
If PaletteList()\Count > 0
Dim PaletteList()\Entry(PaletteList()\Count-1)
EndIf
; read entries
i = 0
*Color = ChildXMLNode(*Palette)
While *Color
If XMLNodeType(*Color) = #PB_XML_Normal And GetXMLNodeName(*Color) = "color"
PaletteList()\Entry(i)\Name$ = GetXMLAttribute(*Color, Lang$)
If PaletteList()\Entry(i)\Name$ = ""
PaletteList()\Entry(i)\Name$ = GetXMLAttribute(*Color, "en") ; fallback
EndIf
PaletteList()\Entry(i)\Color = RGB(Val(Trim(GetXMLAttribute(*Color, "r"))), Val(Trim(GetXMLAttribute(*Color, "g"))), Val(Trim(GetXMLAttribute(*Color, "b"))))
i + 1
EndIf
*Color = NextXMLNode(*Color)
Wend
EndIf
*Palette = NextXMLNode(*Palette)
Wend
EndIf
EndIf
; if still zero, add an empty palette to the list so we do not have to handle
; that condition later
;
If ListSize(PaletteList()) = 0
AddElement(PaletteList())
PaletteList()\Name$ = ""
PaletteList()\Count = 0
EndIf
FirstElement(PaletteList())
*Palette = @PaletteList()
; fill the combobox
;
ClearGadgetItems(#GADGET_Color_Scheme)
index = 0
ForEach PaletteList()
AddGadgetItem(#GADGET_Color_Scheme, -1, PaletteList()\Name$)
If PaletteList()\ID$ = *Entry\Palette$
index = ListIndex(PaletteList())
*Palette = @PaletteList()
EndIf
Next PaletteList()
SetGadgetState(#GADGET_Color_Scheme, index)
EndIf
EndProcedure
; create the chessfield effect below transparent areas
;
Procedure ColorPicker_TransparentFilter(x, y, SourceColor, TargetColor)
If (x % 14 < 7 And y % 14 < 7) Or (x % 14 >= 7 And y % 14 >= 7)
ProcedureReturn AlphaBlend(SourceColor, $FFC0C0C0)
Else
ProcedureReturn AlphaBlend(SourceColor, $FFFFFFFF)
EndIf
EndProcedure
; Draw a simple cross marker for 2d areas
;
Procedure ColorPicker_DrawCross(x, y)
Box(x-8, y-1, 7, 3, $000000)
Box(x+2, y-1, 7, 3, $000000)
Box(x-1, y-8, 3, 7, $000000)
Box(x-1, y+2, 3, 7, $000000)
Line(x-5, y, 3, 1, $FFFFFF)
Line(x+3, y, 3, 1, $FFFFFF)
Line(x, y-5, 1, 3, $FFFFFF)
Line(x, y+3, 1, 3, $FFFFFF)
EndProcedure
; Draw a double arrow marker for slides
;
Procedure ColorPicker_DrawArrow(x, y1, y2)
Line(x-4, y1, 9, 1, $FFFFFF)
Line(x-3, y1+1, 7, 1, $FFFFFF)
Line(x-2, y1+2, 5, 1, $FFFFFF)
Line(x-1, y1+3, 3, 1, $FFFFFF)
Line(x, y1+4, 1, 1, $FFFFFF) ; use Line here too, so it is bounds checked (unlike Plot)
Line(x-3, y1, 7, 1, $000000)
Line(x-2, y1+1, 5, 1, $000000)
Line(x-1, y1+2, 3, 1, $000000)
Line(x, y1+3, 1, 1, $000000)
Line(x-4, y2, 9, 1, $FFFFFF)
Line(x-3, y2-1, 7, 1, $FFFFFF)
Line(x-2, y2-2, 5, 1, $FFFFFF)
Line(x-1, y2-3, 3, 1, $FFFFFF)
Line(x, y2-4, 1, 1, $FFFFFF)
Line(x-3, y2, 7, 1, $000000)
Line(x-2, y2-1, 5, 1, $000000)
Line(x-1, y2-2, 3, 1, $000000)
Line(x, y2-3, 1, 1, $000000)
EndProcedure
; get a RGBA color from an RGB and A value
;
Procedure ColorPicker_AddAlpha(Color, Alpha)
ProcedureReturn (Color & $FFFFFF) | (Alpha << 24)
EndProcedure
;- ----- RGB picker -----
Procedure ColorPicker_RGB_Update(*Entry.ColorPickerData)
r = Int(*Entry\r * 255.0)
g = Int(*Entry\g * 255.0)
b = Int(*Entry\b * 255.0)
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas1))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $000000)
For x = 0 To w-3
Line(x+1, 1, 1, h-2, RGB(Int((x * 255.0) / (w-3)), g, b))
Next x
ColorPicker_DrawArrow(1+*Entry\r * (w-3), 1, DesktopScaledY(28))
StopDrawing()
EndIf
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas2))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $000000)
For x = 0 To w-3
Line(x+1, 1, 1, h-2, RGB(r, Int((x * 255.0) / (w-3)), b))
Next x
ColorPicker_DrawArrow(1+*Entry\g * (w-3), 1, DesktopScaledY(28))
StopDrawing()
EndIf
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas3))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $000000)
For x = 0 To w-3
Line(x+1, 1, 1, h-2, RGB(r, g, Int((x * 255.0) / (w-3))))
Next x
ColorPicker_DrawArrow(1+*Entry\b * (w-3), 1, DesktopScaledY(28))
StopDrawing()
EndIf
ColorPicker_UpdateColor(*Entry, RGB(r, g, b))
EndProcedure
Procedure ColorPicker_RGB_Setup(*Entry.ColorPickerData, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
HideGadget(#GADGET_Color_Canvas1, 0)
HideGadget(#GADGET_Color_Canvas2, 0)
HideGadget(#GADGET_Color_Canvas3, 0)
HideGadget(#GADGET_Color_Scheme, 1)
HideGadget(#GADGET_Color_Scroll, 1)
HideGadget(#GADGET_Color_Filter, 1)
HideGadget(#GADGET_Color_Label1, 0)
HideGadget(#GADGET_Color_Label2, 0)
HideGadget(#GADGET_Color_Label3, 0)
SetGadgetText(#GADGET_Color_Label1, "R: ")
SetGadgetText(#GADGET_Color_Label2, "G: ")
SetGadgetText(#GADGET_Color_Label3, "B: ")
GetRequiredSize(#GADGET_Color_Label1, @LabelWidth, @LabelHeight)
Offset = (30-LabelHeight) / 2
ResizeGadget(#GADGET_Color_Label1, CanvasX, CanvasY+Offset+5, LabelWidth, LabelHeight)
ResizeGadget(#GADGET_Color_Label2, CanvasX, CanvasY+Offset+45, LabelWidth, LabelHeight)
ResizeGadget(#GADGET_Color_Label3, CanvasX, CanvasY+Offset+85, LabelWidth, LabelHeight)
ResizeGadget(#GADGET_Color_Canvas1, CanvasX+LabelWidth, CanvasY+5, CanvasWidth-LabelWidth, 30)
ResizeGadget(#GADGET_Color_Canvas2, CanvasX+LabelWidth, CanvasY+45, CanvasWidth-LabelWidth, 30)
ResizeGadget(#GADGET_Color_Canvas3, CanvasX+LabelWidth, CanvasY+85, CanvasWidth-LabelWidth, 30)
; Get the current color
;
*Entry\r = Red(*Entry\RGBColor) / 255.0
*Entry\g = Green(*Entry\RGBColor) / 255.0
*Entry\b = Blue(*Entry\RGBColor) / 255.0
ColorPicker_RGB_Update(*Entry)
ProcedureReturn 115 ; actual height used
EndProcedure
Procedure ColorPicker_RGB_Event(*Entry.ColorPickerData, Gadget, Type)
If Gadget = #GADGET_Color_Canvas1 Or Gadget = #GADGET_Color_Canvas2 Or Gadget = #GADGET_Color_Canvas3
If Type = #PB_EventType_LeftButtonDown Or (Type = #PB_EventType_MouseMove And GetGadgetAttribute(Gadget, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
value.f = (GetGadgetAttribute(Gadget, #PB_Canvas_MouseX) - 1) / (DesktopScaledX(GadgetWidth(Gadget)) - 3)
If value < 0: value = 0: EndIf
If value > 1: value = 1: EndIf
Select Gadget
Case #GADGET_Color_Canvas1: *Entry\r = value
Case #GADGET_Color_Canvas2: *Entry\g = value
Case #GADGET_Color_Canvas3: *Entry\b = value
EndSelect
*Entry\RGBColor = RGB(*Entry\r * 255, *Entry\g * 255, *Entry\b * 255)
ColorPicker_RGB_Update(*Entry)
EndIf
EndIf
EndProcedure
;- ----- HSV picker -----
Procedure ColorPicker_HSV_Update(*Entry.ColorPickerData, DrawAll)
; draw the s/v field on a backing image and only
; change it if the hue changed as it is quite slow to draw
;
If DrawAll
If StartDrawing(ImageOutput(#IMAGE_Color_Content2))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $000000)
For x = 0 To w-3
For y = 0 To h-3
Plot(1+x, 1+y, HSVToRGB(*Entry\h, x / (w-3), 1.0-(y / (h-3))))
Next y
Next x
StopDrawing()
EndIf
EndIf
; redraw the canvases with new markers
;
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas1))
DrawImage(ImageID(#IMAGE_Color_Content1), 0, 0)
ColorPicker_DrawArrow(1 + Int((*Entry\h * (OutputWidth() - 3)) / 360.0), 1, DesktopScaledY(28))
StopDrawing()
EndIf
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas2))
DrawImage(ImageID(#IMAGE_Color_Content2), 0, 0)
ColorPicker_DrawCross(1 + Int(*Entry\s * (OutputWidth()-3)), 1 + Int((1.0 - *Entry\v) * (OutputHeight()-3)))
StopDrawing()
EndIf
ColorPicker_UpdateColor(*Entry, HSVToRGB(*Entry\h, *Entry\s, *Entry\v))
EndProcedure
Procedure ColorPicker_HSV_Setup(*Entry.ColorPickerData, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
; Limit the height to a 4/3 ratio to avoid an overly long picker area
If CanvasHeight*3 > CanvasWidth*4
CanvasHeight = (CanvasWidth * 4) / 3
EndIf
; Create hue image and draw it
ImageWidth = DesktopScaledX(CanvasWidth)
ImageHeight = DesktopScaledY(CanvasHeight)
CreateImage(#IMAGE_Color_Content1, ImageWidth, ImageHeight, 24)
If StartDrawing(ImageOutput(#IMAGE_Color_Content1))
Box(0, 0, ImageWidth, ImageHeight, $000000)
For x = 0 To ImageWidth-3
Line(x+1, 1, 1, ImageHeight-3, HueToRGB((x * 360.0) / (ImageWidth-3)))
Next x
StopDrawing()
EndIf
; Create image for s/v field
CreateImage(#IMAGE_Color_Content2, ImageWidth, ImageHeight-35, 24)
RGBToHSV(*Entry\RGBColor, @*Entry\h, @*Entry\s, @*Entry\v)
HideGadget(#GADGET_Color_Canvas1, 0)
HideGadget(#GADGET_Color_Canvas2, 0)
HideGadget(#GADGET_Color_Canvas3, 1)
HideGadget(#GADGET_Color_Scheme, 1)
HideGadget(#GADGET_Color_Scroll, 1)
HideGadget(#GADGET_Color_Filter, 1)
HideGadget(#GADGET_Color_Label1, 1)
HideGadget(#GADGET_Color_Label2, 1)
HideGadget(#GADGET_Color_Label3, 1)
ResizeGadget(#GADGET_Color_Canvas1, CanvasX, CanvasY, CanvasWidth, 30)
ResizeGadget(#GADGET_Color_Canvas2, CanvasX, CanvasY+35, CanvasWidth, CanvasHeight-35)
ColorPicker_HSV_Update(*Entry, #True)
ProcedureReturn CanvasHeight ; actual height used
EndProcedure
Procedure ColorPicker_HSV_Event(*Entry.ColorPickerData, Gadget, Type)
If Gadget = #GADGET_Color_Canvas1
If Type = #PB_EventType_LeftButtonDown Or (Type = #PB_EventType_MouseMove And GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
*Entry\h = ((GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseX) - 1) * 360.0) / (DesktopScaledX(GadgetWidth(#GADGET_Color_Canvas1)) - 3)
If *Entry\h < 0: *Entry\h = 0: EndIf
If *Entry\h >= 360: value = 360: EndIf
ColorPicker_HSV_Update(*Entry, #True) ; redraw main area
EndIf
ElseIf Gadget = #GADGET_Color_Canvas2
If Type = #PB_EventType_LeftButtonDown Or (Type = #PB_EventType_MouseMove And GetGadgetAttribute(#GADGET_Color_Canvas2, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
*Entry\s = (GetGadgetAttribute(#GADGET_Color_Canvas2, #PB_Canvas_MouseX)-1) / (DesktopScaledX(GadgetWidth(#GADGET_Color_Canvas2))-3)
*Entry\v = 1.0 - (GetGadgetAttribute(#GADGET_Color_Canvas2, #PB_Canvas_MouseY)-1) / (DesktopScaledX(GadgetHeight(#GADGET_Color_Canvas2))-3)
If *Entry\s < 0: *Entry\s = 0: EndIf
If *Entry\s > 1: *Entry\s = 1: EndIf
If *Entry\v < 0: *Entry\v = 0: EndIf
If *Entry\v > 1: *Entry\v = 1: EndIf
ColorPicker_HSV_Update(*Entry, #False) ; no redraw of the s/v area
EndIf
EndIf
EndProcedure
;- ----- HSL picker -----
Procedure ColorPicker_HSL_Update(*Entry.ColorPickerData)
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas1))
DrawImage(ImageID(#IMAGE_Color_Content1), 0, 0)
ColorPicker_DrawCross(1 + Int((*Entry\h * (OutputWidth()-3)) / 360.0), 1 + Int((1.0-*Entry\s) * (OutputHeight()-3)))
StopDrawing()
EndIf
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas2))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $000000)
For x = 0 To w-3
Line(1+x, 1+y, 1, h-2, HSLToRGB(*Entry\h, *Entry\s, (x / (w-3))))
Next x
ColorPicker_DrawArrow(1 + Int(*Entry\l * (w - 3)), 1, h-2)
StopDrawing()
EndIf
ColorPicker_UpdateColor(*Entry, HSLToRGB(*Entry\h, *Entry\s, *Entry\l))
EndProcedure
Procedure ColorPicker_HSL_Setup(*Entry.ColorPickerData, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
; Limit the height to a 4/3 ratio to avoid an overly long picker area
If CanvasHeight*3 > CanvasWidth*4
CanvasHeight = (CanvasWidth * 4) / 3
EndIf
; Create h/v image and draw it
ImageWidth = DesktopScaledX(CanvasWidth)
ImageHeight = DesktopScaledY(CanvasHeight)
CreateImage(#IMAGE_Color_Content1, ImageWidth, ImageHeight -35, 24)
If StartDrawing(ImageOutput(#IMAGE_Color_Content1))
Box(0, 0, w, ImageHeight-35, $000000)
For x = 0 To ImageWidth-3
For y = 0 To ImageHeight-38
Plot(1+x, 1+y, HSLToRGB((x * 360.0) / (ImageWidth-3), 1.0-(y / (ImageHeight-37)), 0.5)) ; divide by h-42, as a value of 0 in s is not allowed
Next y
Next x
StopDrawing()
EndIf
RGBToHSL(*Entry\RGBColor, @*Entry\h, @*Entry\s, @*Entry\l)
HideGadget(#GADGET_Color_Canvas1, 0)
HideGadget(#GADGET_Color_Canvas2, 0)
HideGadget(#GADGET_Color_Canvas3, 1)
HideGadget(#GADGET_Color_Scheme, 1)
HideGadget(#GADGET_Color_Scroll, 1)
HideGadget(#GADGET_Color_Filter, 1)
HideGadget(#GADGET_Color_Label1, 1)
HideGadget(#GADGET_Color_Label2, 1)
HideGadget(#GADGET_Color_Label3, 1)
ResizeGadget(#GADGET_Color_Canvas1, CanvasX, CanvasY, CanvasWidth, CanvasHeight-35)
ResizeGadget(#GADGET_Color_Canvas2, CanvasX, CanvasY+CanvasHeight-30, CanvasWidth, 30)
ColorPicker_HSL_Update(*Entry)
ProcedureReturn CanvasHeight ; actual height used
EndProcedure
Procedure ColorPicker_HSL_Event(*Entry.ColorPickerData, Gadget, Type)
If Gadget = #GADGET_Color_Canvas1
If Type = #PB_EventType_LeftButtonDown Or (Type = #PB_EventType_MouseMove And GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
*Entry\h = ((GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseX)-1) * 360.0) / (DesktopScaledX(GadgetWidth(#GADGET_Color_Canvas1))-3)
*Entry\s = 1.0 - (GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseY)-1) / (DesktopScaledY(GadgetHeight(#GADGET_Color_Canvas1))-2)
If *Entry\h < 0: *Entry\h = 0: EndIf
If *Entry\h >= 360.0: *Entry\h = 360: EndIf
If *Entry\s <= 0: *Entry\s = 0.001: EndIf
If *Entry\s > 1: *Entry\s = 1: EndIf
ColorPicker_HSL_Update(*Entry)
EndIf
ElseIf Gadget = #GADGET_Color_Canvas2
If Type = #PB_EventType_LeftButtonDown Or (Type = #PB_EventType_MouseMove And GetGadgetAttribute(#GADGET_Color_Canvas2, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
*Entry\l = (GetGadgetAttribute(#GADGET_Color_Canvas2, #PB_Canvas_MouseX)-1) / (DesktopScaledX(GadgetWidth(#GADGET_Color_Canvas2))-3)
If *Entry\l < 0: *Entry\l = 0: EndIf
If *Entry\l > 1: *Entry\l = 1: EndIf
ColorPicker_HSL_Update(*Entry)
EndIf
EndIf
EndProcedure
;- ----- Wheel picker (HSV) -----
; rotate in degrees
Procedure Rotate(Angle.f, *x.FLOAT, *y.FLOAT)
cos.f = Cos(Radian(Angle))
sin.f = Sin(Radian(Angle))
x.f = *x\f * cos - *y\f * sin
y.f = *x\f * sin + *y\f * cos
*x\f = x
*y\f = y
EndProcedure
Procedure ColorPicker_WheelFilter(x, y, SourceColor, TargetColor)
Center = OutputWidth() / 2
Angle.f = Mod(Degree(ATan2(x-Center, y-Center)) + 360, 360)
ProcedureReturn HueToRGB(Angle)
EndProcedure
Procedure ColorPicker_WheelCenterFilter(x, y, SourceColor, TargetColor)
Center = OutputWidth() / 2
; first rotate the x/y around the center by the negative (hue+30) degrees
; so the triangle is upright, with the v direction parallel to the y axis and
; the s direction parallel to x
;
r_x.f = x - Center
r_y.f = y - Center
Rotate(-(*ColorPicker\h + 30.0), @r_x, @r_y)
; v is now just the reversed y coordinate scaled by the triangle height
;
v.f = 1.0 - (r_y + (*ColorPicker\TriangleHeight / 3.0)) / *ColorPicker\TriangleHeight
; s is the x coordinate scaled by the triangle side
; the triangle side has to be scaled by v first, as we want a triangle shape, not a square shape
;
s.f = (r_x + ((*ColorPicker\TriangleSide * v) / 2.0)) / (*ColorPicker\TriangleSide * v)
; now we have the color
;
ProcedureReturn HSVToRGB(*ColorPicker\h, s, v)
EndProcedure
Procedure ColorPicker_Wheel_Update(*Entry.ColorPickerData, DrawAll)
; draw the triangle on the backing image and only update it when
; needed, as it is quite slow
If DrawAll
If StartDrawing(ImageOutput(#IMAGE_Color_Content1))
w = OutputWidth()
h = OutputHeight()
Center = w / 2
Radius = w / 2 - 10
; clear the center
DrawingMode(#PB_2DDrawing_Default)
Circle(Center, Center, *Entry\TriangleRadius, $FFFFFF)
DrawingMode(#PB_2DDrawing_Outlined)
Circle(Center, Center, *Entry\TriangleRadius, $000000)
; draw the triangle
x1 = Center + Cos(Radian(*Entry\h)) * (*Entry\TriangleRadius-2) ; inner radius at hue
y1 = Center + Sin(Radian(*Entry\h)) * (*Entry\TriangleRadius-2)
x2 = Center + Cos(Radian(*Entry\h + 120.0)) * (*Entry\TriangleRadius-2) ; inner radius at full v
y2 = Center + Sin(Radian(*Entry\h + 120.0)) * (*Entry\TriangleRadius-2)
x3 = Center + Cos(Radian(*Entry\h + 240.0)) * (*Entry\TriangleRadius-2) ; inner radius at full s
y3 = Center + Sin(Radian(*Entry\h + 240.0)) * (*Entry\TriangleRadius-2)
DrawingMode(#PB_2DDrawing_Default)
LineXY(x1, y1, x2, y2, $000000)
LineXY(x1, y1, x3, y3, $000000)
LineXY(x2, y2, x3, y3, $000000)
; fill the triangle
DrawingMode(#PB_2DDrawing_CustomFilter)
CustomFilterCallback(@ColorPicker_WheelCenterFilter())
FillArea(Center, Center, $000000)
StopDrawing()
EndIf
EndIf
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas1))
DrawImage(ImageID(#IMAGE_Color_Content1), 0, 0)
w = OutputWidth()
h = OutputHeight()
Center = w / 2
Radius = w / 2 - 10
x0 = Center + Cos(Radian(*Entry\h)) * (Radius-1) ; outer radius at hue
y0 = Center + Sin(Radian(*Entry\h)) * (Radius-1)
x1 = Center + Cos(Radian(*Entry\h)) * (*Entry\TriangleRadius+1) ; inner radius at hue
y1 = Center + Sin(Radian(*Entry\h)) * (*Entry\TriangleRadius+1)
LineXY(x0, y0, x1, y1, $FFFFFF)
; draw the cross marker
; do the action from the WheelCenterFilter() in reverse order
;
DrawingMode(#PB_2DDrawing_Default)
r_x.f = - ((*Entry\TriangleSide * *Entry\v) / 2) + (*Entry\s * (*Entry\TriangleSide * *Entry\v))
r_y.f = - (*Entry\TriangleHeight / 3) + ((1.0 - *Entry\v) * *Entry\TriangleHeight)
Rotate(*Entry\h + 30.0, @r_x, @r_y)
ColorPicker_DrawCross(Center + r_x, Center + r_y)
StopDrawing()
EndIf
ColorPicker_UpdateColor(*Entry, HSVToRGB(*Entry\h, *Entry\s, *Entry\v))
ProcedureReturn CanvasWidth ; actual height used
EndProcedure
Procedure ColorPicker_Wheel_Setup(*Entry.ColorPickerData, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
; use the smaller one as the size
If CanvasWidth < CanvasHeight
Size = CanvasWidth
Else
Size = CanvasHeight
CanvasX + (CanvasWidth-Size) / 2
EndIf
; create the image with the static content
;
CreateImage(#IMAGE_Color_Content1, DesktopScaledX(Size), DesktopScaledY(Size), 24)
If StartDrawing(ImageOutput(#IMAGE_Color_Content1))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $FFFFFF)
Center = w / 2
Radius = w / 2 - 10
If Radius > 150
*Entry\TriangleRadius = Radius - 30
Else
*Entry\TriangleRadius = Radius * 0.85
EndIf
; Precalculate some values
; The triangle in the middle is an equilateral triangle, so these are simple to calculate
; from the radius of the surrounding circle
;
*Entry\TriangleSide = (3.0 * *Entry\TriangleRadius) / Sqr(3.0)
*Entry\TriangleHeight = (3.0 * *Entry\TriangleRadius) / 2.0
DrawingMode(#PB_2DDrawing_CustomFilter)
CustomFilterCallback(@ColorPicker_WheelFilter())
Circle(Center, Center, Radius)
DrawingMode(#PB_2DDrawing_Outlined)
Circle(Center, Center, Radius, $000000)
StopDrawing()
EndIf
RGBToHSV(*Entry\RGBColor, @*Entry\h, @*Entry\s, @*Entry\v)
HideGadget(#GADGET_Color_Canvas1, 0)
HideGadget(#GADGET_Color_Canvas2, 1)
HideGadget(#GADGET_Color_Canvas3, 1)
HideGadget(#GADGET_Color_Scheme, 1)
HideGadget(#GADGET_Color_Scroll, 1)
HideGadget(#GADGET_Color_Filter, 1)
HideGadget(#GADGET_Color_Label1, 1)
HideGadget(#GADGET_Color_Label2, 1)
HideGadget(#GADGET_Color_Label3, 1)
ResizeGadget(#GADGET_Color_Canvas1, CanvasX, CanvasY, Size, Size)
ColorPicker_Wheel_Update(*Entry, #True)
ProcedureReturn Size
EndProcedure
Procedure ColorPicker_Wheel_Event(*Entry.ColorPickerData, Gadget, Type)
Static State
If Gadget = #GADGET_Color_Canvas1
; get info
w = DesktopScaledX(GadgetWidth(#GADGET_Color_Canvas1))
h = DesktopScaledY(GadgetHeight(#GADGET_Color_Canvas1))
x = GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseX)
y = GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseY)
Center = w / 2
Radius = (w / 2) - 10
; handle event
Select Type
Case #PB_EventType_LeftButtonDown
Distance.f = Sqr((x-Center)*(x-Center) + (y-Center)*(y-Center))
If Distance <= *Entry\TriangleRadius
State = 2 ; Mouse down in V/S area
ElseIf Distance <= Radius
State = 1 ; Mouse down in H area
EndIf
Case #PB_EventType_LeftButtonUp
State = 0
EndSelect
; update values
Select State
Case 1
*Entry\h = Mod(Degree(ATan2(x-Center, y-Center)) + 360, 360)
ColorPicker_Wheel_Update(*Entry, #True)
Case 2
; same action as the WheelCenterFilter()
r_x.f = x - Center
r_y.f = y - Center
Rotate(-(*Entry\h + 30.0), @r_x, @r_y)
; calculate v first, as we need it for s
*Entry\v = 1.0 - ((r_y + (*Entry\TriangleHeight / 3.0)) / *ColorPicker\TriangleHeight)
If *Entry\v < 0: *Entry\v = 0: EndIf
If *Entry\v > 1: *Entry\v = 1: EndIf
*Entry\s = (r_x + ((*Entry\TriangleSide * *Entry\v) / 2.0)) / (*Entry\TriangleSide * *Entry\v)
If *Entry\s < 0: *Entry\s = 0: EndIf
If *Entry\s > 1: *Entry\s = 1: EndIf
ColorPicker_Wheel_Update(*Entry, #False)
EndSelect
EndIf
EndProcedure
;- ----- Palette picker -----
Procedure ColorPicker_Palette_Update(*Entry.ColorPickerData)
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas1))
w = OutputWidth()
h = OutputHeight()
; if a palette has less items than can be displayed, it is important to remove old content
Box(0, 0, w, h, $FFFFFF)
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, w, h, $000000)
DrawingMode(#PB_2DDrawing_Default)
Color = *Entry\First
For row = 0 To *Entry\Rows-1
For col = 0 To *Entry\Cols-1
If Color >= *Palette\Count
Break 2
EndIf
x = col * DesktopScaledX(16)
y = row * DesktopScaledY(16)
Box(x, y, DesktopScaledX(17), DesktopScaledY(17), $000000)
Box(x+1, y+1, DesktopScaledX(15), DesktopScaledY(15), *Palette\Entry(Color)\Color)
Color + 1
Next col
Next row
StopDrawing()
EndIf
; No ColorPicker_UpdateColor() here, as the color only changes on a click
EndProcedure
Procedure ColorPicker_Palette_Setup(*Entry.ColorPickerData, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
; load the palettes
ColorPicker_LoadPalettes(*Entry)
; There is no backing image for this picker, as
; everything can be drawn directly on the canvas
ComboHeight = GetRequiredHeight(#GADGET_Color_Scheme)
*Entry\First = 0
*Entry\Cols = (CanvasWidth - 20) / 16
*Entry\Rows = (CanvasHeight - ComboHeight - 5) / 16
width = *Entry\Cols * 16 + 1
height = *Entry\Rows * 16 + 1
xoffset = CanvasX + ((CanvasWidth - 20) % 16) / 2
yoffset = CanvasY + 5 + ComboHeight + ((CanvasHeight - ComboHeight - 5) % 16) / 2
SetGadgetAttribute(#GADGET_Color_Scroll, #PB_ScrollBar_Maximum, Int(Round(*Palette\Count / *Entry\Cols, #PB_Round_Up)))
SetGadgetAttribute(#GADGET_Color_Scroll, #PB_ScrollBar_PageLength, *Entry\Rows)
SetGadgetState(#GADGET_Color_Scroll, 0)
If *Entry\Cols * *Entry\Rows >= *Palette\Count
DisableGadget(#GADGET_Color_Scroll, 1)
Else
DisableGadget(#GADGET_Color_Scroll, 0)
EndIf
HideGadget(#GADGET_Color_Canvas1, 0)
HideGadget(#GADGET_Color_Canvas2, 1)
HideGadget(#GADGET_Color_Canvas3, 1)
HideGadget(#GADGET_Color_Scheme, 0)
HideGadget(#GADGET_Color_Scroll, 0)
HideGadget(#GADGET_Color_Filter, 1)
HideGadget(#GADGET_Color_Label1, 1)
HideGadget(#GADGET_Color_Label2, 1)
HideGadget(#GADGET_Color_Label3, 1)
ResizeGadget(#GADGET_Color_Canvas1, xoffset, yoffset, width, height)
ResizeGadget(#GADGET_Color_Scheme, CanvasX, CanvasY, CanvasWidth, ComboHeight)
ResizeGadget(#GADGET_Color_Scroll, xoffset+width, yoffset, 20, height)
ColorPicker_Palette_Update(*Entry)
ColorPicker_UpdateColor(*Entry, *Entry\RGBColor)
ProcedureReturn CanvasHeight ; actual height used
EndProcedure
Procedure ColorPicker_Palette_Event(*Entry.ColorPickerData, Gadget, Type)
Select Gadget
Case #GADGET_Color_Scheme
index = GetGadgetState(#GADGET_Color_Scheme)
If index <> -1
SelectElement(PaletteList(), index)
If *Palette <> @PaletteList() ; only if the entry changed
*Palette = @PaletteList()
; trigger a resize which sets up the display of this new palette
ColorPicker_TriggerResize(*Entry)
EndIf
EndIf
Case #GADGET_Color_Scroll
*Entry\First = GetGadgetState(#GADGET_Color_Scroll) * *Entry\Cols
ColorPicker_Palette_Update(*Entry)
Case #GADGET_Color_Canvas1
If Type = #PB_EventType_LeftButtonDown
col = Round(GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseX) / DesktopScaledX(16), #PB_Round_Down)
row = Round(GetGadgetAttribute(#GADGET_Color_Canvas1, #PB_Canvas_MouseY) / DesktopScaledY(16), #PB_Round_Down)
Color = *Entry\First + row * *Entry\Cols + col
If Color >= 0 And Color < *Palette\Count
; valid color entry
*Entry\RGBColor = *Palette\Entry(Color)\Color
ColorPicker_Palette_Update(*Entry)
ColorPicker_UpdateColor(*Entry, *Entry\RGBColor)
EndIf
EndIf
EndSelect
EndProcedure
;- ----- Name picker -----
Procedure ColorPicker_Name_UpdateFilter(*Entry.ColorPickerData)
; make a copy of the real palette and sort it by name
FilteredPalette\Count = *Palette\Count
CopyArray(*Palette\Entry(), FilteredPalette\Entry())
SortStructuredArray(FilteredPalette\Entry(), #PB_Sort_Ascending|#PB_Sort_NoCase, OffsetOf(ColorEntry\Name$), #PB_String)
; apply the filter
Filter$ = LCase(Trim(ReplaceString(RemoveString(GetGadgetText(#GADGET_Color_Filter), Chr(9)), ",", " ")))
If Filter$
ReadEntry = 0
WriteEntry = 0
Words = CountString(Filter$, " ") + 1
For ReadEntry = 0 To FilteredPalette\Count-1
Name$ = LCase(FilteredPalette\Entry(ReadEntry)\Name$)
match = 1
For i = 1 To Words
Word$ = StringField(Filter$, i, " ")
If FindString(Name$, Word$, 1) = 0
match = 0
Break
EndIf
Next i
If match
If ReadEntry <> WriteEntry
FilteredPalette\Entry(WriteEntry)\Name$ = FilteredPalette\Entry(ReadEntry)\Name$
FilteredPalette\Entry(WriteEntry)\Color = FilteredPalette\Entry(ReadEntry)\Color
EndIf
WriteEntry + 1
EndIf
Next ReadEntry
FilteredPalette\Count = WriteEntry
EndIf
; update the scrollbar
SetGadgetAttribute(#GADGET_Color_Scroll, #PB_ScrollBar_Maximum, FilteredPalette\Count)
SetGadgetAttribute(#GADGET_Color_Scroll, #PB_ScrollBar_PageLength, *Entry\Rows)
*Entry\First = 0
SetGadgetState(#GADGET_Color_Scroll, 0)
If *Entry\Rows >= FilteredPalette\Count
DisableGadget(#GADGET_Color_Scroll, 1)
Else
DisableGadget(#GADGET_Color_Scroll, 0)
EndIf
EndProcedure
Procedure ColorPicker_Name_Update(*Entry.ColorPickerData)
If StartDrawing(CanvasOutput(#GADGET_Color_Canvas1))
DrawingFont(GetGadgetFont(#PB_Default))
w = OutputWidth()
h = OutputHeight()
Box(0, 0, w, h, $FFFFFF)
DrawingMode(#PB_2DDrawing_Default|#PB_2DDrawing_Transparent)
For row = 0 To *Entry\Rows ; draw also the last half row in the remaining space
If *Entry\First + row >= FilteredPalette\Count
Break
EndIf
y = 1 + row * *Entry\RowHeight
If row % 2
Box(1, y, w-2, *Entry\RowHeight, $E0E0E0)
Else
Box(1, y, w-2, *Entry\RowHeight, $FFFFFF)
EndIf
Box(w-3-*Entry\BoxWidth, y+2, *Entry\BoxWidth, *Entry\RowHeight-4, FilteredPalette\Entry(*Entry\First + row)\Color)
DrawText(5, y+4, FilteredPalette\Entry(*Entry\First + row)\Name$, $000000)
Next row
If FilteredPalette\Count = 0
DrawText(5, 5, Language("ToolsPanel", "NoMatch"), $808080)
EndIf
; do this last, to draw over the last maybe half row in the gadget
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, w, h, $000000)
StopDrawing()
EndIf
; No ColorPicker_UpdateColor() here, as the color only changes on a click
EndProcedure
Procedure ColorPicker_Name_Setup(*Entry.ColorPickerData, CanvasX, CanvasY, CanvasWidth, CanvasHeight)
; load the palettes
ColorPicker_LoadPalettes(*Entry)
; make sure the palette is sorted in alphabetic order
; There is no backing image for this picker, as
; everything can be drawn directly on the canvas
SetGadgetText(#GADGET_Color_Label1, Language("ToolsPanel", "Color_Filter")+": ")
GetRequiredSize(#GADGET_Color_Label1, @LabelWidth, @LabelHeight)
ComboHeight = GetRequiredHeight(#GADGET_Color_Scheme)
FilterHeight = Max(LabelHeight, GetRequiredHeight(#GADGET_Color_Filter))