Debian Codegraph @

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

raw
1 unit HSLRingPicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics,
11 Forms, Menus, Math, Themes,
12 mbColorConv, HRingPicker, SLColorPicker, HTMLColors, mbBasicPicker;
13
14 type
15 THSLRingPicker = class(TmbBasicPicker)
16 private
17 FRingPicker: THRingPicker;
18 FSLPicker: TSLColorPicker;
19 FSelectedColor: TColor;
20 // FRValue, FGValue, FBValue: integer;
21 FRingHint{, FSLHint}: string;
22 FSLMenu, FRingMenu: TPopupMenu;
23 FSLCursor, FRingCursor: TCursor;
24 PBack: TBitmap;
25 function GetBrightnessMode: TBrightnessMode;
26 function GetHue: Integer;
27 function GetLum: Integer;
28 function GetSat: Integer;
29 function GetVal: Integer;
30 function GetMaxHue: Integer;
31 function GetMaxLum: Integer;
32 function GetMaxSat: Integer;
33 function GetRed: Integer;
34 function GetGreen: Integer;
35 function GetBlue: Integer;
36 function GetLVHint(AMode: TBrightnessMode): String;
37 procedure SetBrightnessMode(AMode: TBrightnessMode);
38 procedure SetHue(H: integer);
39 procedure SetSat(S: integer);
40 procedure SetLum(L: integer);
41 procedure SetVal(V: Integer);
42 procedure SetMaxHue(H: Integer);
43 procedure SetMaxLum(L: Integer);
44 procedure SetMaxSat(S: Integer);
45 procedure SetRed(R: integer);
46 procedure SetGreen(G: integer);
47 procedure SetBlue(B: integer);
48 procedure SetRingHint(h: string);
49 procedure SetSLMenu(m: TPopupMenu);
50 procedure SetRingMenu(m: TPopupMenu);
51 procedure SetRingCursor(c: TCursor);
52 procedure SetSLCursor(c: TCursor);
53 procedure SetLVHint(AMode: TBrightnessMode; AText: String);
54 protected
55 // procedure CreateWnd; override;
56 procedure DoChange; override;
57 procedure DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
58 function GetColorUnderCursor: TColor; override;
59 function GetSelectedColor: TColor; override;
60 procedure Paint; override;
61 procedure Resize; override;
62 procedure RingPickerChange(Sender: TObject);
63 procedure SetSelectedColor(c: TColor); override;
64 procedure SLPickerChange(Sender: TObject);
65 public
66 constructor Create(AOwner: TComponent); override;
67 destructor Destroy; override;
68 function GetHexColorUnderCursor: string; override;
69 function GetSelectedHexColor: string;
70 procedure SetFocus; override;
71 property ColorUnderCursor;
72 property Red: integer read GetRed write SetRed;
73 property Green: integer read GetGreen write SetGreen;
74 property Blue: integer read GetBlue write SetBlue;
75 published
76 property BrightnessMode: TBrightnessMode read GetBrightnessMode
77 write SetBrightnessMode default bmValue;
78 property Hue: integer read GetHue write SetHue default 0;
79 property Saturation: integer read GetSat write SetSat default 255;
80 property Luminance: integer read GetLum write SetLum default 127;
81 property Value: Integer read GetVal write SetVal default 255;
82 property SelectedColor default clRed;
83 property RingPickerPopupMenu: TPopupMenu read FRingMenu write SetRingMenu;
84 property SLPickerPopupMenu: TPopupMenu read FSLMenu write SetSLMenu;
85 property RingPickerHintFormat: string read FRingHint write SetRingHint;
86 property SLPickerHintFormat: string index bmLuminance read GetLVHint write SetLVHint;
87 property SVPickerHintFormat: String index bmValue read GetLVHint write SetLVHint;
88 property RingPickerCursor: TCursor read FRingCursor write SetRingCursor default crDefault;
89 property SLPickerCursor: TCursor read FSLCursor write SetSLCursor default crDefault;
90 property MaxHue: Integer read GetMaxHue write SetMaxHue default 360;
91 property MaxLuminance: Integer read GetMaxLum write SetMaxLum default 240;
92 property MaxSaturation: Integer read GetMaxSat write SetMaxSat default 240;
93 property TabStop default true;
94 property ShowHint;
95 property ParentShowHint;
96 property Anchors;
97 property Align;
98 property Visible;
99 property Enabled;
100 property TabOrder;
101 property Color;
102 property ParentColor default true;
103 property OnChange; //: TNotifyEvent read FOnChange write FOnChange;
104 property OnMouseMove;
105 end;
106
107 implementation
108
109 {THSLRingPicker}
110
111 constructor THSLRingPicker.Create(AOwner: TComponent);
112 begin
113 inherited;
114 // ControlStyle := ControlStyle - [csAcceptsControls] + [csOpaque{$IFDEF DELPHI_7_UP}, csParentBackground{$ENDIF}];
115
116 PBack := TBitmap.Create;
117 // PBack.PixelFormat := pf32bit;
118 SetInitialBounds(0, 0, 245, 245);
119 TabStop := true;
120 FRingCursor := crDefault;
121 FSLCursor := crDefault;
122
123 FRingPicker := THRingPicker.Create(Self);
124 InsertControl(FRingPicker);
125 with FRingPicker do
126 begin
127 SetInitialBounds(0, 0, 246, 246);
128 BrightnessMode := bmValue;
129 Align := alClient;
130 OnChange := RingPickerChange;
131 OnMouseMove := DoMouseMove;
132 end;
133
134 FSLPicker := TSLColorPicker.Create(Self);
135 InsertControl(FSLPicker);
136 with FSLPicker do
137 begin
138 SetInitialBounds(63, 63, 120, 120);
139 BrightnessMode := bmValue;
140 SLHintFormat := 'S: %hslS L: %l'#13'Hex: %hex';
141 SVHintFormat := 'S: %hslS V: %v'#13'Hex: %hex';
142 OnChange := SLPickerChange;
143 OnMouseMove := DoMouseMove;
144 end;
145
146 SetSelectedColor(clRed);
147 end;
148
149 destructor THSLRingPicker.Destroy;
150 begin
151 PBack.Free;
152 inherited Destroy;
153 end;
154 (*
155 procedure THSLRingPicker.CreateWnd;
156 begin
157 inherited;
158 //PaintParentBack(PBack);
159 end; *)
160
161 procedure THSLRingPicker.DoChange;
162 begin
163 FSelectedColor := FSLPicker.SelectedColor;
164 inherited;
165 end;
166
167 procedure THSLRingPicker.DoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
168 begin
169 if Assigned(OnMouseMove) then
170 OnMouseMove(Self, Shift, x, y);
171 inherited;
172 end;
173
174 function THSLRingPicker.GetBlue: Integer;
175 begin
176 Result := GetRValue(FSelectedColor);
177 end;
178
179 function THSLRingPicker.GetBrightnessMode: TBrightnessMode;
180 begin
181 Result := FSLPicker.BrightnessMode;
182 end;
183
184 function THSLRingPicker.GetColorUnderCursor: TColor;
185 begin
186 Result := FSLPicker.ColorUnderCursor;
187 end;
188
189 function THSLRingPicker.GetGreen: Integer;
190 begin
191 Result := GetGValue(FSelectedColor);
192 end;
193
194 function THSLRingPicker.GetHexColorUnderCursor: string;
195 begin
196 Result := FSLPicker.GetHexColorUnderCursor;
197 end;
198
199 function THSLRingPicker.GetHue: Integer;
200 begin
201 Result := FRingPicker.Hue;
202 end;
203
204 function THSLRingPicker.GetLum: Integer;
205 begin
206 Result := FSLPicker.Luminance;
207 end;
208
209 function THSLRingPicker.GetLVHint(AMode: TBrightnessMode): String;
210 begin
211 case BrightnessMode of
212 bmLuminance: Result := FSLPicker.SLHintFormat;
213 bmValue : Result := FSLPicker.SVHintFormat;
214 end;
215 end;
216
217 function THSLRingPicker.GetMaxHue: Integer;
218 begin
219 Result := FRingPicker.MaxHue;
220 end;
221
222 function THSLRingPicker.GetMaxSat: Integer;
223 begin
224 Result := FSLPicker.MaxSaturation;
225 end;
226
227 function THSLRingPicker.GetMaxLum: Integer;
228 begin
229 Result := FSLPicker.MaxLuminance;
230 end;
231
232 function THSLRingPicker.GetRed: Integer;
233 begin
234 Result := GetRValue(FSelectedColor);
235 end;
236
237 function THSLRingPicker.GetSat: Integer;
238 begin
239 Result := FSLPicker.Saturation;
240 end;
241
242 function THSLRingPicker.GetSelectedColor: TColor;
243 begin
244 Result := FSelectedColor;
245 end;
246
247 function THSLRingPicker.GetSelectedHexColor: string;
248 begin
249 Result := ColorToHex(FSelectedColor);
250 end;
251
252 function THSLRingPicker.GetVal: Integer;
253 begin
254 Result := FSLPicker.Value;
255 end;
256
257 procedure THSLRingPicker.Paint;
258 begin
259 PaintParentBack(PBack);
260 Canvas.Draw(0, 0, PBack);
261 end;
262
263 procedure THSLRingPicker.Resize;
264 var
265 circ: TPoint;
266 ctr: double;
267 begin
268 inherited;
269 if (FRingPicker = nil) or (FSLPicker = nil) then
270 exit;
271
272 ctr := Min(Width, Height) / 100;
273 circ.x := Min(Width, Height) div 2;
274 circ.y := circ.x;
275
276 FRingPicker.Radius := circ.x - round(12*ctr);
277
278 FSLPicker.Left := circ.x - FSLPicker.Width div 2;
279 FSLPicker.Top := circ.y - FSLPicker.Height div 2;
280 FSLPicker.Width := round(50 * ctr);
281 FSLPicker.Height := FSLPicker.Width;
282
283 PaintParentBack(PBack);
284 end;
285
286 procedure THSLRingPicker.RingPickerChange(Sender: TObject);
287 begin
288 if FSLPicker.Hue <> FRingPicker.Hue then
289 begin
290 FSLPicker.Hue := FRingPicker.Hue;
291 DoChange;
292 end;
293 end;
294
295 procedure THSLRingPicker.SetBlue(B: integer);
296 begin
297 SetSelectedColor(RgbToColor(Red, Green, B));
298 end;
299
300 procedure THSLRingPicker.SetBrightnessMode(AMode: TBrightnessMode);
301 begin
302 FRingPicker.BrightnessMode := AMode;
303 FSLPicker.BrightnessMode := AMode;
304 end;
305
306 procedure THSLRingPicker.SetFocus;
307 begin
308 inherited;
309 FRingPicker.SetFocus;
310 end;
311
312 procedure THSLRingPicker.SetGreen(G: integer);
313 begin
314 SetSelectedColor(RgbToColor(Red, G, Blue));
315 end;
316
317 procedure THSLRingPicker.SetHue(H: integer);
318 begin
319 FRingPicker.Hue := H;
320 FSLPicker.Hue := H;
321 end;
322
323 procedure THSLRingPicker.SetLum(L: integer);
324 begin
325 FSLPicker.Luminance := L;
326 end;
327
328 procedure THSLRingPicker.SetLVHint(AMode: TBrightnessMode; AText: string);
329 begin
330 case AMode of
331 bmLuminance: FSLPicker.SLHintFormat := AText;
332 bmValue : FSLPicker.SVHintFormat := AText;
333 end;
334 end;
335
336 procedure THSLRingPicker.SetMaxHue(H: Integer);
337 begin
338 FRingPicker.MaxHue := H;
339 FSLPicker.MaxHue := H;
340 end;
341
342 procedure THSLRingPicker.SetMaxLum(L: Integer);
343 begin
344 FRingPicker.MaxLuminance := L;
345 FSLPicker.MaxLuminance := L;
346 end;
347
348 procedure THSLRingPicker.SetMaxSat(S: Integer);
349 begin
350 FRingPicker.MaxSaturation := S;
351 FSLPicker.MaxSaturation := S;
352 end;
353
354 procedure THSLRingPicker.SetRed(R: integer);
355 begin
356 SetSelectedColor(RgbToColor(R, Green, Blue));
357 end;
358
359 procedure THSLRingPicker.SetRingCursor(c: TCursor);
360 begin
361 FRingCursor := c;
362 FRingPicker.Cursor := c;
363 end;
364
365 procedure THSLRingPicker.SetRingHint(h: string);
366 begin
367 FRingHint := h;
368 FRingPicker.HintFormat := h;
369 end;
370
371 procedure THSLRingPicker.SetRingMenu(m: TPopupMenu);
372 begin
373 FRingMenu := m;
374 FRingPicker.PopupMenu := m;
375 end;
376
377 procedure THSLRingPicker.SetSat(S: integer);
378 begin
379 FSLPicker.Saturation := S;
380 end;
381
382 procedure THSLRingPicker.SetSelectedColor(c: TColor);
383 var
384 H, S, LV: Double;
385 begin
386 case BrightnessMode of
387 bmLuminance: ColorToHSL(c, H, S, LV);
388 bmValue : ColorToHSV(c, H, S, LV);
389 end;
390 FRingPicker.RelHue := H;
391 FSLPicker.SelectedColor := c;
392 FSelectedColor := FSLPicker.SelectedColor;
393 end;
394
395 procedure THSLRingPicker.SetSLCursor(c: TCursor);
396 begin
397 FSLCursor := c;
398 FSLPicker.Cursor := c;
399 end;
400
401 procedure THSLRingPicker.SetSLMenu(m: TPopupMenu);
402 begin
403 FSLMenu := m;
404 FSLPicker.PopupMenu := m;
405 end;
406
407 procedure THSLRingPicker.SetVal(V: integer);
408 begin
409 FSLPicker.Value := V;
410 end;
411
412 procedure THSLRingPicker.SLPickerChange(Sender: TObject);
413 begin
414 if FSelectedColor = FSLPicker.SelectedColor then
415 exit;
416 FSelectedColor := FSLPicker.SelectedColor;
417 DoChange;
418 end;
419
420 end.