Debian Codegraph @

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

raw
1 unit HSCirclePicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Math, Forms, Themes,
11 HTMLColors, mbColorConv, mbColorPickerControl;
12
13 type
14 THSCirclePicker = class(TmbHSLVColorPickerControl)
15 private
16 FSatCircColor, FHueLineColor: TColor;
17 FShowSatCirc: boolean;
18 FShowHueLine: boolean;
19 FShowSelCirc: boolean;
20 { Commented out.
21 These were causing warnings (they obscure ancestor virtual
22 SetRelHue, SetRelSat) and actually these were unused. }
23 // procedure SetRelHue(H: Double);
24 // procedure SetRelSat(S: Double);
25 procedure SetSatCircColor(c: TColor);
26 procedure SetHueLineColor(c: TColor);
27 procedure DrawSatCirc;
28 procedure DrawHueLine;
29 procedure DrawMarker(x, y: integer);
30 procedure SetShowSatCirc(s: boolean);
31 procedure SetShowSelCirc(s: boolean);
32 procedure SetShowHueLine(s: boolean);
33 procedure UpdateCoords;
34 protected
35 procedure CreateGradient; override;
36 // procedure CreateWnd; override;
37 function GetGradientColor2D(X, Y: Integer): TColor; override;
38 procedure Paint; override;
39 procedure Resize; override;
40 procedure SelectColor(x, y: integer); override;
41 procedure SetSelectedColor(c: TColor); override;
42 public
43 constructor Create(AOwner: TComponent); override;
44 function GetColorAtPoint(x, y: integer): TColor; override;
45 published
46 property BrightnessMode default bmValue;
47 property SelectedColor default clRed;
48 property Hue default 0;
49 property Luminance default 127;
50 property Saturation default 255;
51 property Value default 255;
52 property MaxHue default 360;
53 property MaxLuminance default 255;
54 property MaxSaturation default 255;
55 property MaxValue default 255;
56 property SaturationCircleColor: TColor read FSatCircColor write SetSatCircColor default clSilver;
57 property HueLineColor: TColor read FHueLineColor write SetHueLineColor default clGray;
58 property ShowSaturationCircle: boolean read FShowSatCirc write SetShowSatCirc default true;
59 property ShowHueLine: boolean read FShowHueLine write SetShowHueLine default true;
60 property ShowSelectionCircle: boolean read FShowSelCirc write SetShowSelCirc default true;
61 property MarkerStyle default msCrossCirc;
62 property OnChange;
63 end;
64
65 implementation
66
67 uses
68 mbUtils;
69
70 { THSCirclePicker }
71
72 constructor THSCirclePicker.Create(AOwner: TComponent);
73 begin
74 inherited;
75 SetInitialBounds(0, 0, 204, 204);
76 FHue := 0;
77 FSat := 1.0;
78 FLum := 0.5;
79 FVal := 1.0;
80 SetSelectedColor(clRed);
81 BrightnessMode := bmValue;
82 FSatCircColor := clSilver;
83 FHueLineColor := clGray;
84 FShowSatCirc := true;
85 FShowHueLine := true;
86 FShowSelCirc := true;
87 MarkerStyle := msCrossCirc;
88 end;
89
90 procedure THSCirclePicker.CreateGradient;
91 begin
92 FGradientWidth := Min(Width, Height);
93 FGradientHeight := FGradientWidth;
94 inherited;
95 end;
96 (*
97 procedure THSCirclePicker.CreateWnd;
98 begin
99 inherited;
100 CreateGradient;
101 UpdateCoords;
102 end; *)
103
104 procedure THSCirclePicker.DrawSatCirc;
105 var
106 delta: integer;
107 radius: integer;
108 begin
109 if not FShowSatCirc then
110 exit;
111 if (FSat > 0) and (FSat < 1.0) then
112 begin
113 radius := Min(Width, Height) div 2;
114 Canvas.Pen.Color := FSatCircColor;
115 Canvas.Brush.Style := bsClear;
116 delta := round(radius * FSat);
117 Canvas.Ellipse(radius - delta, radius - delta, radius + delta, radius + delta);
118 end;
119 end;
120
121 procedure THSCirclePicker.DrawHueLine;
122 var
123 angle: double;
124 sinAngle, cosAngle: Double;
125 radius: integer;
126 begin
127 if not FShowHueLine then
128 exit;
129 radius := Min(Width, Height) div 2;
130 if (FHue >= 0) and (FHue <= 1.0) then
131 begin
132 angle := -FHue * 2 * pi;
133 SinCos(angle, sinAngle, cosAngle);
134 Canvas.Pen.Color := FHueLineColor;
135 Canvas.MoveTo(radius, radius);
136 Canvas.LineTo(radius + round(radius*cosAngle), radius + round(radius*sinAngle));
137 end;
138 end;
139
140 procedure THSCirclePicker.DrawMarker(x, y: integer);
141 var
142 c: TColor;
143 begin
144 if not FShowSelCirc then
145 exit;
146 if Focused or (csDesigning in ComponentState) then
147 c := clBlack
148 else
149 c := clGray;
150 InternalDrawMarker(x, y, c);
151 end;
152
153 { Castle Game Engine additional property:
154 allows to treat clicks within control rectangle,
155 but outside of the circle, as still valid (instead of resetting color
156 to default). }
157 const
158 TolerateOutsideCircle = true;
159
160 function THSCirclePicker.GetColorAtPoint(x, y: integer): TColor;
161 var
162 angle: Double;
163 dx, dy, r, radius: integer;
164 h, s: double;
165 begin
166 radius := Min(Width, Height) div 2;
167 dx := x - Radius;
168 dy := y - Radius;
169
170 r := round(sqrt(sqr(dx) + sqr(dy)));
171 if TolerateOutsideCircle then
172 r := Min(r, Radius);
173 if r <= radius then
174 begin
175 angle := 360 + 180 * arctan2(-dy, dx) / pi;
176 if angle < 0 then
177 angle := angle + 360
178 else if angle > 360 then
179 angle := angle - 360;
180 h := angle / 360;
181 s := r / radius;
182 Result := HSLVtoColor(h, s, FLum, FVal);
183 if WebSafe then
184 Result := GetWebSafe(Result);
185 end else
186 Result := clNone;
187 end;
188
189 { Outer loop: Y, Inner loop: X }
190 function THSCirclePicker.GetGradientColor2D(X, Y: Integer): TColor;
191 var
192 dx, dy: Integer;
193 dSq, radiusSq: Integer;
194 radius, size: Integer;
195 S, H: Double;
196 begin
197 size := FGradientWidth; // or Height, they are the same...
198 radius := size div 2;
199 radiusSq := sqr(radius);
200 dx := X - radius;
201 dy := Y - radius;
202 dSq := sqr(dx) + sqr(dy);
203 if dSq <= radiusSq then
204 begin
205 if radius <> 0 then
206 S := sqrt(dSq) / radius
207 else
208 S := 0;
209 H := 180 * (1 + arctan2(dx, dy) / pi); // wp: order (x,y) is correct!
210 H := H + 90;
211 if H > 360 then H := H - 360;
212 Result := HSLVtoColor(H/360, S, FLum, FVal);
213 if WebSafe then
214 Result := GetWebSafe(Result);
215 end else
216 Result := GetDefaultColor(dctBrush);
217 end;
218
219 procedure THSCirclePicker.Paint;
220 var
221 rgn: HRGN;
222 R: TRect;
223 begin
224 PaintParentBack(Canvas);
225 R := ClientRect;
226 R.Right := R.Left + Min(Width, Height);
227 R.Bottom := R.Top + Min(Width, Height);
228 InflateRect(R, -1, -1); // Avoid spurious black pixels at the border
229 rgn := CreateEllipticRgnIndirect(R);
230 SelectClipRgn(Canvas.Handle, rgn);
231 Canvas.Draw(0, 0, FBufferBmp);
232 DeleteObject(rgn);
233 DrawSatCirc;
234 DrawHueLine;
235 DrawMarker(mx, my);
236 end;
237
238 procedure THSCirclePicker.Resize;
239 begin
240 inherited;
241 CreateGradient;
242 UpdateCoords;
243 end;
244
245 procedure THSCirclePicker.SelectColor(x, y: integer);
246 var
247 angle: Double;
248 dx, dy, r, radius: integer;
249 H, S: Double;
250 begin
251 mx := x;
252 my := y;
253
254 radius := Min(Width, Height) div 2;
255 dx := x - radius;
256 dy := y - radius;
257 r := round(sqrt(sqr(dx) + sqr(dy)));
258
259 if r > radius then // point outside circle
260 begin
261 if TolerateOutsideCircle then
262 r := Radius
263 else
264 {$warnings off} // silence FPC warning "unreachable code"
265 begin
266 SetSelectedColor(clNone);
267 exit;
268 end;
269 {$warnings on}
270 end;
271
272 //FSelectedColor := clWhite; // ????
273 angle := 360 + 180*arctan2(-dy, dx) / pi; // wp: "-y, x" correct? The others have "x, y"
274 if angle < 0 then
275 angle := angle + 360
276 else if angle > 360 then
277 angle := angle - 360;
278 H := angle / 360;
279 if r > radius then
280 S := 1.0
281 else
282 S := r / radius;
283
284 if (H = FHue) and (S = FSat) then
285 exit;
286
287 FHue := H;
288 FSat := S;
289 FSelected := HSLVToColor(FHue, FSat, FLum, FVal);
290 UpdateCoords;
291 Invalidate;
292 DoChange;
293 end;
294
295 procedure THSCirclePicker.SetHueLineColor(c: TColor);
296 begin
297 if FHueLineColor <> c then
298 begin
299 FHueLineColor := c;
300 Invalidate;
301 end;
302 end;
303
304 (*
305 procedure THSCirclePicker.SetRelHue(H: Double);
306 begin
307 if H > 1 then H := H - 1;
308 if H < 0 then H := H + 1;
309 if FHue <> h then
310 begin
311 FHue := h;
312 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
313 UpdateCoords;
314 Invalidate;
315 DoChange;
316 end;
317 end;
318
319 procedure THSCirclePicker.SetRelSat(S: Double);
320 begin
321 Clamp(S, 0.0, 1.0);
322 if FSat <> S then
323 begin
324 FSat := s;
325 FSelected := HSLVToColor(FHue, FSat, FLum, FVal);
326 UpdateCoords;
327 Invalidate;
328 DoChange;
329 end;
330 end;
331 *)
332
333 procedure THSCirclePicker.SetSatCircColor(c: TColor);
334 begin
335 if FSatCircColor <> c then
336 begin
337 FSatCircColor := c;
338 Invalidate;
339 end;
340 end;
341
342 procedure THSCirclePicker.SetSelectedColor(c: TColor);
343 begin
344 if WebSafe then
345 c := GetWebSafe(c);
346 if c = FSelected then
347 exit;
348 ColorToHSLV(c, FHue, FSat, FLum, FVal);
349 FSelected := c;
350 UpdateCoords;
351 Invalidate;
352 DoChange;
353 end;
354
355 procedure THSCirclePicker.SetShowHueLine(s: boolean);
356 begin
357 if FShowHueLine <> s then
358 begin
359 FShowHueLine := s;
360 Invalidate;
361 end;
362 end;
363
364 procedure THSCirclePicker.SetShowSatCirc(s: boolean);
365 begin
366 if FShowSatCirc <> s then
367 begin
368 FShowSatCirc := s;
369 Invalidate;
370 end;
371 end;
372
373 procedure THSCirclePicker.SetShowSelCirc(s: boolean);
374 begin
375 if FShowSelCirc <> s then
376 begin
377 FShowSelCirc := s;
378 Invalidate;
379 end;
380 end;
381
382 procedure THSCirclePicker.UpdateCoords;
383 var
384 r, angle: double;
385 sinAngle, cosAngle: Double;
386 radius: integer;
387 begin
388 radius := Min(Width, Height) div 2;
389 r := -FSat * radius;
390 angle := -(FHue * 2 + 1) * pi;
391 SinCos(angle, sinAngle, cosAngle);
392 mx := round(cosAngle * r) + radius;
393 my := round(sinAngle * r) + radius;
394 end;
395
396 end.