-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFluidColorBoxUnit.pas
More file actions
399 lines (342 loc) · 11.5 KB
/
FluidColorBoxUnit.pas
File metadata and controls
399 lines (342 loc) · 11.5 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
unit FluidColorBoxUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Winapi.GDIPOBJ, Winapi.GDIPAPI, System.UITypes,
System.Math, Vcl.GraphUtil;
type
TFluidColorBox = class(TCustomControl)
private
FHue: Double; // 0.0 - 1.0
FSat: Double; // 0.0 - 1.0
FVal: Double; // 0.0 - 1.0
FBorderColor: TColor;
FBorderRadius: Integer;
FOnColorChange: TNotifyEvent;
FIsDraggingMain: Boolean;
FIsDraggingHue: Boolean;
procedure SetBorderColor(const Value: TColor);
procedure SetBorderRadius(const Value: Integer);
function GetSelectedColor: TColor;
procedure SetSelectedColor(const Value: TColor);
// HSV Helpers
procedure ColorToHSV(C: TColor; var H, S, V: Double);
function HSVToColor(H, S, V: Double): TColor;
// Internal Geometry Helpers
function GetMainBoxRect: TGPRectF;
function GetHueSliderRect: TGPRectF;
function ColorToGPColor(const Value: TColor): TGPColor;
procedure UpdateFromMouse(X, Y: Integer);
// Manual RoundRect implementation for TGPGraphicsPath compatibility
procedure AddRoundRectToPath(Path: TGPGraphicsPath; Rect: TGPRectF; Radius: Single);
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 Resize; override;
public
constructor Create(AOwner: TComponent); override;
property SelectedColor: TColor read GetSelectedColor write SetSelectedColor;
function GetColorHex: string;
function GetColorRGB: string;
procedure SetHSV(AHue, ASat, AVal: Double);
published
property BorderColor: TColor read FBorderColor write SetBorderColor default clGray;
property BorderRadius: Integer read FBorderRadius write SetBorderRadius default 8;
property OnColorChange: TNotifyEvent read FOnColorChange write FOnColorChange;
property Align;
property Anchors;
property Visible;
property Enabled;
property Color;
property ParentColor;
property ParentShowHint;
property ShowHint;
property TabStop;
property TabOrder;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('FluidVCL', [TFluidColorBox]);
end;
{ TFluidColorBox }
constructor TFluidColorBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csOpaque, csDoubleClicks];
Width := 320;
Height := 280;
FHue := 0;
FSat := 1.0;
FVal := 1.0;
FBorderColor := $00444444;
FBorderRadius := 8;
DoubleBuffered := True;
end;
procedure TFluidColorBox.ColorToHSV(C: TColor; var H, S, V: Double);
var
R, G, B: Byte;
MaxV, MinV, Diff: Byte;
RGBVal: LongInt;
begin
RGBVal := ColorToRGB(C);
R := GetRValue(RGBVal);
G := GetGValue(RGBVal);
B := GetBValue(RGBVal);
MaxV := Max(R, Max(G, B));
MinV := Min(R, Min(G, B));
Diff := MaxV - MinV;
V := MaxV / 255.0;
if MaxV = 0 then S := 0 else S := Diff / MaxV;
if Diff = 0 then H := 0
else begin
if MaxV = R then H := (G - B) / Diff
else if MaxV = G then H := 2 + (B - R) / Diff
else H := 4 + (R - G) / Diff;
H := H / 6.0;
if H < 0 then H := H + 1.0;
end;
end;
function TFluidColorBox.HSVToColor(H, S, V: Double): TColor;
var
Hi: Integer;
f, p, q, t: Double;
r, g, b: Byte;
begin
if S = 0 then begin
r := Round(V * 255); g := r; b := r;
end else begin
Hi := Floor(H * 6);
f := (H * 6) - Hi;
p := V * (1 - S);
q := V * (1 - f * S);
t := V * (1 - (1 - f) * S);
case Hi of
0, 6: begin r := Round(V * 255); g := Round(t * 255); b := Round(p * 255); end;
1: begin r := Round(q * 255); g := Round(V * 255); b := Round(p * 255); end;
2: begin r := Round(p * 255); g := Round(V * 255); b := Round(t * 255); end;
3: begin r := Round(p * 255); g := Round(q * 255); b := Round(V * 255); end;
4: begin r := Round(t * 255); g := Round(p * 255); b := Round(V * 255); end;
5: begin r := Round(V * 255); g := Round(p * 255); b := Round(q * 255); end;
else begin r := 0; g := 0; b := 0; end;
end;
end;
Result := RGB(r, g, b);
end;
procedure TFluidColorBox.AddRoundRectToPath(Path: TGPGraphicsPath; Rect: TGPRectF; Radius: Single);
var D: Single;
begin
D := Radius * 2;
if D > Rect.Width then D := Rect.Width;
if D > Rect.Height then D := Rect.Height;
Path.Reset;
Path.AddArc(Rect.X, Rect.Y, D, D, 180, 90);
Path.AddLine(Rect.X + Radius, Rect.Y, Rect.X + Rect.Width - Radius, Rect.Y);
Path.AddArc(Rect.X + Rect.Width - D, Rect.Y, D, D, 270, 90);
Path.AddLine(Rect.X + Rect.Width, Rect.Y + Radius, Rect.X + Rect.Width, Rect.Y + Rect.Height - Radius);
Path.AddArc(Rect.X + Rect.Width - D, Rect.Y + Rect.Height - D, D, D, 0, 90);
Path.AddLine(Rect.X + Rect.Width - Radius, Rect.Y + Rect.Height, Rect.X + Radius, Rect.Y + Rect.Height);
Path.AddArc(Rect.X, Rect.Y + Rect.Height - D, D, D, 90, 90);
Path.AddLine(Rect.X, Rect.Y + Rect.Height - Radius, Rect.X, Rect.Y + Radius);
Path.CloseFigure;
end;
function TFluidColorBox.ColorToGPColor(const Value: TColor): TGPColor;
var RGBValue: LongInt;
begin
RGBValue := ColorToRGB(Value);
Result := MakeColor(255, GetRValue(RGBValue), GetGValue(RGBValue), GetBValue(RGBValue));
end;
function TFluidColorBox.GetColorHex: string;
var C: TColor;
begin
C := GetSelectedColor;
Result := Format('#%.2x%.2x%.2x', [GetRValue(C), GetGValue(C), GetBValue(C)]);
end;
function TFluidColorBox.GetColorRGB: string;
var C: TColor;
begin
C := GetSelectedColor;
Result := Format('%d, %d, %d', [GetRValue(C), GetGValue(C), GetBValue(C)]);
end;
function TFluidColorBox.GetSelectedColor: TColor;
begin
Result := HSVToColor(FHue, FSat, FVal);
end;
procedure TFluidColorBox.SetSelectedColor(const Value: TColor);
begin
ColorToHSV(Value, FHue, FSat, FVal);
Invalidate;
end;
procedure TFluidColorBox.SetHSV(AHue, ASat, AVal: Double);
begin
FHue := EnsureRange(AHue, 0.0, 1.0);
FSat := EnsureRange(ASat, 0.0, 1.0);
FVal := EnsureRange(AVal, 0.0, 1.0);
Invalidate;
end;
function TFluidColorBox.GetMainBoxRect: TGPRectF;
begin
Result.X := 10;
Result.Y := 10;
Result.Width := Width - 50;
Result.Height := Height - 20;
end;
function TFluidColorBox.GetHueSliderRect: TGPRectF;
begin
Result.X := Width - 30;
Result.Y := 10;
Result.Width := 20;
Result.Height := Height - 20;
end;
procedure TFluidColorBox.UpdateFromMouse(X, Y: Integer);
var
MainR, HueR: TGPRectF;
begin
MainR := GetMainBoxRect;
HueR := GetHueSliderRect;
if FIsDraggingMain then
begin
FSat := EnsureRange((X - MainR.X) / MainR.Width, 0.0, 1.0);
FVal := 1.0 - EnsureRange((Y - MainR.Y) / MainR.Height, 0.0, 1.0);
end
else if FIsDraggingHue then
begin
FHue := EnsureRange((Y - HueR.Y) / HueR.Height, 0.0, 1.0);
end;
Invalidate;
if Assigned(FOnColorChange) then
FOnColorChange(Self);
end;
procedure TFluidColorBox.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
MainR, HueR: TGPRectF;
begin
if Button <> mbLeft then Exit;
MainR := GetMainBoxRect;
HueR := GetHueSliderRect;
if (X >= MainR.X) and (X <= MainR.X + MainR.Width) and (Y >= MainR.Y) and (Y <= MainR.Y + MainR.Height) then
FIsDraggingMain := True
else if (X >= HueR.X) and (X <= HueR.X + HueR.Width) and (Y >= HueR.Y) and (Y <= HueR.Y + HueR.Height) then
FIsDraggingHue := True;
if FIsDraggingMain or FIsDraggingHue then
begin
UpdateFromMouse(X, Y);
SetCapture(Handle);
end;
end;
procedure TFluidColorBox.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
if FIsDraggingMain or FIsDraggingHue then
UpdateFromMouse(X, Y);
end;
procedure TFluidColorBox.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
FIsDraggingMain := False;
FIsDraggingHue := False;
ReleaseCapture;
end;
procedure TFluidColorBox.Paint;
var
Graphics: TGPGraphics;
MainR, HueR: TGPRectF;
Path: TGPGraphicsPath;
LBrush: TGPLinearGradientBrush;
HueColors: array[0..6] of TGPColor;
Positions: array[0..6] of Single;
I: Integer;
IndY, IndX: Single;
BorderPen: TGPPen;
HueBaseColor: TColor;
P1, P2: TGPPointF;
begin
Graphics := TGPGraphics.Create(Canvas.Handle);
try
Graphics.SetSmoothingMode(SmoothingModeAntiAlias);
Graphics.Clear(ColorToGPColor(Color));
MainR := GetMainBoxRect;
HueR := GetHueSliderRect;
BorderPen := TGPPen.Create(ColorToGPColor(FBorderColor), 1.2);
// --- 1. Main Saturation/Value Box ---
Path := TGPGraphicsPath.Create;
try
AddRoundRectToPath(Path, MainR, FBorderRadius);
Graphics.SetClip(Path);
// Background color is the pure Hue at 100% Sat and 100% Val
HueBaseColor := HSVToColor(FHue, 1.0, 1.0);
Graphics.Clear(ColorToGPColor(HueBaseColor));
// White Horizontal Gradient (Saturation)
P1 := MakePoint(MainR.X, MainR.Y);
P2 := MakePoint(MainR.X + MainR.Width, MainR.Y);
LBrush := TGPLinearGradientBrush.Create(P1, P2, MakeColor(255, 255, 255, 255), MakeColor(0, 255, 255, 255));
Graphics.FillRectangle(LBrush, MainR);
LBrush.Free;
// Black Vertical Gradient (Value/Brightness)
P2 := MakePoint(MainR.X, MainR.Y + MainR.Height);
LBrush := TGPLinearGradientBrush.Create(P1, P2, MakeColor(0, 0, 0, 0), MakeColor(255, 0, 0, 0));
Graphics.FillRectangle(LBrush, MainR);
LBrush.Free;
Graphics.ResetClip;
Graphics.DrawPath(BorderPen, Path);
finally
Path.Free;
end;
// --- 2. Hue Slider ---
Path := TGPGraphicsPath.Create;
try
AddRoundRectToPath(Path, HueR, FBorderRadius);
Graphics.SetClip(Path);
P1 := MakePoint(HueR.X, HueR.Y);
P2 := MakePoint(HueR.X, HueR.Y + HueR.Height);
LBrush := TGPLinearGradientBrush.Create(P1, P2, MakeColor(255, 255, 0, 0), MakeColor(255, 255, 0, 0));
HueColors[0] := MakeColor(255, 255, 0, 0);
HueColors[1] := MakeColor(255, 255, 255, 0);
HueColors[2] := MakeColor(255, 0, 255, 0);
HueColors[3] := MakeColor(255, 0, 255, 255);
HueColors[4] := MakeColor(255, 0, 0, 255);
HueColors[5] := MakeColor(255, 255, 0, 255);
HueColors[6] := MakeColor(255, 255, 0, 0);
for I := 0 to 6 do Positions[I] := I / 6.0;
LBrush.SetInterpolationColors(@HueColors[0], @Positions[0], 7);
Graphics.FillRectangle(LBrush, HueR);
LBrush.Free;
Graphics.ResetClip;
Graphics.DrawPath(BorderPen, Path);
finally
Path.Free;
end;
// --- 3. Indicators ---
IndX := MainR.X + (FSat * MainR.Width);
IndY := MainR.Y + ((1.0 - FVal) * MainR.Height);
Graphics.DrawEllipse(TGPPen.Create(MakeColor(150, 0, 0, 0), 2), IndX - 6, IndY - 6, 12, 12);
Graphics.DrawEllipse(TGPPen.Create(MakeColor(255, 255, 255, 255), 2), IndX - 5, IndY - 5, 10, 10);
IndY := HueR.Y + (FHue * HueR.Height);
Graphics.DrawEllipse(TGPPen.Create(MakeColor(150, 0, 0, 0), 2), HueR.X - 3, IndY - 6, HueR.Width + 6, 12);
Graphics.DrawEllipse(TGPPen.Create(MakeColor(255, 255, 255, 255), 2), HueR.X - 2, IndY - 5, HueR.Width + 4, 10);
BorderPen.Free;
finally
Graphics.Free;
end;
end;
procedure TFluidColorBox.Resize;
begin
inherited;
Invalidate;
end;
procedure TFluidColorBox.SetBorderColor(const Value: TColor);
begin
if FBorderColor <> Value then begin
FBorderColor := Value;
Invalidate;
end;
end;
procedure TFluidColorBox.SetBorderRadius(const Value: Integer);
begin
if FBorderRadius <> Value then begin
FBorderRadius := Value;
Invalidate;
end;
end;
end.