Debian Codegraph @

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

raw
1 unit mbColorPickerControl;
2
3 {$MODE DELPHI}
4
5 interface
6
7 uses
8 LCLIntf, LCLType, LMessages, SysUtils, Classes, Controls, Graphics, Forms, Themes,
9 HTMLColors, mbColorConv, mbBasicPicker;
10
11 type
12 TMarkerStyle = (msCircle, msSquare, msCross, msCrossCirc);
13
14 { TmbCustomPicker }
15
16 TmbCustomPicker = class(TmbBasicPicker)
17 private
18 FHintFormat: string;
19 FMarkerStyle: TMarkerStyle;
20 FWebSafe: boolean;
21 procedure SetMarkerStyle(s: TMarkerStyle);
22 procedure SetWebSafe(s: boolean);
23 protected
24 FSelected: TColor;
25 mx, my: integer;
26 procedure CreateGradient; override;
27 function GetHintStr({%H-}X, {%H-}Y: Integer): String; override;
28 function GetSelectedColor: TColor; override;
29 procedure InternalDrawMarker(X, Y: Integer; C: TColor);
30 procedure SetSelectedColor(C: TColor); override;
31 procedure WebSafeChanged; dynamic;
32 procedure CMGotFocus(var Message: TLMessage); message CM_ENTER;
33 procedure CMLostFocus(var Message: TLMessage); message CM_EXIT;
34 // procedure CMMouseLeave(var Message: TLMessage); message CM_MOUSELEAVE;
35 property MarkerStyle: TMarkerStyle read FMarkerStyle write SetMarkerStyle;
36 public
37 constructor Create(AOwner: TComponent); override;
38 property ColorUnderCursor;
39 published
40 property HintFormat: string read FHintFormat write FHintFormat;
41 property WebSafe: boolean read FWebSafe write SetWebSafe default false;
42 end;
43
44 TmbColorPickerControl = class(TmbCustomPicker)
45 published
46 property Anchors;
47 property Align;
48 property BorderSpacing;
49 property ShowHint;
50 property ParentShowHint;
51 property Visible;
52 property Enabled;
53 property PopupMenu;
54 property TabOrder;
55 property TabStop default true;
56 property Color;
57 property ParentColor;
58 property DragCursor;
59 property DragMode;
60 property DragKind;
61 property Constraints;
62 property OnContextPopup;
63 property OnGetHintStr;
64 property OnMouseDown;
65 property OnMouseMove;
66 property OnMouseUp;
67 property OnKeyDown;
68 property OnKeyPress;
69 property OnKeyUp;
70 property OnDragDrop;
71 property OnDragOver;
72 property OnEndDrag;
73 property OnEnter;
74 property OnExit;
75 property OnResize;
76 property OnStartDrag;
77 end;
78
79 TmbHSLVColorPickerControl = class(TmbColorPickerControl)
80 private
81 FBrightnessMode: TBrightnessMode;
82 function GetHue: Integer;
83 function GetLum: Integer;
84 function GetSat: Integer;
85 function GetVal: Integer;
86 function GetRed: Integer;
87 function GetGreen: Integer;
88 function GetBlue: Integer;
89 procedure SetHue(h: integer);
90 procedure SetLum(L: Integer);
91 procedure SetSat(s: integer);
92 procedure SetVal(v: integer);
93 procedure SetRed(R: Integer);
94 procedure SetGreen(G: Integer);
95 procedure SetBlue(B: Integer);
96 protected
97 FHue, FSat, FLum, FVal: Double;
98 FMaxHue, FMaxSat, FMaxLum, FMaxVal: Integer;
99 procedure ColorToHSLV(c: TColor; var H, S, L, V: Double);
100 procedure CorrectCoords(var x, y: integer);
101 function HSLVtoColor(H, S, L, V: Double): TColor;
102 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
103 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
104 procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
105 procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
106 procedure SelectColor({%H-}x, {%H-}y: Integer); virtual;
107 procedure SetBrightnessMode(AMode: TBrightnessMode); virtual;
108 procedure SetMaxHue(H: Integer); virtual;
109 procedure SetMaxLum(L: Integer); virtual;
110 procedure SetMaxSat(S: Integer); virtual;
111 procedure SetMaxVal(V: Integer); virtual;
112 procedure SetRelHue(H: Double); virtual;
113 procedure SetRelLum(L: Double); virtual;
114 procedure SetRelSat(S: Double); virtual;
115 procedure SetRelVal(V: Double); virtual;
116 public
117 constructor Create(AOwner: TComponent); override;
118 property RelHue: Double read FHue write SetRelHue;
119 property RelSaturation: Double read FSat write SetRelSat;
120 property RelLuminance: Double read FLum write SetRelLum;
121 property RelValue: Double read FVal write SetRelVal;
122 property Red: Integer read GetRed write SetRed;
123 property Green: Integer read GetGreen write SetGreen;
124 property Blue: Integer read GetBlue write SetBlue;
125 published
126 property BrightnessMode: TBrightnessMode
127 read FBrightnessMode write SetBrightnessMode default bmLuminance;
128 property Hue: integer read GetHue write SetHue;
129 property Luminance: Integer read GetLum write SetLum;
130 property Saturation: integer read GetSat write SetSat;
131 property Value: integer read GetVal write SetVal;
132 property MaxHue: Integer read FMaxHue write SetMaxHue default 360;
133 property MaxSaturation: Integer read FMaxSat write SetMaxSat default 255;
134 property MaxLuminance: Integer read FMaxLum write SetMaxLum default 255;
135 property MaxValue: Integer read FMaxVal write SetMaxVal default 255;
136 end;
137
138 implementation
139
140 uses
141 Math, IntfGraphics, fpimage,
142 PalUtils, SelPropUtils, mbUtils;
143
144 constructor TmbCustomPicker.Create(AOwner: TComponent);
145 begin
146 inherited;
147 //ControlStyle := ControlStyle + [csOpaque] - [csAcceptsControls];
148
149 TabStop := true;
150 mx := 0;
151 my := 0;
152 FHintFormat := 'Hex #%hex'#10#13'RGB[%r, %g, %b]'#10#13'HSL[%hslH, %hslS, %hslL]'#10#13'HSV[%hsvH, %hsvS, %hsvV]'#10#13'CMYK[%c, %m, %y, %k]'#10#13'L*a*b*[%cieL, %cieA, %cieB]'#10#13'XYZ[%cieX, %cieY, %cieZ]';
153 FWebSafe := false;
154 end;
155
156 procedure TmbCustomPicker.CMGotFocus(var Message: TLMessage);
157 begin
158 inherited;
159 Invalidate;
160 end;
161
162 procedure TmbCustomPicker.CMLostFocus(var Message: TLMessage);
163 begin
164 inherited;
165 Invalidate;
166 end;
167 (*
168 procedure TmbCustomPicker.CMMouseLeave(var Message: TLMessage);
169 begin
170 mx := 0;
171 my := 0;
172 inherited;
173 end;
174 *)
175 procedure TmbCustomPicker.CreateGradient;
176 var
177 x, y: Integer;
178 col: TColor;
179 fpcol: TFPColor;
180 intfimg: TLazIntfImage;
181 imgHandle, imgMaskHandle: HBitmap;
182 begin
183 if FBufferBmp = nil then
184 begin
185 FBufferBmp := TBitmap.Create;
186 // FBufferBmp.PixelFormat := pf32bit;
187 end;
188 FBufferBmp.Width := FGradientWidth;
189 FBufferBmp.Height := FGradientHeight;
190
191 intfimg := TLazIntfImage.Create(FBufferBmp.Width, FBufferBmp.Height);
192 try
193 intfImg.LoadFromBitmap(FBufferBmp.Handle, FBufferBmp.MaskHandle);
194
195 for y := 0 to FBufferBmp.Height - 1 do
196 begin
197 for x := 0 to FBufferBmp.Width - 1 do
198 begin
199 col := GetGradientColor2D(x, y);
200 if WebSafe then
201 col := GetWebSafe(col);
202 fpcol := TColorToFPColor(col);
203 intfImg.Colors[x, y] := fpcol;
204 end;
205 end;
206
207 intfimg.CreateBitmaps(imgHandle, imgMaskHandle, false);
208 FBufferBmp.Handle := imgHandle;
209 FBufferBmp.MaskHandle := imgMaskHandle;
210 finally
211 intfimg.Free;
212 end;
213 end;
214
215 function TmbCustomPicker.GetHintStr(X, Y: Integer): String;
216 begin
217 Result := FormatHint(FHintFormat, GetColorUnderCursor);
218 end;
219
220 function TmbCustomPicker.GetSelectedColor: TColor;
221 begin
222 Result := FSelected; // valid for most descendents
223 end;
224
225 procedure TmbCustomPicker.InternalDrawMarker(X, Y: Integer; C: TColor);
226 begin
227 case MarkerStyle of
228 msCircle : DrawSelCirc(x, y, Canvas);
229 msSquare : DrawSelSquare(x, y, Canvas);
230 msCross : DrawSelCross(x, y, Canvas, c);
231 msCrossCirc : DrawSelCrossCirc(x, y, Canvas, c);
232 end;
233 end;
234
235 procedure TmbCustomPicker.SetMarkerStyle(s: TMarkerStyle);
236 begin
237 if FMarkerStyle <> s then
238 begin
239 FMarkerStyle := s;
240 Invalidate;
241 end;
242 end;
243
244 procedure TmbCustomPicker.SetSelectedColor(C: TColor);
245 begin
246 FSelected := C;
247 //handled in descendents
248 end;
249
250 procedure TmbCustomPicker.SetWebSafe(s: boolean);
251 begin
252 if FWebSafe <> s then
253 begin
254 FWebSafe := s;
255 WebSafeChanged;
256 end;
257 end;
258
259 procedure TmbCustomPicker.WebSafeChanged;
260 begin
261 CreateGradient;
262 Invalidate;
263 end;
264
265
266 { TmbHSLVColorPickerControl }
267
268 constructor TmbHSLVColorPickerControl.Create(AOwner: TComponent);
269 begin
270 inherited Create(AOwner);
271 FBrightnessMode := bmLuminance;
272 FMaxHue := 360;
273 FMaxSat := 255;
274 FMaxVal := 255;
275 FMaxLum := 255;
276 end;
277
278 procedure TmbHSLVColorPickerControl.ColorToHSLV(c: TColor;
279 var H, S, L, V: Double);
280 begin
281 case FBrightnessMode of
282 bmLuminance : ColorToHSL(c, H, S, L);
283 bmValue : ColorToHSV(c, H, S, V);
284 end;
285 end;
286
287 procedure TmbHSLVColorPickerControl.CorrectCoords(var x, y: integer);
288 begin
289 Clamp(x, 0, Width - 1);
290 Clamp(y, 0, Height - 1);
291 end;
292
293 function TmbHSLVColorPickerControl.GetBlue: Integer;
294 begin
295 Result := GetBValue(GetSelectedColor);
296 end;
297
298 function TmbHSLVColorPickerControl.GetGreen: Integer;
299 begin
300 Result := GetGValue(GetSelectedColor);
301 end;
302
303 function TmbHSLVColorPickerControl.GetHue: Integer;
304 begin
305 Result := Round(FHue * FMaxHue);
306 end;
307
308 function TmbHSLVColorPickerControl.GetLum: Integer;
309 begin
310 Result := Round(FLum * FMaxLum);
311 end;
312
313 function TmbHSLVColorPickerControl.GetRed: Integer;
314 begin
315 Result := GetRValue(GetSelectedColor);
316 end;
317
318 function TmbHSLVColorPickerControl.GetSat: Integer;
319 begin
320 Result := Round(FSat * FMaxSat);
321 end;
322
323 function TmbHSLVColorPickerControl.GetVal: Integer;
324 begin
325 Result := Round(FVal * FMaxVal);
326 end;
327
328 function TmbHSLVColorPickerControl.HSLVtoColor(H, S, L, V: Double): TColor;
329 begin
330 case FBrightnessMode of
331 bmLuminance : Result := HSLToColor(H, S, L);
332 bmValue : Result := HSVtoColor(H, S, V);
333 end;
334 if WebSafe then
335 Result := GetWebSafe(Result);
336 end;
337
338 procedure TmbHSLVColorPickerControl.KeyDown(var Key: Word; Shift: TShiftState);
339 var
340 eraseKey: Boolean;
341 delta: Integer;
342 begin
343 eraseKey := true;
344 delta := IfThen(ssCtrl in Shift, 10, 1);
345
346 case Key of
347 VK_LEFT : SelectColor(mx - delta, my);
348 VK_RIGHT : SelectColor(mx + delta, my);
349 VK_UP : SelectColor(mx, my - delta);
350 VK_DOWN : SelectColor(mx, my + delta);
351 else eraseKey := false;
352 end;
353
354 if eraseKey then
355 Key := 0;
356
357 inherited;
358 end;
359
360 procedure TmbHSLVColorPickerControl.MouseDown(Button: TMouseButton; Shift: TShiftState;
361 X, Y: Integer);
362 begin
363 inherited;
364 if csDesigning in ComponentState then
365 Exit;
366 if Button = mbLeft then
367 SelectColor(x, y);
368 SetFocus;
369 end;
370
371 procedure TmbHSLVColorPickerControl.MouseMove(Shift: TShiftState; X, Y: Integer);
372 begin
373 inherited;
374 if csDesigning in ComponentState then
375 Exit;
376 if ssLeft in Shift then
377 SelectColor(x, y);
378 end;
379
380 procedure TmbHSLVColorPickerControl.MouseUp(Button: TMouseButton; Shift: TShiftState;
381 X, Y: Integer);
382 begin
383 inherited;
384 if csDesigning in ComponentState then
385 Exit;
386 if Button = mbLeft then
387 SelectColor(x, y);
388 end;
389
390 procedure TmbHSLVColorPickerControl.SelectColor(x, y: Integer);
391 begin
392 end;
393
394 procedure TmbHSLVColorPickerControl.SetBlue(B: Integer);
395 begin
396 Clamp(B, 0, 255);
397 SetSelectedColor(RgbToColor(Red, Green, B));
398 end;
399
400 procedure TmbHSLVColorPickerControl.SetBrightnessMode(AMode: TBrightnessMode);
401 var
402 c: TColor;
403 begin
404 c := HSLVtoColor(FHue, FSat, FLum, FVal);
405 FBrightnessMode := AMode;
406 ColorToHSLV(c, FHue, FSat, FLum, FVal);
407 CreateGradient;
408 Invalidate;
409 DoChange;
410 end;
411
412 procedure TmbHSLVColorPickerControl.SetGreen(G: Integer);
413 begin
414 Clamp(G, 0, 255);
415 SetSelectedColor(RgbToColor(Red, G, Blue));
416 end;
417
418 procedure TmbHSLVColorPickerControl.SetHue(H: Integer);
419 begin
420 SetRelHue(H / FMaxHue);
421 end;
422
423 procedure TmbHSLVColorPickerControl.SetLum(L: Integer);
424 begin
425 SetRelLum(L / FMaxLum);
426 end;
427
428 procedure TmbHSLVColorPickerControl.SetMaxHue(h: Integer);
429 begin
430 if h = FMaxHue then
431 exit;
432 FMaxHue := h;
433 CreateGradient;
434 Invalidate;
435 end;
436
437 procedure TmbHSLVColorPickerControl.SetMaxLum(L: Integer);
438 begin
439 if L = FMaxLum then
440 exit;
441 FMaxLum := L;
442 if BrightnessMode = bmLuminance then begin
443 CreateGradient;
444 Invalidate;
445 end;
446 end;
447
448 procedure TmbHSLVColorPickerControl.SetMaxSat(S: Integer);
449 begin
450 if S = FMaxSat then
451 exit;
452 FMaxSat := S;
453 CreateGradient;
454 Invalidate;
455 end;
456
457 procedure TmbHSLVColorPickerControl.SetMaxVal(V: Integer);
458 begin
459 if V = FMaxVal then
460 exit;
461 FMaxVal := V;
462 if BrightnessMode = bmLuminance then
463 begin
464 CreateGradient;
465 Invalidate;
466 end;
467 end;
468
469 procedure TmbHSLVColorPickerControl.SetRed(R: Integer);
470 begin
471 Clamp(R, 0, 255);
472 SetSelectedColor(RgbToColor(R, Green, Blue));
473 end;
474
475 procedure TmbHSLVColorPickerControl.SetRelHue(H: Double);
476 begin
477 Clamp(H, 0, 1.0);
478 if FHue <> H then
479 begin
480 FHue := H;
481 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
482 CreateGradient;
483 Invalidate;
484 DoChange;
485 end;
486 end;
487
488 procedure TmbHSLVColorPickerControl.SetRelLum(L: Double);
489 begin
490 Clamp(L, 0, 1.0);
491 if FLum <> L then
492 begin
493 FLum := L;
494 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
495 if BrightnessMode = bmLuminance then begin
496 CreateGradient;
497 Invalidate;
498 end;
499 DoChange;
500 end;
501 end;
502
503 procedure TmbHSLVColorPickerControl.SetRelSat(S: Double);
504 begin
505 Clamp(S, 0, 1.0);
506 if FSat <> S then
507 begin
508 FSat := S;
509 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
510 CreateGradient;
511 Invalidate;
512 DoChange;
513 end;
514 end;
515
516 procedure TmbHSLVColorPickerControl.SetRelVal(V: Double);
517 begin
518 Clamp(v, 0, 1.0);
519 if FVal <> V then
520 begin
521 FVal := V;
522 if BrightnessMode = bmValue then
523 begin
524 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
525 CreateGradient;
526 Invalidate;
527 end;
528 DoChange;
529 end;
530 end;
531
532 procedure TmbHSLVColorPickerControl.SetSat(S: Integer);
533 begin
534 SetRelSat(S / FMaxSat);
535 end;
536
537 procedure TmbHSLVColorPickerControl.SetVal(V: Integer);
538 begin
539 SetRelVal(V / FMaxVal);
540 end;
541
542
543 end.