-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuColorPickerControls.pas
More file actions
775 lines (715 loc) · 21.9 KB
/
Copy pathuColorPickerControls.pas
File metadata and controls
775 lines (715 loc) · 21.9 KB
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
{ uColorPickerControls.pas
Custom Color Picker Controls v1.9
------------------------------------------------------------------------------
Contains the custom controls used by the Color Picker:
• TBufferedControl - Flicker-reduced base control
• TSVSquare - Interactive color selection surface
• THueBar - Hue / Saturation / Lightness control bar
• TColorSwatch - Color preview control
Includes gradient caching, smooth selector animation and mouse interaction.
Developer : Shohanur Rahman
Publisher : SR Studio 24 - BD
Copyright : © 2026 SR Studio 24 - BD
Website : https://srstudio24.blogspot.com/
IMPORTANT:
Mode mapping, gradient generation, cache, animation and mouse handling
are core logic. Do not modify without checking uCustomColorDlg.pas.
Internal project code.
Unauthorized modification or redistribution is not permitted.
}
unit uColorPickerControls;
interface
uses
Winapi.Windows, Winapi.Messages, Winapi.MMSystem, System.SysUtils, System.Classes, Vcl.ExtCtrls,
System.Types, System.Math, Vcl.Graphics, Vcl.Controls, Vcl.Forms;
type
TBufferedControl = class(TCustomControl)
private
FOnPaint: TNotifyEvent;
protected
procedure Paint; override;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
public
constructor Create(AOwner: TComponent); override;
property Canvas;
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
end;
TSVSquareMode = (ssmSatBri, ssmHueBri, ssmHueSat);
TSVSquare = class(TBufferedControl)
private
FHue, FSat, FBri: Integer;
FMode: TSVSquareMode;
FCache: TBitmap;
FDisplayBmp: TBitmap;
FCacheKey: string;
FOnChange: TNotifyEvent;
FDragging: Boolean;
FDisplaySat, FDisplayBri, FTargetSat, FTargetBri: Double;
FAnimationSpeed: Double;
FUpdateTimer: TTimer;
FLastAnimationTick: UInt64;
procedure SetHue(Value: Integer);
procedure SetSat(Value: Integer);
procedure SetBri(Value: Integer);
procedure SetMode(Value: TSVSquareMode);
procedure RebuildCache;
procedure RestretchDisplay;
procedure UpdateTimerTick(Sender: TObject);
procedure UpdateAnimation;
procedure DoChange;
function GetCacheKey: string;
protected
procedure Paint; override;
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure CMCancelMode(var Message: TMessage); message CM_CANCELMODE;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Hue: Integer read FHue write SetHue;
property Sat: Integer read FSat write SetSat;
property Bri: Integer read FBri write SetBri;
property Mode: TSVSquareMode read FMode write SetMode;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
procedure ForceRebuild;
end;
THueBarMode = (hbmHue, hbmSat, hbmLight);
THueBar = class(TBufferedControl)
private
FHue, FSat, FBri: Integer;
FMode: THueBarMode;
FCache: TBitmap;
FOnChange: TNotifyEvent;
FDragging: Boolean;
procedure SetHue(Value: Integer);
procedure SetSat(Value: Integer);
procedure SetBri(Value: Integer);
procedure SetMode(Value: THueBarMode);
procedure RebuildCache;
procedure DoChange;
protected
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure CMCancelMode(var Message: TMessage); message CM_CANCELMODE;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Hue: Integer read FHue write SetHue;
property Sat: Integer read FSat write SetSat;
property Bri: Integer read FBri write SetBri;
property Mode: THueBarMode read FMode write SetMode;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property IsDragging: Boolean read FDragging;
end;
TColorSwatch = class(TBufferedControl)
private
FColor: TColor;
FShowBorder: Boolean;
procedure SetColor(Value: TColor);
procedure SetShowBorder(Value: Boolean);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
property Color: TColor read FColor write SetColor;
property ShowBorder: Boolean read FShowBorder write SetShowBorder;
property OnClick;
end;
implementation
uses
uColorUtils;
// Delphi XE2 .. 10.3 compatibility (GetTickCount64 added later in Winapi.Windows)
function GetTickCount64: UInt64; stdcall; external kernel32 name 'GetTickCount64';
{ TBufferedControl }
constructor TBufferedControl.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csOpaque, csCaptureMouse, csClickEvents, csReplicatable];
DoubleBuffered := True;
ParentBackground := False;
TabStop := False;
end;
procedure TBufferedControl.Paint;
begin
if Assigned(FOnPaint) then FOnPaint(Self);
end;
procedure TBufferedControl.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
Message.Result := 1;
end;
procedure TBufferedControl.CMFontChanged(var Message: TMessage);
begin
inherited; Invalidate;
end;
procedure TBufferedControl.CMColorChanged(var Message: TMessage);
begin
inherited; Invalidate;
end;
{ TSVSquare }
constructor TSVSquare.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCache := TBitmap.Create;
FCache.PixelFormat := pf24bit;
FCache.SetSize(512, 512);
FDisplayBmp := TBitmap.Create;
FDisplayBmp.PixelFormat := pf24bit;
FDisplayBmp.SetSize(256, 256);
FCacheKey := '';
FHue := 0; FSat := 100; FBri := 100;
FMode := ssmSatBri;
FDragging := False;
FDisplaySat := 100; FDisplayBri := 100;
FTargetSat := 100; FTargetBri := 100;
FAnimationSpeed := 22.0;
FLastAnimationTick := 0;
Width := 256; Height := 256;
FUpdateTimer := TTimer.Create(Self);
FUpdateTimer.Interval := 4;
FUpdateTimer.Enabled := False;
FUpdateTimer.OnTimer := UpdateTimerTick;
end;
destructor TSVSquare.Destroy;
begin
if FDragging then
begin
FDragging := False;
timeEndPeriod(1);
if GetCapture = Handle then ReleaseCapture;
end;
FUpdateTimer.Free;
FDisplayBmp.Free;
FCache.Free;
inherited;
end;
procedure TSVSquare.CMCancelMode(var Message: TMessage);
begin
inherited;
if FDragging then
begin
FDragging := False;
timeEndPeriod(1);
FUpdateTimer.Enabled := False;
if GetCapture = Handle then ReleaseCapture;
end;
end;
function TSVSquare.GetCacheKey: string;
begin
case FMode of
ssmSatBri: Result := Format('SV_%d', [FHue]);
ssmHueBri: Result := Format('HV_%d', [FHue]); // fixed Bri এখন FHue-তে
ssmHueSat: Result := Format('HS_%d', [FBri]);
end;
end;
procedure TSVSquare.SetMode(Value: TSVSquareMode);
begin
if FMode <> Value then
begin
FMode := Value;
FCacheKey := '';
RebuildCache;
Invalidate;
end;
end;
procedure TSVSquare.SetHue(Value: Integer);
begin
Value := ((Value mod 360) + 360) mod 360;
if FHue <> Value then
begin
FHue := Value;
FCacheKey := '';
RebuildCache;
Invalidate;
end;
end;
procedure TSVSquare.SetSat(Value: Integer);
begin
Value := EnsureRange(Value, 0, 100);
if FSat <> Value then
begin
FSat := Value;
FDisplaySat := Value;
FTargetSat := Value;
Invalidate;
end;
end;
procedure TSVSquare.SetBri(Value: Integer);
begin
Value := EnsureRange(Value, 0, 100);
if FBri <> Value then
begin
FBri := Value;
FDisplayBri := Value;
FTargetBri := Value;
if FMode = ssmHueSat then
begin
FCacheKey := '';
RebuildCache;
end;
Invalidate;
end;
end;
procedure TSVSquare.RebuildCache;
var
x, y: Integer;
R, G, B: Byte;
P: PByteArray;
Hf, Sf, Vf, C, Xv, M, Rf, Gf, Bf: Double;
Hi: Integer;
Key: string;
begin
Key := GetCacheKey;
if Key = FCacheKey then Exit;
FCacheKey := Key;
for y := 0 to 511 do
begin
P := FCache.ScanLine[y];
for x := 0 to 511 do
begin
case FMode of
ssmSatBri:
begin
Sf := x / 511.0;
Vf := (511 - y) / 511.0;
Hf := FHue / 60.0;
C := Vf * Sf;
Xv := C * (1.0 - Abs(Frac(Hf * 0.5) * 2.0 - 1.0));
M := Vf - C;
Hi := Trunc(Hf) mod 6;
case Hi of
0: begin Rf := C; Gf := Xv; Bf := 0; end;
1: begin Rf := Xv; Gf := C; Bf := 0; end;
2: begin Rf := 0; Gf := C; Bf := Xv; end;
3: begin Rf := 0; Gf := Xv; Bf := C; end;
4: begin Rf := Xv; Gf := 0; Bf := C; end;
else begin Rf := C; Gf := 0; Bf := Xv; end;
end;
R := EnsureRange(Round((Rf + M) * 255.0), 0, 255);
G := EnsureRange(Round((Gf + M) * 255.0), 0, 255);
B := EnsureRange(Round((Bf + M) * 255.0), 0, 255);
end;
ssmHueBri:
begin
// S mode নতুন অর্থ: X = Hue, Y = Sat, fixed = Bri (FHue)
Hf := (x / 511.0) * 6.0;
Sf := (511 - y) / 511.0; // Y = Saturation
Vf := FHue / 100.0; // fixed Brightness
C := Vf * Sf;
Xv := C * (1.0 - Abs(Frac(Hf * 0.5) * 2.0 - 1.0));
M := Vf - C;
Hi := Trunc(Hf) mod 6;
case Hi of
0: begin Rf := C; Gf := Xv; Bf := 0; end;
1: begin Rf := Xv; Gf := C; Bf := 0; end;
2: begin Rf := 0; Gf := C; Bf := Xv; end;
3: begin Rf := 0; Gf := Xv; Bf := C; end;
4: begin Rf := Xv; Gf := 0; Bf := C; end;
else begin Rf := C; Gf := 0; Bf := Xv; end;
end;
R := EnsureRange(Round((Rf + M) * 255.0), 0, 255);
G := EnsureRange(Round((Gf + M) * 255.0), 0, 255);
B := EnsureRange(Round((Bf + M) * 255.0), 0, 255);
end;
ssmHueSat:
begin
HSLtoRGBValues(
Round((x / 511.0) * 359.0),
Round(((511 - y) / 511.0) * 100.0),
FHue, // fixed Lightness
R, G, B);
end;
end;
P[x * 3 + 0] := B;
P[x * 3 + 1] := G;
P[x * 3 + 2] := R;
end;
end;
// Expensive HALFTONE resample now happens ONCE here (only when the
// gradient content actually changed), instead of on every Paint().
RestretchDisplay;
end;
procedure TSVSquare.RestretchDisplay;
var
OldMode: Integer;
begin
if (FDisplayBmp = nil) or (FDisplayBmp.Width <= 0) or (FDisplayBmp.Height <= 0) then Exit;
OldMode := SetStretchBltMode(FDisplayBmp.Canvas.Handle, HALFTONE);
SetBrushOrgEx(FDisplayBmp.Canvas.Handle, 0, 0, nil);
FDisplayBmp.Canvas.StretchDraw(Rect(0, 0, FDisplayBmp.Width, FDisplayBmp.Height), FCache);
SetStretchBltMode(FDisplayBmp.Canvas.Handle, OldMode);
end;
procedure TSVSquare.Resize;
begin
inherited;
if (FDisplayBmp <> nil) and (Width > 0) and (Height > 0) then
begin
FDisplayBmp.SetSize(Width, Height);
RestretchDisplay;
end;
end;
procedure TSVSquare.Paint;
var
cx, cy: Integer;
begin
RebuildCache;
// Safety net: keep display bitmap in sync even if a Resize message was
// ever missed (e.g. programmatic size change) - identical output,
// negligible cost (two integer compares) on the common path.
if (FDisplayBmp.Width <> Width) or (FDisplayBmp.Height <> Height) then
begin
FDisplayBmp.SetSize(Width, Height);
RestretchDisplay;
end;
Canvas.Draw(0, 0, FDisplayBmp);
cx := Round(FDisplaySat * 255 / 100);
cy := Round((100 - FDisplayBri) * 255 / 100);
Canvas.Brush.Style := bsClear;
Canvas.Pen.Color := clWhite;
Canvas.Pen.Width := 2;
Canvas.Ellipse(cx - 6, cy - 6, cx + 7, cy + 7);
Canvas.Pen.Color := clBlack;
Canvas.Pen.Width := 1;
Canvas.Ellipse(cx - 5, cy - 5, cx + 6, cy + 6);
end;
procedure TSVSquare.UpdateTimerTick(Sender: TObject);
begin
UpdateAnimation;
end;
procedure TSVSquare.UpdateAnimation;
var
NowTick: UInt64;
DeltaMS, Alpha: Double;
OldSat, OldBri: Double;
begin
if not FDragging then
begin
FUpdateTimer.Enabled := False;
Exit;
end;
NowTick := GetTickCount64;
if FLastAnimationTick = 0 then FLastAnimationTick := NowTick;
DeltaMS := NowTick - FLastAnimationTick;
FLastAnimationTick := NowTick;
if DeltaMS > 50 then DeltaMS := 50;
OldSat := FDisplaySat; OldBri := FDisplayBri;
Alpha := 1.0 - Exp(-FAnimationSpeed * DeltaMS / 1000.0);
FDisplaySat := FDisplaySat + (FTargetSat - FDisplaySat) * Alpha;
FDisplayBri := FDisplayBri + (FTargetBri - FDisplayBri) * Alpha;
if Abs(FDisplaySat - FTargetSat) < 0.02 then FDisplaySat := FTargetSat;
if Abs(FDisplayBri - FTargetBri) < 0.02 then FDisplayBri := FTargetBri;
if (Abs(FDisplaySat - OldSat) > 0.001) or (Abs(FDisplayBri - OldBri) > 0.001) then
Invalidate;
if (Abs(FDisplaySat - FTargetSat) < 0.01) and (Abs(FDisplayBri - FTargetBri) < 0.01) then
begin
FDisplaySat := FTargetSat;
FDisplayBri := FTargetBri;
end;
end;
procedure TSVSquare.DoChange;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure TSVSquare.ForceRebuild;
begin
FCacheKey := '';
RebuildCache;
Invalidate;
end;
procedure TSVSquare.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if Button <> mbLeft then Exit;
FDragging := True;
timeBeginPeriod(1); // raise Windows timer resolution for smooth per-frame animation while dragging
FTargetSat := FSat; FTargetBri := FBri;
FLastAnimationTick := GetTickCount64;
SetCapture(Handle);
FUpdateTimer.Enabled := True;
MouseMove(Shift, X, Y);
end;
procedure TSVSquare.MouseMove(Shift: TShiftState; X, Y: Integer);
var
NewS, NewB: Integer;
begin
inherited;
if not FDragging then Exit;
X := EnsureRange(X, 0, Width - 1);
Y := EnsureRange(Y, 0, Height - 1);
if Width > 1 then NewS := Round(X * 100 / (Width - 1)) else NewS := 0;
if Height > 1 then NewB := 100 - Round(Y * 100 / (Height - 1)) else NewB := 100;
NewS := EnsureRange(NewS, 0, 100);
NewB := EnsureRange(NewB, 0, 100);
if (NewS <> FSat) or (NewB <> FBri) then
begin
FSat := NewS; FBri := NewB;
FTargetSat := NewS; FTargetBri := NewB;
DoChange;
end;
if not FUpdateTimer.Enabled then
begin
FLastAnimationTick := GetTickCount64;
FUpdateTimer.Enabled := True;
end;
end;
procedure TSVSquare.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if not FDragging then Exit;
FDragging := False;
timeEndPeriod(1);
FDisplaySat := FTargetSat; FDisplayBri := FTargetBri;
Invalidate;
FUpdateTimer.Enabled := False;
if GetCapture = Handle then ReleaseCapture;
end;
{ THueBar }
constructor THueBar.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCache := TBitmap.Create;
FCache.PixelFormat := pf24bit;
FCache.SetSize(22, 256);
FHue := 0; FSat := 100; FBri := 100;
FMode := hbmHue;
FDragging := False;
Width := 22; Height := 256;
RebuildCache;
end;
destructor THueBar.Destroy;
begin
if FDragging then
begin
FDragging := False;
if GetCapture = Handle then ReleaseCapture;
end;
FCache.Free;
inherited;
end;
procedure THueBar.CMCancelMode(var Message: TMessage);
begin
inherited;
if FDragging then
begin
FDragging := False;
if GetCapture = Handle then ReleaseCapture;
end;
end;
procedure THueBar.SetHue(Value: Integer);
begin
Value := ((Value mod 360) + 360) mod 360;
if FHue <> Value then
begin
FHue := Value;
// Hue feeds the fixed-color term in BOTH hbmSat and hbmLight gradients,
// so both modes must rebuild - unchanged from original behavior.
if FMode <> hbmHue then RebuildCache;
Invalidate;
end;
end;
procedure THueBar.SetSat(Value: Integer);
begin
Value := EnsureRange(Value, 0, 100);
if FSat <> Value then
begin
FSat := Value;
// hbmSat-এ এখন Sat fixed → rebuild লাগে
if (FMode = hbmLight) or (FMode = hbmSat) then RebuildCache;
Invalidate;
end;
end;
procedure THueBar.SetBri(Value: Integer);
begin
Value := EnsureRange(Value, 0, 100);
if FBri <> Value then
begin
FBri := Value;
// hbmSat-এ Bri এখন axis → gradient rebuild লাগে না
if FMode = hbmLight then RebuildCache;
Invalidate;
end;
end;
procedure THueBar.SetMode(Value: THueBarMode);
begin
if FMode <> Value then
begin
FMode := Value;
RebuildCache;
Invalidate;
end;
end;
procedure THueBar.RebuildCache;
var
y, x: Integer;
Hval, Lval, BriVal: Integer; // <-- BriVal এখানে যোগ করা হয়েছে (inline var সরানো হয়েছে)
R, G, B: Byte;
P: PByteArray;
begin
for y := 0 to 255 do
begin
case FMode of
hbmHue:
begin
Hval := 359 - (y * 359 div 255);
HSBtoRGBValues(Hval, 100, 100, R, G, B);
end;
hbmSat:
begin
// S mode: Bri ভ্যারি করে (fixed Hue + fixed Sat)। টপ = পিওর কালার
BriVal := 100 - (y * 100 div 255);
HSBtoRGBValues(FHue, FSat, BriVal, R, G, B);
end;
hbmLight:
begin
Lval := 100 - (y * 100 div 255);
HSLtoRGBValues(FHue, FSat, Lval, R, G, B);
end;
end;
P := FCache.ScanLine[y];
for x := 0 to 21 do
begin
P[x * 3 + 0] := B;
P[x * 3 + 1] := G;
P[x * 3 + 2] := R;
end;
end;
end;
procedure THueBar.Paint;
var
cy: Integer;
CurVal: Integer;
begin
Canvas.Draw(0, 0, FCache);
// Marker is drawn straight from the live value - no lerp/animation
// buffer - so it always sits exactly where the mouse (or an external
// Hue/Sat/Bri assignment) put it, with zero lag.
case FMode of
hbmHue: CurVal := FHue;
hbmSat: CurVal := FBri; // <-- আগে FSat ছিল, এখন FBri
hbmLight: CurVal := FBri;
else
CurVal := 0;
end;
case FMode of
hbmHue: cy := Round((359 - CurVal) * 255 / 359);
hbmSat: cy := Round((100 - CurVal) * 255 / 100);
hbmLight: cy := Round((100 - CurVal) * 255 / 100);
else
cy := 0;
end;
Canvas.Brush.Style := bsClear;
Canvas.Pen.Color := clWhite;
Canvas.Pen.Width := 2;
Canvas.Rectangle(1, cy - 3, 21, cy + 4);
Canvas.Pen.Color := clBlack;
Canvas.Pen.Width := 1;
Canvas.Rectangle(2, cy - 2, 20, cy + 3);
end;
procedure THueBar.DoChange;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure THueBar.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if Button <> mbLeft then Exit;
FDragging := True;
SetCapture(Handle);
MouseMove(Shift, X, Y);
end;
procedure THueBar.MouseMove(Shift: TShiftState; X, Y: Integer);
var
NewVal: Integer;
begin
inherited;
if not FDragging then Exit;
Y := EnsureRange(Y, 0, Height - 1);
case FMode of
hbmHue:
begin
if Height > 1 then NewVal := 359 - Round(Y * 359 / (Height - 1)) else NewVal := 359;
NewVal := EnsureRange(NewVal, 0, 359);
if NewVal <> FHue then
begin
FHue := NewVal;
DoChange;
end;
end;
hbmSat:
begin
if Height > 1 then NewVal := 100 - Round(Y * 100 / (Height - 1)) else NewVal := 100;
NewVal := EnsureRange(NewVal, 0, 100);
if NewVal <> FBri then // <-- আগে FSat ছিল
begin
FBri := NewVal;
DoChange;
end;
end;
hbmLight:
begin
if Height > 1 then NewVal := 100 - Round(Y * 100 / (Height - 1)) else NewVal := 100;
NewVal := EnsureRange(NewVal, 0, 100);
if NewVal <> FBri then
begin
FBri := NewVal;
DoChange;
end;
end;
end;
// Force a SYNCHRONOUS repaint right now (not just an async Invalidate
// request). Without this, Windows can keep delivering a burst of
// WM_MOUSEMOVE messages before it ever gets around to the lower-priority
// WM_PAINT - the value updates internally every message, but the screen
// only catches up once at the end of the burst, which is exactly what
// reads as "jumping" instead of a smooth 1:1 drag. Repaint forces the
// paint to happen immediately, every single mouse move.
Repaint;
end;
procedure THueBar.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if not FDragging then Exit;
FDragging := False;
if GetCapture = Handle then ReleaseCapture;
end;
{ TColorSwatch }
constructor TColorSwatch.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FColor := clBlack;
FShowBorder := True;
Width := 60; Height := 60;
end;
procedure TColorSwatch.SetColor(Value: TColor);
begin
if FColor <> Value then
begin
FColor := Value;
Invalidate;
end;
end;
procedure TColorSwatch.SetShowBorder(Value: Boolean);
begin
if FShowBorder <> Value then
begin
FShowBorder := Value;
Invalidate;
end;
end;
procedure TColorSwatch.Paint;
begin
Canvas.Brush.Color := FColor;
Canvas.FillRect(ClientRect);
if FShowBorder then
begin
Canvas.Brush.Style := bsClear;
Canvas.Pen.Color := clWindowFrame;
Canvas.Rectangle(0, 0, Width, Height);
end;
end;
end.