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
|
unit frm_main;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, fpg_base, fpg_main, fpg_widget,
fpg_edit, fpg_form, fpg_label, fpg_button,
fpg_dialogs, fpg_menu, fpg_checkbox, fpg_listbox, fpg_combobox,
fpg_panel, fpg_ColorWheel, fpg_spinedit;
type
TColorPickedEvent = procedure(Sender: TObject; const AMousePos: TPoint; const AColor: TfpgColor) of object;
TPickerButton = class(TfpgButton)
private
FContinuousResults: Boolean;
FOnColorPicked: TColorPickedEvent;
FColorPos: TPoint;
FColor: TfpgColor;
FColorPicking: Boolean;
private
procedure DoColorPicked;
protected
procedure HandleLMouseDown(X, Y: integer; ShiftState: TShiftState); override;
procedure HandleLMouseUp(x, y: integer; shiftstate: TShiftState); override;
procedure HandleMouseMove(x, y: integer; btnstate: word; shiftstate: TShiftState); override;
public
constructor Create(AOwner: TComponent); override;
published
property ContinuousResults: Boolean read FContinuousResults write FContinuousResults;
property OnColorPicked: TColorPickedEvent read FOnColorPicked write FOnColorPicked;
end;
TMainForm = class(TfpgForm)
private
{@VFD_HEAD_BEGIN: MainForm}
Button1: TfpgButton;
ColorWheel1: TfpgColorWheel;
ValueBar1: TfpgValueBar;
Bevel1: TfpgBevel;
Label1: TfpgLabel;
Label2: TfpgLabel;
Label3: TfpgLabel;
edH: TfpgEdit;
edS: TfpgEdit;
edV: TfpgEdit;
Label4: TfpgLabel;
Label5: TfpgLabel;
Label6: TfpgLabel;
edR: TfpgSpinEdit;
edG: TfpgSpinEdit;
edB: TfpgSpinEdit;
Label10: TfpgLabel;
cbColors: TfpgComboBox;
lbColors: TfpgColorListBox;
lblHex: TfpgLabel;
eHex: TfpgEdit;
Label7: TfpgLabel;
Label8: TfpgLabel;
Bevel2: TfpgBevel;
Label9: TfpgLabel;
chkCrossHair: TfpgCheckBox;
chkBGColor: TfpgCheckBox;
btnPicker: TPickerButton;
chkContinuous: TfpgCheckBox;
{@VFD_HEAD_END: MainForm}
FViaRGB: Boolean; // to prevent recursive changes
FColorPicking: Boolean;
procedure btnColorPicked(Sender: TObject; const AMousePos: TPoint; const AColor: TfpgColor);
procedure chkContinuousChanged(Sender: TObject);
procedure btnQuitClicked(Sender: TObject);
procedure chkCrossHairChange(Sender: TObject);
procedure chkBGColorChange(Sender: TObject);
procedure UpdateHSVComponents;
procedure UpdateRGBComponents;
procedure ColorChanged(Sender: TObject);
procedure RGBChanged(Sender: TObject);
procedure RGBChanging;
procedure ConvertToInt(Value: string);
procedure eHexKeyChar(Sender: TObject; AChar: TfpgChar; var Consumed: boolean);
procedure eHexKeyPress(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState;
var Consumed: boolean);
procedure PopulatePaletteColorCombo;
procedure cbColorsChange(Sender: TObject);
procedure lbColorsChange(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
procedure AfterCreate; override;
end;
{@VFD_NEWFORM_DECL}
implementation
{@VFD_NEWFORM_IMPL}
procedure TMainForm.ConvertToInt(Value: string);
var
iRed, iGreen, iBlue: integer;
i, iTemp : integer;
HexVal: string;
begin
for i:= 2 to 7 do
begin
HexVal:= Copy(Value,i,1);
case Uppercase(HexVal) of
'F':
iTemp:= 15;
'E':
iTemp:= 14;
'D':
iTemp:= 13;
'C':
iTemp:= 12;
'B':
iTemp:= 11;
'A':
iTemp:= 10
else
if (HexVal>= '0') and (HexVal<= '9') then
iTemp:= StrToInt(HexVal);
end;
case i of
2:
iRed:= iTemp;
3:
iRed:= iRed * 16 +iTemp;
4:
iGreen:= iTemp;
5:
iGreen:= iGreen * 16 +iTemp;
6:
iBlue:= iTemp;
7:
iBlue:= iBlue * 16 +iTemp;
end;
end;
edR.Value := iRed;
edG.Value := iGreen;
edB.Value := iBlue;
end;
function ConvertToHexa(Value: Integer): string;
var
ValH,ValL: Integer;
begin
ValH:= Value div 16;
ValL:= Value mod 16;
case ValH of
15:
Result:= 'F';
14:
Result:= 'E';
13:
Result:= 'D';
12:
Result:= 'C';
11:
Result:= 'B';
10:
Result:= 'A';
else
Result:= IntToStr(ValH);
end;
case ValL of
15:
Result:= Result+'F';
14:
Result:= Result+'E';
13:
Result:= Result+'D';
12:
Result:= Result+'C';
11:
Result:= Result+'B';
10:
Result:= Result+'A';
else
Result:= Result+IntToStr(ValL);
end;
end;
function Hexa(Red,Green,Blue: Integer): string;
begin
Result:= '$'+ConvertToHexa(Red)+ConvertToHexa(Green)+ConvertToHexa(Blue);
end;
{ TPickerButton }
procedure TPickerButton.DoColorPicked;
var
pt: TPoint;
begin
pt := WindowToScreen(self, FColorPos);
FColor := fpgApplication.GetScreenPixelColor(pt);
if Assigned(FOnColorPicked) then
FOnColorPicked(self, FColorPos, FColor);
end;
procedure TPickerButton.HandleLMouseDown(X, Y: integer; ShiftState: TShiftState);
begin
inherited HandleLMouseDown(X, Y, ShiftState);
MouseCursor := mcCross;
FColorPicking := True;
CaptureMouse;
end;
procedure TPickerButton.HandleLMouseUp(x, y: integer; shiftstate: TShiftState);
begin
inherited HandleLMouseUp(x, y, shiftstate);
ReleaseMouse;
FColorPicking := False;
MouseCursor := mcDefault;
DoColorPicked;
end;
procedure TPickerButton.HandleMouseMove(x, y: integer; btnstate: word;
shiftstate: TShiftState);
begin
//inherited HandleMouseMove(x, y, btnstate, shiftstate);
if not FColorPicking then
Exit;
FColorPos.x := x;
FColorPos.y := y;
if FContinuousResults then
DoColorPicked;
end;
constructor TPickerButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColorPicking := False;
FContinuousResults := False;
end;
procedure TMainForm.ColorChanged(Sender: TObject);
begin
UpdateHSVComponents;
if not FViaRGB then
UpdateRGBComponents;
end;
procedure TMainForm.RGBChanged(Sender: TObject);
begin
RGBChanging;
end;
procedure TMainForm.RGBChanging;
var
rgb: TRGBTriple;
c: TfpgColor;
begin
FViaRGB := True; // revent recursive updates
rgb.Red := edR.Value;
rgb.Green := edG.Value;
rgb.Blue := edB.Value;
c := RGBTripleTofpgColor(rgb);
ColorWheel1.SetSelectedColor(c); // This will trigger ColorWheel and ValueBar OnChange event
FViaRGB := False;
eHex.Text:= Hexa(rgb.Red,rgb.Green,rgb.Blue);
end;
constructor TMainForm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FViaRGB := False;
FColorPicking := False;
end;
procedure TMainForm.btnColorPicked(Sender: TObject; const AMousePos: TPoint; const AColor: TfpgColor);
begin
ColorWheel1.SetSelectedColor(AColor);
end;
procedure TMainForm.chkContinuousChanged(Sender: TObject);
begin
btnPicker.ContinuousResults := chkContinuous.Checked;
end;
procedure TMainForm.btnQuitClicked(Sender: TObject);
begin
Close;
end;
procedure TMainForm.chkCrossHairChange(Sender: TObject);
begin
if chkCrossHair.Checked then
ColorWheel1.CursorSize := 400
else
ColorWheel1.CursorSize := 5;
end;
procedure TMainForm.chkBGColorChange(Sender: TObject);
begin
if chkBGColor.Checked then
begin
ColorWheel1.BackgroundColor := clBrown;
ValueBar1.BackgroundColor := clBrown;
end
else
begin
ColorWheel1.BackgroundColor := clWindowBackground;
ValueBar1.BackgroundColor := clWindowBackground;
end;
end;
procedure TMainForm.UpdateHSVComponents;
begin
edH.Text := IntToStr(ColorWheel1.Hue);
edS.Text := FormatFloat('0.000', ColorWheel1.Saturation);
edV.Text := FormatFloat('0.000', ValueBar1.Value);
Bevel1.BackgroundColor := ValueBar1.SelectedColor;
end;
procedure TMainForm.UpdateRGBComponents;
var
rgb: TRGBTriple;
c: TfpgColor;
begin
c := ValueBar1.SelectedColor;
rgb := fpgColorToRGBTriple(c);
edR.Value := rgb.Red;
edG.Value := rgb.Green;
edB.Value := rgb.Blue;
eHex.Text:= Hexa(rgb.Red,rgb.Green,rgb.Blue);
end;
procedure TMainForm.eHexKeyChar(Sender: TObject; AChar: TfpgChar; var Consumed: boolean);
begin
if Length(eHex.Text)= 0 then
begin
if AChar<> '$' then
Consumed:= True;
end
else
if ((AChar< '0') or (AChar> '9')) and ((AChar< 'A') or (AChar> 'F')) and ((AChar< 'a') or (AChar> 'f')) then
Consumed:= True;
end;
procedure TMainForm.eHexKeyPress(Sender: TObject; var KeyCode: word; var ShiftState: TShiftState;
var Consumed: boolean);
begin
if ((KeyCode= KeyReturn) or (KeyCode= KeyPEnter)) and (Length(eHex.Text)= 7) then
begin
ConvertToInt(eHex.Text);
RGBChanging;
end;
end;
procedure TMainForm.PopulatePaletteColorCombo;
begin
with cbColors do
begin
Items.Clear;
Items.Add('cpStandardColors');
Items.Add('cpSystemColors');
Items.Add('cpWebColors');
FocusItem := 0;
OnChange := @cbColorsChange;
end;
end;
procedure TMainForm.cbColorsChange(Sender: TObject);
begin
if cbColors.Text = 'cpStandardColors' then
lbColors.ColorPalette := cpStandardColors
else if cbColors.Text = 'cpSystemColors' then
lbColors.ColorPalette := cpSystemColors
else
lbColors.ColorPalette := cpWebColors;
end;
procedure TMainForm.lbColorsChange(Sender: TObject);
var
rgb: TRGBTriple;
c: TfpgColor;
begin
c := lbColors.Color;
rgb := fpgColorToRGBTriple(c);
edR.Value := rgb.Red;
edG.Value := rgb.Green;
edB.Value := rgb.Blue;
eHex.Text:= Hexa(rgb.Red,rgb.Green,rgb.Blue);
ConvertToInt(eHex.Text);
RGBChanging;
end;
procedure TMainForm.AfterCreate;
begin
{@VFD_BODY_BEGIN: MainForm}
Name := 'MainForm';
SetPosition(0, 0, 540, 420);
WindowTitle := 'ColorWheel test app';
Hint := '';
WindowPosition := wpScreenCenter;
Button1 := TfpgButton.Create(self);
with Button1 do
begin
Name := 'Button1';
SetPosition(447, 376, 80, 26);
Anchors := [anRight,anBottom];
Text := 'Quit';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 0;
OnClick := @btnQuitClicked;
end;
ColorWheel1 := TfpgColorWheel.Create(self);
with ColorWheel1 do
begin
Name := 'ColorWheel1';
SetPosition(12, 20, 272, 244);
end;
ValueBar1 := TfpgValueBar.Create(self);
with ValueBar1 do
begin
Name := 'ValueBar1';
SetPosition(290, 20, 52, 244);
Value := 1;
OnChange := @ColorChanged;
end;
Bevel1 := TfpgBevel.Create(self);
with Bevel1 do
begin
Name := 'Bevel1';
SetPosition(20, 288, 76, 56);
Hint := '';
end;
Label1 := TfpgLabel.Create(self);
with Label1 do
begin
Name := 'Label1';
SetPosition(116, 284, 52, 18);
Alignment := taRightJustify;
FontDesc := '#Label1';
Hint := '';
Text := 'Hue';
end;
Label2 := TfpgLabel.Create(self);
with Label2 do
begin
Name := 'Label2';
SetPosition(116, 316, 52, 18);
Alignment := taRightJustify;
FontDesc := '#Label1';
Hint := '';
Text := 'Sat';
end;
Label3 := TfpgLabel.Create(self);
with Label3 do
begin
Name := 'Label3';
SetPosition(116, 344, 52, 18);
Alignment := taRightJustify;
FontDesc := '#Label1';
Hint := '';
Text := 'Val';
end;
edH := TfpgEdit.Create(self);
with edH do
begin
Name := 'edH';
SetPosition(172, 280, 56, 26);
TabOrder := 8;
Text := '';
FontDesc := '#Edit1';
BackgroundColor := clWindowBackground;
end;
edS := TfpgEdit.Create(self);
with edS do
begin
Name := 'edS';
SetPosition(172, 308, 56, 26);
TabOrder := 9;
Text := '';
FontDesc := '#Edit1';
BackgroundColor := clWindowBackground;
end;
edV := TfpgEdit.Create(self);
with edV do
begin
Name := 'edV';
SetPosition(172, 336, 56, 26);
TabOrder := 10;
Text := '';
FontDesc := '#Edit1';
BackgroundColor := clWindowBackground;
end;
Label4 := TfpgLabel.Create(self);
with Label4 do
begin
Name := 'Label4';
SetPosition(230, 284, 56, 18);
Alignment := taRightJustify;
FontDesc := '#Label1';
Hint := '';
Text := 'Red';
end;
Label5 := TfpgLabel.Create(self);
with Label5 do
begin
Name := 'Label5';
SetPosition(230, 316, 56, 18);
Alignment := taRightJustify;
FontDesc := '#Label1';
Hint := '';
Text := 'Green';
end;
Label6 := TfpgLabel.Create(self);
with Label6 do
begin
Name := 'Label6';
SetPosition(230, 344, 56, 18);
Alignment := taRightJustify;
FontDesc := '#Label1';
Hint := '';
Text := 'Blue';
end;
edR := TfpgSpinEdit.Create(self);
with edR do
begin
Name := 'edR';
SetPosition(290, 280, 44, 26);
TabOrder := 13;
MinValue := 0;
MaxValue := 255;
Value := 255;
FontDesc := '#Edit1';
OnChange := @RGBChanged;
OnExit := @RGBChanged;
end;
edG := TfpgSpinEdit.Create(self);
with edG do
begin
Name := 'edG';
SetPosition(290, 308, 44, 26);
TabOrder := 14;
MinValue := 0;
MaxValue := 255;
Value := 255;
FontDesc := '#Edit1';
OnChange := @RGBChanged;
OnExit := @RGBChanged;
end;
edB := TfpgSpinEdit.Create(self);
with edB do
begin
Name := 'edB';
SetPosition(290, 336, 44, 26);
TabOrder := 15;
MinValue := 0;
MaxValue := 255;
Value := 255;
FontDesc := '#Edit1';
OnChange := @RGBChanged;
OnExit := @RGBChanged;
end;
Label10 := TfpgLabel.Create(Self);
with Label10 do
begin
Name := 'Label10';
Setposition(352,100,180,16);
FontDesc := '#Label1';
Hint := '';
Text := 'Predefined Color Palettes';
end;
cbColors := TfpgComboBox.Create(Self);
with cbColors do
begin
Name := 'cbColors';
SetPosition(352,120,180,22);
FontDesc := '#List';
Hint := '';
end;
lbColors := TfpgColorListBox.Create(Self);
with lbColors do
begin
Name := 'lbColors';
SetPosition(352,150,180,160);
FontDesc := '#List';
Hint := '';
ScrollbarWidth := 12;
ScrollbarPage := VisibleItems;
OnChange := @lbColorsChange;
end;
lblHex := TfpgLabel.Create(self);
with lblHex do
begin
Name := 'lblHex';
SetPosition(375, 340, 120, 16);
FontDesc := '#Label2';
Hint := '';
Text := 'Hex = ';
end;
eHex := TfpgEdit.Create(self);
with eHex do
begin
Name := 'E_Hexa';
SetPosition(420, 336, 65, 26);
FontDesc := '#Label2';
Hint := '';
Text := '';
MaxLength:= 7;
OnKeyChar:= @eHexKeyChar;
OnKeyPress:= @eHexKeyPress;
end;
Label7 := TfpgLabel.Create(self);
with Label7 do
begin
Name := 'Label7';
SetPosition(108, 3, 80, 16);
FontDesc := '#Label2';
Hint := '';
Text := 'ColorWheel';
end;
Label8 := TfpgLabel.Create(self);
with Label8 do
begin
Name := 'Label8';
SetPosition(290, 3, 64, 16);
FontDesc := '#Label2';
Hint := '';
Text := 'ValueBar';
end;
Bevel2 := TfpgBevel.Create(self);
with Bevel2 do
begin
Name := 'Bevel2';
SetPosition(388, 8, 2, 80);
Hint := '';
Style := bsLowered;
end;
Label9 := TfpgLabel.Create(self);
with Label9 do
begin
Name := 'Label9';
SetPosition(400, 3, 128, 16);
Alignment := taCenter;
FontDesc := '#Label2';
Hint := '';
Text := 'Custom Options';
end;
chkCrossHair := TfpgCheckBox.Create(self);
with chkCrossHair do
begin
Name := 'chkCrossHair';
SetPosition(396, 32, 128, 20);
FontDesc := '#Label1';
Hint := '';
TabOrder := 20;
Text := 'Large CrossHair';
OnChange := @chkCrossHairChange;
end;
chkBGColor := TfpgCheckBox.Create(self);
with chkBGColor do
begin
Name := 'chkBGColor';
SetPosition(396, 56, 132, 20);
FontDesc := '#Label1';
Hint := '';
TabOrder := 21;
Text := 'New BG Color';
OnChange := @chkBGColorChange;
end;
btnPicker := TPickerButton.Create(self);
with btnPicker do
begin
Name := 'btnPicker';
SetPosition(116, 372, 80, 23);
Text := 'Picker';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 24;
OnColorPicked := @btnColorPicked;
end;
chkContinuous := TfpgCheckBox.Create(self);
with chkContinuous do
begin
Name := 'chkContinous';
SetPosition(205, 375, 90, 19);
FontDesc := '#Label1';
Hint := '';
TabOrder := 25;
Text := 'Continous';
OnChange := @chkContinuousChanged;
end;
{@VFD_BODY_END: MainForm}
PopulatePaletteColorCombo;
// link the two components
ColorWheel1.ValueBar := ValueBar1;
// ColorWheel1.BackgroundColor := clFuchsia;
// ValueBar1.BackgroundColor := clFuchsia;
// ColorWheel1.CursorSize := 400;
UpdateHSVComponents;
if not FViaRGB then
UpdateRGBComponents;
end;
end.
|