Debian Codegraph @

tools/castle-editor/components/mbColorLib/HSLColorPicker.pas

raw
1 unit HSLColorPicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms, Menus, Themes,
11 HTMLColors, mbColorConv, HSColorPicker, LVColorPicker, mbBasicPicker;
12
13 type
14 THSLColorPicker = class(TmbBasicPicker)
15 private
16 FHSPicker: THSColorPicker;
17 FLVPicker: TLVColorPicker;
18 FRed, FGreen, FBlue: integer;
19 FHSHint: string;
20 FLVMenu, FHSMenu: TPopupMenu;
21 FLVIncrement: integer;
22 FHSCursor, FLVCursor: TCursor;
23 PBack: TBitmap;
24 function GetBrightnessMode: TBrightnessMode;
25 function GetHue: Integer;
26 function GetSat: Integer;
27 function GetLum: Integer;
28 function GetVal: Integer;
29 function GetMaxHue: Integer;
30 function GetMaxSat: Integer;
31 function GetMaxLum: Integer;
32 function GetMaxVal: Integer;
33 function GetRelHue: Double;
34 function GetRelSat: Double;
35 function GetRelLum: Double;
36 function GetRelVal: Double;
37 function GetLVHint(AMode: TBrightnessMode): String;
38
39 procedure SetBrightnessMode(AMode: TBrightnessMode);
40
41 procedure SetHue(H: integer);
42 procedure SetSat(S: integer);
43 procedure SetLum(L: integer);
44 procedure SetVal(V: Integer);
45
46 procedure SetMaxHue(H: Integer);
47 procedure SetMaxLum(L: Integer);
48 procedure SetMaxSat(S: Integer);
49 procedure SetMaxVal(V: Integer);
50
51 procedure SetRed(R: integer);
52 procedure SetGreen(G: integer);
53 procedure SetBlue(B: integer);
54
55 procedure SetRelHue(H: Double);
56 procedure SetRelLum(L: Double);
57 procedure SetRelSat(S: Double);
58 procedure SetRelVal(V: Double);
59
60 procedure SetHSCursor(c: TCursor);
61 procedure SetHSHint(h: string);
62 procedure SetHSMenu(m: TPopupMenu);
63
64 procedure SetLVCursor(c: TCursor);
65 procedure SetLVHint(AMode: TBrightnessMode; AText: string);
66 procedure SetLVMenu(m: TPopupMenu);
67 procedure SetLVIncrement(i: integer);
68
69 protected
70 procedure DoChange; override;
71 procedure DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
72 function GetColorUnderCursor: TColor; override;
73 function GetSelectedColor: TColor; override;
74 procedure HSPickerChange(Sender: TObject);
75 procedure LVPickerChange(Sender: TObject);
76 procedure Paint; override;
77 procedure Resize; override;
78 procedure SetSelectedColor(Value: TColor); override;
79
80 public
81 constructor Create(AOwner: TComponent); override;
82 destructor Destroy; override;
83 function GetHexColorUnderCursor: string; override;
84 function GetSelectedHexColor: string;
85 procedure SetFocus; override;
86 property ColorUnderCursor;
87 property Red: integer read FRed write SetRed;
88 property Green: integer read FGreen write SetGreen;
89 property Blue: integer read FBlue write SetBlue;
90 property RelHue: Double read GetRelHue write SetRelHue;
91 property RelSaturation: Double read GetRelSat write SetRelSat;
92 property RelLuminance: Double read GetRelLum write SetRelLum;
93 property RelValue: Double read GetRelVal write SetRelVal;
94 published
95 property BrightnessMode: TBrightnessMode read GetBrightnessMode
96 write SetBrightnessMode default bmLuminance;
97 property Hue: integer read GetHue write SetHue default 0;
98 property Saturation: integer read GetSat write SetSat default 255;
99 property Luminance: integer read GetLum write SetLum default 127;
100 property LVIncrement: integer read FLVIncrement write SetLVIncrement default 1;
101 property Value: Integer read GetVal write SetVal default 255;
102 property SelectedColor default clRed;
103 property HSPickerPopupMenu: TPopupMenu read FHSMenu write SetHSMenu;
104 property LVPickerPopupMenu: TPopupMenu read FLVMenu write SetLVMenu;
105 property HSPickerHintFormat: string read FHSHint write SetHSHint;
106 property LPickerHintFormat: string index bmLuminance read GetLVHint write SetLVHint;
107 property VPickerHintFormat: string index bmValue read GetLVHint write SetLVHint;
108 property HSPickerCursor: TCursor read FHSCursor write SetHSCursor default crDefault;
109 property LVPickerCursor: TCursor read FLVCursor write SetLVCursor default crDefault;
110 property MaxHue: Integer read GetMaxHue write SetMaxHue default 360;
111 property MaxSaturation: Integer read GetMaxSat write SetMaxSat default 255;
112 property MaxLuminance: Integer read GetMaxLum write SetMaxLum default 255;
113 property MaxValue: Integer read GetMaxVal write SetMaxVal default 255;
114 property TabStop default true;
115 property ShowHint;
116 property ParentShowHint;
117 property Anchors;
118 property Align;
119 property BorderSpacing;
120 property Visible;
121 property Enabled;
122 property TabOrder;
123 property Color;
124 property ParentColor default true;
125 property OnChange;
126 property OnMouseMove;
127 end;
128
129
130 implementation
131
132 { THSLColorPicker }
133
134 uses
135 mbTrackbarPicker;
136
137 constructor THSLColorPicker.Create(AOwner: TComponent);
138 begin
139 inherited;
140 // ControlStyle := ControlStyle - [csAcceptsControls] + [csOpaque];
141
142 PBack := TBitmap.Create;
143 // PBack.PixelFormat := pf32bit;
144 SetInitialBounds(0, 0, 206, 146);
145 TabStop := true;
146
147 FLVIncrement := 1;
148 FHSCursor := crDefault;
149 FLVCursor := crDefault;
150
151 FHSPicker := THSColorPicker.Create(Self);
152 InsertControl(FHSPicker);
153 with FHSPicker do
154 begin
155 SetInitialBounds(0, 6, 174, 134);
156 Anchors := [akLeft, akTop, akRight, akBottom];
157 // Visible := true;
158 BrightnessMode := bmLuminance;
159 MaxHue := 360;
160 MaxSaturation := 255;
161 MaxLuminance := 255;
162 MaxValue := 255;
163 OnChange := HSPickerChange;
164 OnMouseMove := DoMouseMove;
165 end;
166
167 FLVPicker := TLVColorPicker.Create(Self);
168 InsertControl(FLVPicker);
169 with FLVPicker do
170 begin
171 Layout := lyVertical;
172 SetInitialBounds(184, 0, 25, 146);
173 Anchors := [akRight, akTop, akBottom];
174 // Visible := true;
175 BrightnessMode := bmLuminance;
176 MaxHue := FHSPicker.MaxHue;
177 MaxSaturation := FHSPicker.MaxSaturation;
178 MaxLuminance := FHSPicker.MaxLuminance;
179 MaxValue := FHSPicker.MaxValue;
180 Luminance := MaxLuminance div 2;
181 Value := MaxValue;
182 OnChange := LVPickerChange;
183 OnMouseMove := DoMouseMove;
184 end;
185
186 Hue := 0;
187 Saturation := FHSPicker.MaxLuminance;
188 Luminance := FHSPicker.MaxLuminance div 2;
189 Value := FHSPicker.MaxValue;
190
191 HSPickerHintFormat := 'H: %h S: %hslS'#13'Hex: %hex';
192 {
193 FLVHint[bmLuminance] := 'Luminance: %l';
194 FLVHint[bmValue] := 'Value: %v';
195 }
196 end;
197
198 destructor THSLColorPicker.Destroy;
199 begin
200 PBack.Free;
201 inherited Destroy;
202 end;
203
204 procedure THSLColorPicker.DoChange;
205 var
206 c: TColor;
207 begin
208 c := FLVPicker.SelectedColor;
209 FRed := GetRValue(c);
210 FGreen := GetGValue(c);
211 FBlue := GetBValue(c);
212 inherited;
213 end;
214
215 procedure THSLColorPicker.DoMouseMove(Sender: TObject; Shift: TShiftState;
216 X, Y: Integer);
217 begin
218 if Assigned(OnMouseMove) then
219 OnMouseMove(Self, Shift, x, y);
220 inherited;
221 end;
222
223 function THSLColorPicker.GetBrightnessMode: TBrightnessMode;
224 begin
225 Result := FHSPicker.BrightnessMode;
226 end;
227
228 function THSLColorPicker.GetColorUnderCursor: TColor;
229 begin
230 Result := FHSPicker.ColorUnderCursor;
231 end;
232
233 function THSLColorPicker.GetHue: Integer;
234 begin
235 Result := FHSPicker.Hue;
236 end;
237
238 function THSLColorPicker.GetHexColorUnderCursor: string;
239 begin
240 Result := FHSPicker.GetHexColorUnderCursor;
241 end;
242
243 function THSLColorPicker.GetSat: Integer;
244 begin
245 Result := FHSPicker.Saturation;
246 end;
247
248 function THSLColorPicker.GetLum: integer;
249 begin
250 Result := FLVPicker.Luminance;
251 end;
252
253 function THSLColorPicker.GetVal: Integer;
254 begin
255 Result := FLVPicker.Value;
256 end;
257
258 function THSLColorPicker.GetMaxHue: Integer;
259 begin
260 Result := FHSPicker.MaxHue;
261 end;
262
263 function THSLColorPicker.GetMaxSat: Integer;
264 begin
265 Result := FHSPicker.MaxSaturation;
266 end;
267
268 function THSLColorPicker.GetMaxLum: Integer;
269 begin
270 Result := FLVPicker.MaxLuminance;
271 end;
272
273 function THSLColorPicker.GetMaxVal: Integer;
274 begin
275 Result := FLVPicker.MaxValue;
276 end;
277
278 function THSLColorPicker.GetRelHue: Double;
279 begin
280 Result := FHSPicker.RelHue;
281 end;
282
283 function THSLColorPicker.GetRelLum: Double;
284 begin
285 Result := FLVPicker.RelLuminance;
286 end;
287
288 function THSLColorPicker.GetRelSat: Double;
289 begin
290 Result := FHSPicker.RelSaturation;
291 end;
292
293 function THSLColorPicker.GetRelVal: Double;
294 begin
295 Result := FLVPicker.RelValue;
296 end;
297
298 function THSLColorPicker.GetLVHint(AMode: TBrightnessMode): String;
299 begin
300 case AMode of
301 bmLuminance: Result := FLVPicker.LHintFormat;
302 bmValue : Result := FLVPicker.VHintFormat;
303 end;
304 end;
305
306 function THSLColorPicker.GetSelectedColor: TColor;
307 begin
308 Result := FLVPicker.SelectedColor;
309 end;
310
311 function THSLColorPicker.GetSelectedHexColor: string;
312 begin
313 Result := ColorToHex(GetSelectedColor);
314 end;
315
316 procedure THSLColorPicker.HSPickerChange(Sender: TObject);
317 begin
318 FLVPicker.Lock; // Lock the LVPicker to generate OnChange events here.
319 try
320 FLVPicker.Hue := FHSPicker.Hue;
321 FLVPicker.Saturation := FHSPicker.Saturation;
322 finally
323 FLVPicker.Unlock;
324 DoChange;
325 end;
326 end;
327
328 procedure THSLColorPicker.LVPickerChange(Sender: TObject);
329 begin
330 DoChange;
331 end;
332
333 procedure THSLColorPicker.Resize;
334 begin
335 inherited;
336
337 { if (FHSPicker = nil) or (FLVPicker = nil) then
338 exit;
339 }
340 FHSPicker.Width := Width - FLVPicker.Width - 15;
341 FHSPicker.Height := Height - 12;
342
343 FLVPicker.Left := Width - FLVPicker.Width - 2;
344 FLVPicker.Height := Height; // - 12;
345 end;
346
347 procedure THSLColorPicker.Paint;
348 begin
349 PaintParentBack(Canvas);
350 Canvas.Draw(0, 0, PBack);
351 end;
352
353 procedure THSLColorPicker.SetBlue(B: integer);
354 begin
355 FBlue := B;
356 SetSelectedColor(RGBtoColor(FRed, FGreen, FBlue));
357 end;
358
359 procedure THSLColorPicker.SetBrightnessMode(AMode: TBrightnessMode);
360 begin
361 FHSPicker.BrightnessMode := AMode;
362 FLVPicker.BrightnessMode := AMode;
363 end;
364
365 procedure THSLColorPicker.SetFocus;
366 begin
367 inherited;
368 FHSPicker.SetFocus;
369 end;
370
371 procedure THSLColorPicker.SetGreen(G: integer);
372 begin
373 FGreen := G;
374 SetSelectedColor(RGBtoColor(FRed, FGreen, FBlue));
375 end;
376
377 procedure THSLColorPicker.SetHue(H: integer);
378 begin
379 FHSPicker.Hue := H;
380 FLVPicker.Hue := H;
381 end;
382
383 procedure THSLColorPicker.SetHSCursor(c: TCursor);
384 begin
385 FHSCursor := c;
386 FHSPicker.Cursor := c;
387 end;
388
389 procedure THSLColorPicker.SetHSHint(h: string);
390 begin
391 FHSHint := h;
392 FHSPicker.HintFormat := h;
393 end;
394
395 procedure THSLColorPicker.SetHSMenu(m: TPopupMenu);
396 begin
397 FHSMenu := m;
398 FHSPicker.PopupMenu := m;
399 end;
400
401 procedure THSLColorPicker.SetLum(L: integer);
402 begin
403 FLVPicker.Luminance := L;
404 end;
405
406 procedure THSLColorPicker.SetLVCursor(c: TCursor);
407 begin
408 FLVCursor := c;
409 FLVPicker.Cursor := c;
410 end;
411
412 procedure THSLColorPicker.SetLVHint(AMode: TBrightnessMode; AText: string);
413 begin
414 case AMode of
415 bmLuminance: FLVPicker.LHintFormat := AText;
416 bmValue : FLVPicker.VHintFormat := AText;
417 end;
418 end;
419
420 procedure THSLColorPicker.SetLVIncrement(i: integer);
421 begin
422 FLVIncrement := i;
423 FLVPicker.Increment := i;
424 end;
425
426 procedure THSLColorPicker.SetLVMenu(m: TPopupMenu);
427 begin
428 FLVMenu := m;
429 FLVPicker.PopupMenu := m;
430 end;
431
432 procedure THSLColorPicker.SetMaxHue(H: Integer);
433 begin
434 FHSPicker.MaxHue := H;
435 FLVPicker.MaxHue := H;
436 end;
437
438 procedure THSLColorPicker.SetMaxLum(L: Integer);
439 begin
440 FHSPicker.MaxLuminance := L;
441 FLVPicker.MaxLuminance := L;
442 end;
443
444 procedure THSLColorPicker.SetMaxSat(S: Integer);
445 begin
446 FHSPicker.MaxSaturation := S;
447 FLVPicker.MaxSaturation := S;
448 end;
449
450 procedure THSLColorPicker.SetMaxVal(V: Integer);
451 begin
452 FLVPicker.MaxValue := V;
453 end;
454
455 procedure THSLColorPicker.SetRed(R: integer);
456 begin
457 FRed := R;
458 SetSelectedColor(RGBtoColor(FRed, FGreen, FBlue));
459 end;
460
461 procedure THSLColorPicker.SetRelHue(H: Double);
462 begin
463 FHSPicker.RelHue := H;
464 FLVPicker.RelHue := H;
465 end;
466
467 procedure THSLCOlorPicker.SetRelSat(S: Double);
468 begin
469 FHSPicker.RelSaturation := S;
470 FLVPicker.RelSaturation := S;
471 end;
472
473 procedure THSLColorPicker.SetRelLum(L: Double);
474 begin
475 FHSPicker.RelLuminance := L;
476 FLVPicker.RelLuminance := L;
477 end;
478
479 procedure THSLColorPicker.SetRelVal(V: Double);
480 begin
481 FHSPicker.RelValue := V;
482 FLVPicker.RelValue := V;
483 end;
484
485 procedure THSLColorPicker.SetSat(S: integer);
486 begin
487 if S <> FHSPicker.Saturation then
488 FHSPicker.Saturation := S;
489 if S <> FLVPicker.Saturation then
490 FLVPicker.Saturation := S;
491 end;
492
493 procedure THSLColorPicker.SetSelectedColor(Value: TColor);
494 var
495 c: TColor;
496 H: Double = 0;
497 S: Double = 0;
498 LV: Double = 0;
499 begin
500 c := GetSelectedColor;
501 if c <> Value then
502 begin
503 case GetBrightnessMode of
504 bmLuminance: ColorToHSL(Value, H, S, LV);
505 bmValue : ColorToHSV(value, H, S, LV);
506 end;
507 FHSPicker.RelHue := H;
508 FHSPicker.RelSaturation := S;
509 FLVPicker.SelectedColor := Value;
510 FRed := GetRValue(Value);
511 FGreen := GetGValue(Value);
512 FBlue := GetBValue(Value);
513 end;
514 end;
515
516 procedure THSLColorPicker.SetVal(V: Integer);
517 begin
518 FLVPicker.Value := V;
519 end;
520
521 end.