-
Notifications
You must be signed in to change notification settings - Fork 17
/
Hybrid_BSC_HSL_ColorPickerDlgUnit.pas
1007 lines (908 loc) · 33.1 KB
/
Hybrid_BSC_HSL_ColorPickerDlgUnit.pas
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
//========================================================================
// Ultimate Color Picker Dialogs - Delphi Edition
//
// Copyright 2003-2005, SimpleWebsiteNavigation.com, All Rights Reserved.
//
// Datecode: 050929
//
// This software is shareware. You may evaluate it for free, but it you
// use it you must purchase a license from:
// www.components.SimpleWebsiteNavigation.com
//========================================================================
unit Hybrid_BSC_HSL_ColorPickerDlgUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
BSC_ColorPickerDlgUnit, ExtCtrls, StdCtrls, StrUtils;
type
THybrid_BSC_HSL_ColorPickerDlg_Mode =
(BSC_Only_ColorPicker_Mode, HSL_Only_ColorPicker_Mode, Any_Colorpicker_Mode);
THybrid_BSC_HSL_ColorPickerDlg = class(TBSC_ColorPickerDlg)
AllowedColorsModeRadioGroup: TRadioGroup;
HSLColorPickerGroupBox: TGroupBox;
HSLValuesLabel: TLabel;
LumPanel: TPanel;
LumSliderPanel: TPanel;
LumSliderImage: TImage;
LumArrowImage: TImage;
LumLeftSidePanel: TPanel;
LumImage: TImage;
LumMarkerShape: TShape;
HueSaturationPanel: TPanel;
HueSliderPanel: TPanel;
HueSliderImage: TImage;
HueArrowImage: TImage;
HueSaturationImagePanel: TPanel;
HueSaturationImage: TImage;
HueSaturationMarkerShape: TShape;
SatSliderPanel: TPanel;
SatSliderImage: TImage;
SatArrowImage: TImage;
UpdateDisplayTimer: TTimer;
HexColor: TEdit;
Label5: TLabel;
procedure FormActivate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure OKBtnClick(Sender: TObject);
procedure CancelBtnClick(Sender: TObject);
procedure HueSaturationImageMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure HueSaturationImageMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
procedure HueArrowImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure HueArrowImageMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure HueSliderImageMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure SatArrowImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SatArrowImageMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure SatSliderImageMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure LumArrowImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure LumArrowImageMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
procedure LumSliderImageMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure LumImageMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure LumImageMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure HandleMouseUp (Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure AllowedColorsModeRadioGroupClick(Sender: TObject);
procedure UpdateDisplayTimerTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure HexColorKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
mouse_state: integer;
grab_pt: integer;
grab_pt_value: real;
last_update_display_color: TColor;
mode: THybrid_BSC_HSL_ColorPickerDlg_Mode;
procedure set_HSL;
function rH: real;
procedure wH (hue: real);
property H: real read rH write wH;
function rS: real;
procedure wS (sat: real);
property S: real read rS write wS;
function rL: real;
procedure wL (lum: real);
property L: real read rL write wL;
procedure calculate_sizes;
procedure display_luminosity_range;
procedure regenerate_hue_saturation_image;
procedure display_HSL_values;
public
HSBitmapRegenerationExpected: boolean;
function Execute (Mode: THybrid_BSC_HSL_ColorPickerDlg_Mode): boolean;
procedure generate_new_hue_saturation_bitmap (fn: string);
end;
var
Hybrid_BSC_HSL_ColorPickerDlg: THybrid_BSC_HSL_ColorPickerDlg;
implementation
{$ifdef clr}
uses Borland.Vcl.Types;
{$endif}
{----------------------------------------------------------------------}
{ The conversion algorithms for HSL color spaces are from Chapter 17 }
{ of the book Fundamentals of Interactive Computer Graphics by Foley }
{ and van Dam, Addison-Wesley, 1982. }
{----------------------------------------------------------------------}
{$R *.DFM}
const
mouse_state_up = 0;
mouse_state_down = 1;
mouse_state_hue_left = 2;
mouse_state_hue_right = 3;
mouse_state_sat_up = 4;
mouse_state_sat_down = 5;
mouse_state_lum_up = 6;
mouse_state_lum_down = 7;
const
// Must correspond to the indexes of the Items in AllowedColorsModeRadioGroup
AllowedColors_BSCMode = 0;
AllowedColors_AllMode = 1;
// Delphi 2 doesn't implement assert
{$ifdef VER90}
procedure assert (b: boolean);
begin
end;
{$endif}
function iClamp (v: integer; min, max: integer): integer;
begin
assert (min <= max);
result := v;
if v < min
then
result := min
else
if v > max
then
result := max
end;
function rClamp (v: real; min, max: real): real;
begin
assert (min <= max);
result := v;
if v < min
then
result := min
else
if v > max
then
result := max
end;
function convert_HSL_to_RGB (H, S, L: real): TColor; // H,S,L are 0.0 .. 1.0
function RGB (R, G, B: real): TColor; // R,G,B are 0.0 .. 1.0
begin
assert ((0 <= R) and (R <= 1));
assert ((0 <= G) and (G <= 1));
assert ((0 <= B) and (B <= 1));
result := round(r*255) + (round(g*255) shl 8) + (round(b*255) shl 16)
end;
var
t1, t2: real;
function convert (t3: real): real;
begin
if t3 < 0
then
t3 := t3 + 1;
if t3 > 1
then
t3 := t3 - 1;
if (6*t3) < 1
then
result := t1 + ((t2-t1)*6*t3)
else if (2*t3) < 1
then
result := t2
else if (3*t3) < 2
then
result := t1 + ((t2-t1)*((2/3)-t3)*6)
else
result := t1;
assert ((0 <= result) and (result <= 1))
end;
begin
assert ((0 <= H) and (H <= 1));
assert ((0 <= S) and (S <= 1));
assert ((0 <= L) and (L <= 1));
if S = 0
then
result := RGB (L, L, L)
else
begin
if L < 0.5
then
t2 := L * (1 + S)
else
t2 := L + S - (L*S);
t1 := (2 * L) - t2;
result := RGB (convert(H+(1/3)), convert(H), convert(H-(1/3)))
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.calculate_sizes;
procedure draw_indented_border_panel (img: TImage;
indent_left, indent_top, indent_right, indent_bottom: integer
// indent values are to make bitmaps appear in correct place
);
begin
with img
do begin
Canvas.Brush.Color := TPanel(Parent).Color;
Canvas.FillRect (Rect (0,0,Width,Height));
Canvas.Pen.Color := RGB(128,128,128);
Canvas.MoveTo (indent_left, Height-1-indent_bottom);
Canvas.LineTo (indent_left, indent_top);
Canvas.LineTo (Width-1-indent_right, indent_top);
Canvas.Pen.Color := clWhite;
Canvas.LineTo (Width-1-indent_right, Height-1-indent_bottom);
Canvas.LineTo (indent_left, Height-1-indent_bottom)
end
end;
procedure draw_horizontal_indented_border_panel (img: TImage;
caption: string;
indent_left, indent_right: integer
);
begin
draw_indented_border_panel (img, indent_left, 0, indent_right, 0);
with img.Canvas
do TextOut ((img.Width - TextWidth(caption)) div 2,
(img.Height - TextHeight(caption)) div 2,
caption
)
end;
procedure draw_vertical_indented_border_panel (img: TImage;
caption: string;
indent_top, indent_bottom: integer
);
var
vertical_font, old_font: HFont;
vertical_font_info: TLogFont;
begin
draw_indented_border_panel (img, 0, indent_top, 0, indent_bottom);
img.Canvas.Font.Name := 'Arial';
img.Canvas.Font.Size := 8;
{$ifdef clr}
if GetObject (img.Canvas.Font.Handle, sizeOf (vertical_font_info), vertical_font_info) = 0
{$else}
if GetObject (img.Canvas.Font.Handle, sizeOf (vertical_font_info), @vertical_font_info) = 0
{$endif}
then
raise Exception.Create ('cant create vertical font');
vertical_font_info.lfEscapement := 900;
vertical_font_info.lfOrientation := 900;
vertical_font_info.lfQuality := PROOF_QUALITY;
vertical_font := CreateFontIndirect (vertical_font_info);
assert (vertical_font <> 0);
old_font := SelectObject (img.Canvas.Handle, vertical_font);
assert (old_font <> 0);
img.Canvas.TextOut (((img.Width - img.Canvas.TextHeight(caption)) div 2) + 1,
(img.Height + img.Canvas.TextWidth(caption)) div 2,
caption
);
old_font := SelectObject (img.Canvas.Handle, old_font);
assert (old_font <> 0);
DeleteObject (vertical_font)
end;
begin
HSLColorPickerGroupBox.Left := 0;
HSLColorPickerGroupBox.Top := 0;
HSLColorPickerGroupBox.Width := ColorPickerPanel.Width;
HSLColorPickerGroupBox.Height := ColorPickerPanel.Height;
LumPanel.Left := HSLColorPickerGroupBox.Width - 68;
LumPanel.Top := 16;
LumPanel.Width := 50;
LumPanel.Height := HSLColorPickerGroupBox.Height - 39;
HueSaturationPanel.Left := 8;
HueSaturationPanel.Top := 16;
HueSaturationPanel.Width := LumPanel.Left - 23;
HueSaturationPanel.Height := HSLColorPickerGroupBox.Height - 56;
HueSaturationImagePanel.Left := 8;
HueSaturationImagePanel.Top := 12;
HueSaturationImagePanel.Width := HueSaturationPanel.Width - 25;
HueSaturationImagePanel.Height := HueSaturationPanel.Height - 29;
HueSaturationImage.Left := 0;
HueSaturationImage.Top := 0;
HueSaturationImage.Width := HueSaturationImagePanel.Width;
HueSaturationImage.Height := HueSaturationImagePanel.Height;
HueSliderPanel.Left := 0;
HueSliderPanel.Top := HueSaturationPanel.Height - 17;
HueSliderPanel.Width := HueSaturationPanel.Width - 9;
HueSliderPanel.Height := 17;
HueSliderImage.Left := 0;
HueSliderImage.Top := 0;
HueSliderImage.Width := HueSliderPanel.Width;
HueSliderImage.Height := HueSliderPanel.Height;
HueArrowImage.Top := 0;
SatSliderPanel.Left := HueSaturationPanel.Width - 17;
SatSliderPanel.Top := 0;
SatSliderPanel.Width := 17;
SatSliderPanel.Height := HueSaturationPanel.Height - 13;
SatSliderImage.Left := 0;
SatSliderImage.Top := 0;
SatSliderImage.Width := SatSliderPanel.Width;
SatSliderImage.Height := SatSliderPanel.Height;
SatArrowImage.Left := 0;
LumSliderPanel.Left := LumPanel.Width - 17;
LumSliderPanel.Top := 0;
LumSliderPanel.Width := 17;
LumSliderPanel.Height := LumPanel.Height;
LumSliderImage.Left := 0;
LumSliderImage.Top := 0;
LumSliderImage.Width := LumSliderPanel.Width;
LumSliderImage.Height := LumSliderPanel.Height;
LumLeftSidePanel.Left := 0;
LumLeftSidePanel.Top := 0;
LumLeftSidePanel.Width := LumPanel.Width - LumSliderPanel.Width;
LumLeftSidePanel.Height := LumPanel.Height;
LumImage.Left := 0;
LumImage.Top := 12;
LumImage.Width := LumLeftSidePanel.Width;
LumImage.Height := LumLeftSidePanel.Height - 29;
LumMarkerShape.Left := (LumImage.Width div 2) - 2;
LumArrowImage.Left := 0;
HSLValuesLabel.Left := (HSLColorPickerGroupBox.Width - HSLValuesLabel.Width) div 2;
HSLValuesLabel.Top := HSLColorPickerGroupBox.Height - 25;
draw_horizontal_indented_border_panel (HueSliderImage, 'Hue', 8, 8);
draw_vertical_indented_border_panel (SatSliderImage, 'Saturation', 12, 5);
draw_vertical_indented_border_panel (LumSliderImage, 'Brightness', 12, 17);
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.FormActivate (Sender: TObject);
begin
old_color := ColorPick;
calculate_sizes;
case Mode of
BSC_Only_ColorPicker_Mode:
begin
AllowedColorsModeRadioGroup.Visible := false;
AllowedColorsModeRadioGroup.ItemIndex := AllowedColors_BSCMode
end;
HSL_Only_ColorPicker_Mode:
begin
AllowedColorsModeRadioGroup.Visible := false;
AllowedColorsModeRadioGroup.ItemIndex := AllowedColors_AllMode
end;
Any_Colorpicker_Mode:
begin
AllowedColorsModeRadioGroup.Visible := true
end
end;
AllowedColorsModeRadioGroup.Visible := Mode = Any_Colorpicker_Mode;
if AllowedColorsModeRadioGroup.ItemIndex = AllowedColors_BSCMode
then
begin
HSLColorPickerGroupBox.Visible := false;
BSCColorPickerGroupBox.Visible := true;
bscFormActivate
end
else
begin
HSLColorPickerGroupBox.Visible := true;
BSCColorPickerGroupBox.Visible := false;
set_HSL;
display_luminosity_range
end;
UpdateDialogPreview;
// this part must be last within FormActivate to ensure that the possible exception is harmless
with HueSaturationImage
do if (Width <> Picture.Bitmap.Width)
or
(Height <> Picture.Bitmap.Height)
then
begin
regenerate_hue_saturation_image;
{if not HSBitmapRegenerationExpected
then
raise Exception.Create ('Hue/Image Bitmap Size Problem - contact program vendor')
// If you are seeing this exception, please consult the documentation about
// "Regenerating the Hue/Saturation Color Map Bitmap".}
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.FormPaint (Sender: TObject);
begin
if AllowedColorsModeRadioGroup.ItemIndex = AllowedColors_BSCMode
then
inherited
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.OKBtnClick (Sender: TObject);
begin
inherited;
if UpdateDisplayTimer.Enabled
and
(last_update_display_color <> ColorPick)
then
fire_color_change_event;
UpdateDisplayTimer.Enabled := false;
mouse_state := mouse_state_up
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.CancelBtnClick (Sender: TObject);
begin
inherited;
UpdateDisplayTimer.Enabled := false;
mouse_state := mouse_state_up
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HueSaturationImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
var
clicked_color: TColor;
begin
mouse_state := mouse_state_down;
X := iClamp (X, 0, HueSaturationImage.Width-1);
Y := iClamp (Y, 0, HueSaturationImage.Height-1);
H := X / (HueSaturationImage.Width-1);
S := 1 - (Y / (HueSaturationImage.Height-1));
clicked_color := convert_HSL_to_RGB (H, S, L);
if ColorPick <> clicked_color
then
begin
ColorPick := clicked_color;
fire_color_change_event;
UpdateDialogPreview;
display_luminosity_range;
last_update_display_color := ColorPick
end;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HueSaturationImageMouseMove
(Sender: TObject;
Shift: TShiftState;
X, Y: Integer
);
begin
if mouse_state = mouse_state_down
then
begin
X := iClamp (X, 0, HueSaturationImage.Width-1);
Y := iClamp (Y, 0, HueSaturationImage.Height-1);
H := X / (HueSaturationImage.Width-1);
S := 1 - (Y / (HueSaturationImage.Height-1));
ColorPick := convert_HSL_to_RGB (H, S, L);
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HueArrowImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
mouse_state := mouse_state_down;
grab_pt := X;
last_update_display_color := ColorPick;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HueArrowImageMouseMove
(Sender: TObject;
Shift: TShiftState;
X, Y: Integer
);
begin
if mouse_state = mouse_state_down
then
begin
H := rClamp (H + ((X-grab_pt) / (HueSaturationImage.Width-1)), 0, 1);
ColorPick := convert_HSL_to_RGB (H, S, L)
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HueSliderImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
if X > HueArrowImage.Left
then
begin
H := H + (1 / HueSaturationImage.Width);
mouse_state := mouse_state_hue_left
end
else
begin
H := H - (1 / HueSaturationImage.Width);
mouse_state := mouse_state_hue_right
end;
ColorPick := convert_HSL_to_RGB (H, S, L);
display_luminosity_range;
fire_color_change_event;
UpdateDialogPreview;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.SatArrowImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
mouse_state := mouse_state_down;
grab_pt := SatArrowImage.Top + Y;
grab_pt_value := S;
last_update_display_color := ColorPick;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.SatArrowImageMouseMove
(Sender: TObject;
Shift: TShiftState;
X, Y: Integer
);
begin
if mouse_state = mouse_state_down
then
begin
S := rClamp (grab_pt_value + ((grab_pt - (SatArrowImage.Top + Y)) / (HueSaturationImage.Height-1)), 0, 1);
ColorPick := convert_HSL_to_RGB (H, S, L)
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.SatSliderImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
if Y > SatArrowImage.Top
then
begin
S := S - (1 / HueSaturationImage.Height);
mouse_state := mouse_state_sat_down
end
else
begin
S := S + (1 / HueSaturationImage.Height);
mouse_state := mouse_state_sat_up
end;
ColorPick := convert_HSL_to_RGB (H, S, L);
display_luminosity_range;
fire_color_change_event;
UpdateDialogPreview;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.LumImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
var
clicked_color: TColor;
begin
mouse_state := mouse_state_down;
Y := iClamp (Y, 0, LumImage.Height-1);
L := 1 - (Y / (LumImage.Height-1));
clicked_color := convert_HSL_to_RGB (H, S, L);
if ColorPick <> clicked_color
then
begin
ColorPick := clicked_color;
fire_color_change_event;
UpdateDialogPreview;
display_luminosity_range;
last_update_display_color := Color
end;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.LumImageMouseMove
(Sender: TObject;
Shift: TShiftState;
X, Y: Integer
);
begin
if mouse_state = mouse_state_down
then
begin
Y := iClamp (Y, 0, LumImage.Height-1);
L := 1 - (Y / (LumImage.Height-1));
ColorPick := convert_HSL_to_RGB (H, S, L)
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.LumArrowImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
mouse_state := mouse_state_down;
grab_pt := LumArrowImage.Top + Y;
grab_pt_value := L;
last_update_display_color := ColorPick;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.LumArrowImageMouseMove
(Sender: TObject;
Shift: TShiftState;
X, Y: Integer
);
begin
if mouse_state = mouse_state_down
then
begin
L := rClamp (grab_pt_value - (((LumArrowImage.Top + Y) - grab_pt) / (LumImage.Height-1)), 0, 1);
ColorPick := convert_HSL_to_RGB (H, S, L)
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.LumSliderImageMouseDown
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
if Y > LumArrowImage.Top
then
begin
L := L - (1 / LumImage.Height);
mouse_state := mouse_state_lum_down
end
else
begin
L := L + (1 / LumImage.Height);
mouse_state := mouse_state_lum_up
end;
ColorPick := convert_HSL_to_RGB (H, S, L);
fire_color_change_event;
UpdateDialogPreview;
UpdateDisplayTimer.Enabled := true
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HandleMouseUp
(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer
);
begin
if mouse_state <> mouse_state_down
then
UpdateDisplayTimer.Enabled := false;
mouse_state := mouse_state_up
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.AllowedColorsModeRadioGroupClick (Sender: TObject);
begin
case AllowedColorsModeRadioGroup.ItemIndex of
AllowedColors_BSCMode:
begin
bscActivate;
BSCColorPickerGroupBox.Visible := true;
HSLColorPickerGroupBox.Visible := false
end;
AllowedColors_AllMode:
begin
BSCColorPickerGroupBox.Visible := false;
HSLColorPickerGroupBox.Visible := true;
set_HSL;
display_luminosity_range
end
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.UpdateDisplayTimerTimer (Sender: TObject);
procedure update_display;
begin
display_luminosity_range;
fire_color_change_event;
UpdateDialogPreview;
end;
begin
case mouse_state of
mouse_state_up:
UpdateDisplayTimer.Enabled := false;
mouse_state_down:
if last_update_display_color <> ColorPick
then
begin
update_display;
last_update_display_color := ColorPick
end;
mouse_state_hue_left:
begin
H := rClamp (H + (1 / HueSaturationImage.Width), 0, 1);
update_display
end;
mouse_state_hue_right:
begin
H := rClamp (H - (1 / HueSaturationImage.Width), 0, 1);
update_display
end;
mouse_state_sat_up:
begin
S := rClamp (S + (1 / HueSaturationImage.Height), 0, 1);
update_display
end;
mouse_state_sat_down:
begin
S := rClamp (S - (1 / HueSaturationImage.Height), 0, 1);
update_display
end;
mouse_state_lum_up:
begin
L := rClamp (L + (1 / LumImage.Height), 0, 1);
update_display
end;
mouse_state_lum_down:
begin
L := rClamp (L - (1 / LumImage.Height), 0, 1);
update_display
end
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.set_HSL;
var
r, g, b, maxcolor, mincolor, new_h: real;
procedure get_max_min (c: real);
begin
if c > maxcolor
then
maxcolor := c;
if c < mincolor
then
mincolor := c
end;
begin
r := (ColorPick and $ff) / 255;
g := ((ColorPick shr 8) and $ff) / 255;
b := ((ColorPick shr 16) and $ff) / 255;
maxcolor := 0;
mincolor := 1;
get_max_min (r);
get_max_min (g);
get_max_min (b);
L := (maxcolor + mincolor) / 2;
if maxcolor = mincolor
then // some kind of gray
begin
S := 0;
H := 0
end
else
begin
if L < 0.5
then
S := (maxcolor-mincolor)/(maxcolor+mincolor)
else
S := (maxcolor-mincolor)/(2-maxcolor-mincolor);
if r = maxcolor
then new_h := ((g-b)/(maxcolor-mincolor))
else if g = maxcolor
then new_h := (2 + ((b-r)/(maxcolor-mincolor)))
else // b=maxcolor
new_h := (4 + ((r-g)/(maxcolor-mincolor)));
if new_h < 0
then
new_h := new_h + 6;
H := new_h / 6
end
end;
function THybrid_BSC_HSL_ColorPickerDlg.rH: real;
begin
rH := (HueSaturationMarkerShape.Left+2) / (HueSaturationImage.Width-1)
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.wH (hue: real);
var x: integer;
begin
assert ((0 <= hue) and (hue <= 1));
x := round ((HueSaturationImage.Width-1) * hue);
// fudge to get bitmaps in right place:
HueSaturationMarkerShape.Left := x - 2;
HueArrowImage.Left := x + 0;
display_HSL_values
end;
function THybrid_BSC_HSL_ColorPickerDlg.rS: real;
begin
rS := 1 - ((HueSaturationMarkerShape.Top+2) / (HueSaturationImage.Height-1))
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.wS (sat: real);
var y: integer;
begin
assert ((0 <= sat) and (sat <= 1));
y := round ((HueSaturationImage.Height-1) * (1-sat));
// fudge to get bitmaps in right place:
HueSaturationMarkerShape.Top := y - 2;
SatArrowImage.Top := y + 4;
display_HSL_values
end;
function THybrid_BSC_HSL_ColorPickerDlg.rL: real;
begin
rL := 1 - ((LumMarkerShape.Top - LumImage.Top + 2) / (LumImage.Height-1))
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.wL (lum: real);
var y: integer;
begin
assert ((0 <= lum) and (lum <= 1));
y := round ((LumImage.Height-1) * (1-lum));
// fudge to get bitmaps in right place:
LumMarkerShape.Top := LumImage.Top + y - 2;
LumArrowImage.Top := y + 4;
display_HSL_values
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.display_luminosity_range;
var
i: integer;
begin
for i := 0 to LumImage.Height-1
do begin
LumImage.Canvas.Brush.Color :=
convert_HSL_to_RGB (H, S, 1 - (i/(LumImage.Height-1)));
LumImage.Canvas.FillRect (Rect (0, i, LumImage.Width, i+1))
end;
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.regenerate_hue_saturation_image;
var
h, s: integer;
begin
calculate_sizes;
with HueSaturationImage
do begin
Picture.Bitmap.Width := Width;
Picture.Bitmap.Height := Height;
for h := 0 to Width-1
do for s := 0 to Height-1
do Canvas.Pixels[h,s] :=
convert_HSL_to_RGB (h / (Width-1),
1 - (s / (Height-1)),
0.5
)
end
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.display_HSL_values;
begin
HexColor.Text := Format('%.2x%.2x%.2x', [byte(ColorPick), byte(ColorPick shr 8), byte(ColorPick shr 16)]);
HSLValuesLabel.Caption :=
format ('Hue = %3d° Saturation = %3d%s Brightness = %3d%s',
[round (H*360), round(S*100), '%', round(L*100), '%']
)
end;
function THybrid_BSC_HSL_ColorPickerDlg.Execute (Mode: THybrid_BSC_HSL_ColorPickerDlg_Mode): boolean;
begin
self.Mode := Mode;
result := ShowModal = mrOk
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.generate_new_hue_saturation_bitmap (fn: string);
begin
regenerate_hue_saturation_image;
HueSaturationImage.Picture.SaveToFile (fn)
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.FormCreate(Sender: TObject);
begin
inherited;
// Delphi 2's TImage component doesn't implement the Transparent property,
// setting it when available is an aesthetic improvement.
{$ifndef VER90}
HueArrowImage.Transparent := true;
SatArrowImage.Transparent := true;
LumArrowImage.Transparent := true;
{$endif}
end;
procedure THybrid_BSC_HSL_ColorPickerDlg.HexColorKeyUp(Sender: TObject;
var Key: Word; Shift: TShiftState);
const
ValidChars: array[0..22] of string = (
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'a', 'b', 'c', 'd', 'e', 'f', '#'
);
var
i: Integer;
Valid: Boolean;
s, Color: string;
begin
inherited;
Color := HexColor.Text;
if Trim(Color) = '' then Exit;
if not (Length(Color) in [6, 7]) then Exit;
if (Length(Color) = 7) and not (Color[1] = '#') then
begin
HexColor.Text := copy(HexColor.Text, 1, 6);
Exit;
end;
Valid := True;
for i := 1 to Length(Color) do
if not AnsiMatchStr(Color[i], ValidChars) then
begin
Valid := False;
Break;
end;
if not Valid then Exit;
if Color[1] = '#' then
s := '$00'+Color[6]+Color[7]+Color[4]+Color[5]+Color[2]+Color[3]
else if Length(Color) = 6 then
s := '$00'+Color[5]+Color[6]+Color[3]+Color[4]+Color[1]+Color[2];
ColorPick := StringToColor(s);
last_update_display_color := ColorPick;
set_HSL;
display_HSL_values;
display_luminosity_range;