Debian Codegraph @

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

raw
1 { A trackbar picker for Luminance or Value parameters from the HSL or HSV
2 color models (depending on setting for BrightnessMode) }
3
4 unit LVColorPicker;
5
6 interface
7
8 {$IFDEF FPC}
9 {$MODE DELPHI}
10 {$ENDIF}
11
12 uses
13 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms,
14 HTMLColors, mbColorConv, mbTrackBarPicker;
15
16 type
17 TLVColorPicker = class(TmbHSLVTrackBarPicker)
18 private
19 FHint: array[TBrightnessMode] of string;
20 function ArrowPosFromLum(L: Double): integer;
21 function ArrowPosFromVal(V: Double): integer;
22 function LumFromArrowPos(p: integer): Double;
23 function ValFromArrowPos(p: Integer): Double;
24 function GetHint(AMode: TBrightnessMode): String;
25 procedure SetMyHint(AMode: TBrightnessMode; AText: String);
26 protected
27 procedure Execute(tbaAction: integer); override;
28 function GetArrowPos: integer; override;
29 function GetGradientColor(AValue: Integer): TColor; override;
30 function GetSelectedValue: integer; override;
31 procedure SetBrightnessMode(AMode: TBrightnessMode); override;
32 procedure SetMaxLum(L: Integer); override;
33 procedure SetMaxVal(V: Integer); override;
34 procedure SetRelLum(L: Double); override;
35 procedure SetRelVal(V: Double); override;
36 procedure SetSelectedColor(c: TColor); override;
37 public
38 constructor Create(AOwner: TComponent); override;
39 published
40 property Hue default 0;
41 property Saturation default 0;
42 property Luminance default 255;
43 property Value default 255;
44 property SelectedColor default clWhite;
45 property LHintFormat: String index bmLuminance read GetHint write SetMyHint;
46 property VHintFormat: String index bmValue read GetHint write SetMyHint;
47 end;
48
49 implementation
50
51 uses
52 mbUtils;
53
54 { TLVColorPicker }
55
56 constructor TLVColorPicker.Create(AOwner: TComponent);
57 begin
58 inherited;
59 case BrightnessMode of
60 bmLuminance : FGradientWidth := FMaxLum + 1;
61 bmValue : FGradientWidth := FMaxVal + 1;
62 end;
63 FGradientHeight := 1;
64 FHue := 0;
65 FSat := 0;
66 FLum := 1;
67 FVal := 1;
68 FHint[bmLuminance] := 'Luminance: %lum (selected)';
69 FHint[bmValue] := 'Value: %value (selected)';
70 end;
71
72 function TLVColorPicker.ArrowPosFromLum(L: Double): integer;
73 var
74 a: integer;
75 begin
76 if Layout = lyHorizontal then
77 begin
78 a := Round((Width - 12) * L);
79 if a > Width - FLimit then a := Width - FLimit;
80 end
81 else
82 begin
83 a := Round((Height - 12) * (1.0 - L));
84 if a > Height - FLimit then a := Height - FLimit;
85 end;
86 if a < 0 then a := 0;
87 Result := a;
88 end;
89
90 function TLVColorPicker.ArrowPosFromVal(V: Double): integer;
91 var
92 a: integer;
93 begin
94 if Layout = lyHorizontal then
95 begin
96 a := Round((Width - 12) * V);
97 if a > Width - FLimit then a := Width - FLimit;
98 end
99 else
100 begin
101 a := Round((Height - 12) * (1.0 - V));
102 if a > Height - FLimit then a := Height - FLimit;
103 end;
104 if a < 0 then a := 0;
105 Result := a;
106 end;
107
108 procedure TLVColorPicker.Execute(tbaAction: integer);
109 var
110 dLum, dVal: Double;
111 begin
112 case BrightnessMode of
113 bmLuminance:
114 begin
115 if FMaxLum = 0 then dLum := 0 else dLum := Increment / FMaxLum;
116 case tbaAction of
117 TBA_Resize:
118 SetRelLum(FLum);
119 TBA_MouseMove:
120 SetRelLum(LumFromArrowPos(FArrowPos));
121 TBA_MouseDown:
122 SetRelLum(LumFromArrowPos(FArrowPos));
123 TBA_MouseUp:
124 SetRelLum(LumFromArrowPos(FArrowPos));
125 TBA_WheelUp:
126 SetRelLum(FLum + dLum);
127 TBA_WheelDown:
128 SetRelLum(FLum - dLum);
129 TBA_VKRight:
130 SetRelLum(FLum + dLum);
131 TBA_VKCtrlRight:
132 SetRelLum(1.0);
133 TBA_VKLeft:
134 SetRelLum(FLum - dLum);
135 TBA_VKCtrlLeft:
136 SetRelLum(0.0);
137 TBA_VKUp:
138 SetRelLum(FLum + dLum);
139 TBA_VKCtrlUp:
140 SetRelLum(1.0);
141 TBA_VKDown:
142 SetRelLum(FLum - dLum);
143 TBA_VKCtrlDown:
144 SetRelLum(0);
145 else
146 inherited;
147 end;
148 end;
149
150 bmValue:
151 begin
152 if FMaxVal = 0 then dVal := 0 else dVal := Increment / FMaxVal;
153 case tbaAction of
154 TBA_Resize:
155 SetRelVal(FVal);
156 TBA_MouseMove:
157 SetRelVal(ValFromArrowPos(FArrowPos));
158 TBA_MouseDown:
159 SetRelVal(ValFromArrowPos(FArrowPos));
160 TBA_MouseUp:
161 SetRelVal(ValFromArrowPos(FArrowPos));
162 TBA_WheelUp:
163 SetRelVal(FVal + dVal);
164 TBA_WheelDown:
165 SetRelVal(FVal - dVal);
166 TBA_VKRight:
167 SetRelval(FVal + dVal);
168 TBA_VKCtrlRight:
169 SetRelVal(1.0);
170 TBA_VKLeft:
171 SetRelval(FVal - dVal);
172 TBA_VKCtrlLeft:
173 SetRelVal(0.0);
174 TBA_VKUp:
175 SetRelVal(FVal + dVal);
176 TBA_VKCtrlUp:
177 SetRelVal(1.0);
178 TBA_VKDown:
179 SetRelval(FVal - dVal);
180 TBA_VKCtrlDown:
181 SetRelVal(0.0);
182 else
183 inherited;
184 end;
185 end;
186 end;
187 end;
188
189 function TLVColorPicker.GetArrowPos: integer;
190 begin
191 case BrightnessMode of
192 bmLuminance:
193 if FMaxLum = 0 then
194 Result := inherited GetArrowPos
195 else
196 Result := ArrowPosFromLum(FLum);
197 bmValue:
198 if FMaxVal = 0 then
199 Result := inherited GetArrowPos
200 else
201 Result := ArrowPosFromVal(FVal);
202 end;
203 end;
204
205 function TLVColorPicker.GetGradientColor(AValue: Integer): TColor;
206 begin
207 Result := HSLVtoColor(FHue, FSat, AValue/FMaxLum, AValue/FMaxVal);
208 end;
209
210 function TLVColorPicker.GetHint(AMode: TBrightnessMode): String;
211 begin
212 Result := FHint[AMode];
213 end;
214
215 function TLVColorPicker.GetSelectedValue: integer;
216 begin
217 case BrightnessMode of
218 bmLuminance : Result := Luminance;
219 bmValue : Result := Value;
220 end;
221 end;
222
223 function TLVColorPicker.LumFromArrowPos(p: integer): Double;
224 var
225 L: Double;
226 begin
227 case Layout of
228 lyHorizontal : L := p / (Width - 12);
229 lyVertical : L := 1.0 - p /(Height - 12);
230 end;
231 Clamp(L, 0, 1.0);
232 Result := L;
233 end;
234
235 procedure TLVColorPicker.SetBrightnessMode(AMode: TBrightnessMode);
236 begin
237 inherited;
238 HintFormat := FHint[AMode];
239 end;
240
241 procedure TLVColorPicker.SetMyHint(AMode: TBrightnessMode; AText: String);
242 begin
243 FHint[AMode] := AText;
244 end;
245
246 procedure TLVColorPicker.SetMaxLum(L: Integer);
247 begin
248 if L = FMaxLum then
249 exit;
250 FMaxLum := L;
251 if BrightnessMode = bmLuminance then begin
252 FGradientWidth := FMaxLum + 1;
253 CreateGradient;
254 Invalidate;
255 DoChange;
256 end;
257 end;
258
259 procedure TLVColorPicker.SetMaxVal(V: Integer);
260 begin
261 if V = FMaxVal then
262 exit;
263 FMaxVal := V;
264 if BrightnessMode = bmValue then begin
265 FGradientWidth := FMaxVal + 1;
266 CreateGradient;
267 Invalidate;
268 DoChange;
269 end;
270 end;
271
272 procedure TLVColorPicker.SetRelLum(L: Double);
273 begin
274 Clamp(L, 0, 1.0);
275 if FLum <> L then
276 begin
277 FLum := L;
278 FArrowPos := ArrowPosFromLum(L);
279 Invalidate;
280 DoChange;
281 end;
282 end;
283
284 procedure TLVColorPicker.SetRelVal(V: Double);
285 begin
286 Clamp(V, 0, 1.0);
287 if FVal <> V then
288 begin
289 FVal := V;
290 FArrowPos := ArrowPosFromVal(V);
291 Invalidate;
292 DoChange;
293 end;
294 end;
295
296 procedure TLVColorPicker.SetSelectedColor(c: TColor);
297 var
298 H: Double = 0;
299 S: Double = 0;
300 L: Double = 0;
301 V: Double = 0;
302 needNewGradient: Boolean;
303 begin
304 if WebSafe then
305 c := GetWebSafe(c);
306 if c = GetSelectedColor then
307 exit;
308
309 ColorToHSLV(c, H, S, L, V);
310 needNewGradient := (H <> FHue) or (S <> FSat);
311 FHue := H;
312 FSat := S;
313 case BrightnessMode of
314 bmLuminance : FLum := L;
315 bmValue : FVal := V;
316 end;
317 if needNewGradient then
318 CreateGradient;
319 Invalidate;
320 DoChange;
321 end;
322
323 function TLVColorPicker.ValFromArrowPos(p: integer): Double;
324 var
325 V: Double;
326 begin
327 case Layout of
328 lyHorizontal : V := p / (Width - 12);
329 lyVertical : V := 1.0 - p /(Height - 12);
330 end;
331 Clamp(V, 0, 1.0);
332 Result := V;
333 end;
334
335 end.