Debian Codegraph @

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

raw
1 unit SLColorPicker;
2
3 {$MODE DELPHI}
4
5 interface
6
7 uses
8 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms,
9 mbColorConv, mbColorPickerControl;
10
11 type
12 TSLColorPicker = class(TmbHSLVColorPickerControl)
13 private
14 FHint: array[TBrightnessMode] of string;
15 function GetHint(AMode: TBrightnessMode): String;
16 procedure SetMyHint(AMode: TBrightnessMode; AText: String);
17 protected
18 procedure CorrectCoords(var x, y: integer);
19 procedure CreateWnd; override;
20 procedure DrawMarker(x, y: integer);
21 function GetGradientColor2D(X, Y: Integer): TColor; override;
22 procedure Resize; override;
23 procedure Paint; override;
24 procedure SelectColor(x, y: integer); override;
25 procedure SetBrightnessMode(AMode: TBrightnessMode); override;
26 procedure SetMaxLum(L: Integer); override;
27 procedure SetMaxSat(S: Integer); override;
28 procedure SetMaxVal(V: Integer); override;
29 procedure SetRelLum(L: Double); override;
30 procedure SetRelSat(S: Double); override;
31 procedure SetRelVal(V: Double); override;
32 procedure SetSelectedColor(c: TColor); override;
33 procedure UpdateCoords;
34 public
35 constructor Create(AOwner: TComponent); override;
36 function GetColorAtPoint(x, y: integer): TColor; override;
37 property ColorUnderCursor;
38 published
39 property Hue default 0;
40 property Saturation default 0;
41 property Luminance default 255;
42 property Value default 255;
43 property MaxHue default 360;
44 property MaxSaturation default 255;
45 property MaxLuminance default 255;
46 property MaxValue default 255;
47 property SelectedColor default clWhite;
48 property MarkerStyle default msCircle;
49 property SLHintFormat: String index bmLuminance read GetHint write SetMyHint;
50 property SVHintFormat: String index bmValue read GetHint write SetMyHint;
51 property OnChange;
52 end;
53
54 implementation
55
56 uses
57 HTMLColors, mbUtils;
58
59 { TSLColorPicker }
60
61 constructor TSLColorPicker.Create(AOwner: TComponent);
62 begin
63 inherited;
64 FGradientWidth := FMaxSat + 1; // x --> Saturation
65 case BrightnessMode of
66 bmLuminance : FGradientHeight := FMaxLum + 1; // y --> Luminance
67 bmValue : FGradientHeight := FMaxVal + 1; // y --> value
68 end;
69 SetInitialBounds(0, 0, FGradientWidth, FGradientHeight);
70 FHue := 0;
71 FSat := 1.0;
72 FLum := 1.0;
73 FVal := 1.0;
74 SLHintFormat := 'S: %hslS L: %l' + LineEnding + 'Hex: %hex';
75 SVHintFormat := 'S: %hslS V: %v' + LineEnding + 'Hex: %hex';
76 MarkerStyle := msCircle;
77 end;
78
79 procedure TSLColorPicker.CorrectCoords(var x, y: integer);
80 begin
81 Clamp(x, 0, Width - 1);
82 Clamp(y, 0, Height - 1);
83 end;
84
85 procedure TSLColorPicker.CreateWnd;
86 begin
87 inherited;
88 CreateGradient;
89 UpdateCoords;
90 end;
91
92 procedure TSLColorPicker.DrawMarker(x, y: integer);
93 var
94 c: TColor;
95 begin
96 c := not GetColorAtPoint(x, y); // "not" --> invert color bits
97 InternalDrawMarker(x, y, c);
98 end;
99
100 function TSLColorPicker.GetColorAtPoint(x, y: integer): TColor;
101 var
102 S, LV: Double;
103 begin
104 S := x / (Width - 1);
105 LV := 1.0 - y / (Height - 1);
106 Result := HSLVtoColor(FHue, S, LV, LV);
107 end;
108
109 { This picker has Saturation along the X and Luminance or Value on the Y axis. }
110 function TSLColorPicker.GetGradientColor2D(X, Y: Integer): TColor;
111 begin
112 Result := HSLVtoColor(FHue, x/FMaxSat, 1.0 - y/FMaxLum, 1.0 - y/FMaxVal);
113 end;
114
115 function TSLColorPicker.GetHint(AMode: TBrightnessMode): String;
116 begin
117 Result := FHint[AMode];
118 end;
119
120 procedure TSLColorPicker.Paint;
121 begin
122 Canvas.StretchDraw(ClientRect, FBufferBMP);
123 // UpdateCoords;
124 DrawMarker(mx, my);
125 end;
126
127 procedure TSLColorPicker.Resize;
128 begin
129 inherited;
130 UpdateCoords;
131 end;
132
133 procedure TSLColorPicker.SelectColor(x, y: integer);
134 var
135 S, LV: Double;
136 begin
137 CorrectCoords(x, y);
138 S := x / (Width - 1);
139 LV := 1 - y / (Height - 1);
140
141 case BrightnessMode of
142 bmLuminance:
143 begin
144 if (S = FSat) and (LV = FLum) then
145 exit;
146 FLum := LV;
147 end;
148 bmValue:
149 begin
150 if (S = FSat) and (LV = FVal) then
151 exit;
152 FVal := LV;
153 end;
154 end;
155 FSat := S;
156 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
157
158 Invalidate;
159 UpdateCoords;
160 DoChange;
161 end;
162
163 procedure TSLColorPicker.SetBrightnessMode(AMode: TBrightnessMode);
164 begin
165 inherited;
166 HintFormat := FHint[AMode];
167 end;
168
169 procedure TSLColorPicker.SetMyHint(AMode: TBrightnessMode; AText: String);
170 begin
171 FHint[AMode] := AText;
172 end;
173
174 procedure TSLColorPicker.SetMaxLum(L: Integer);
175 begin
176 if L = FMaxLum then
177 exit;
178 if BrightnessMode = bmLuminance then
179 FGradientHeight := L + 1;
180 inherited;
181 end;
182
183 procedure TSLColorPicker.SetMaxSat(S: Integer);
184 begin
185 if S = FMaxSat then
186 exit;
187 FGradientWidth := S + 1; // inherited will re-create the gradient
188 inherited;
189 end;
190
191 procedure TSLColorPicker.SetMaxVal(V: Integer);
192 begin
193 if V = FMaxVal then
194 exit;
195 if BrightnessMode = bmValue then
196 FGradientHeight := V + 1;
197 inherited;
198 end;
199
200 procedure TSLColorPicker.SetRelLum(L: Double);
201 begin
202 Clamp(L, 0.0, 1.0);
203 if FLum <> L then
204 begin
205 FLum := L;
206 if BrightnessMode = bmLuminance then
207 begin
208 FSelected := HSLtoColor(FHue, FSat, FLum);
209 UpdateCoords;
210 Invalidate;
211 end;
212 DoChange;
213 end;
214 end;
215
216 procedure TSLColorPicker.SetRelSat(S: Double);
217 begin
218 Clamp(S, 0.0, 1.0);
219 if FSat <> S then
220 begin
221 FSat := S;
222 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
223 UpdateCoords;
224 Invalidate;
225 DoChange;
226 end;
227 end;
228
229 procedure TSLColorPicker.SetSelectedColor(c: TColor);
230 var
231 H: Double = 0;
232 S: Double = 0;
233 L: Double = 0;
234 V: Double = 0;
235 needNewGradient: Boolean;
236 begin
237 if WebSafe then
238 c := GetWebSafe(c);
239 if c = GetSelectedColor then
240 exit;
241
242 ColorToHSLV(c, H, S, L, V);
243 needNewGradient := (FHue <> H);
244 FHue := H;
245 FSat := S;
246 case BrightnessMode of
247 bmLuminance : FLum := L;
248 bmValue : FVal := V;
249 end;
250 FSelected := c;
251 UpdateCoords;
252 if needNewGradient then
253 CreateGradient;
254 Invalidate;
255 DoChange;
256 end;
257
258 procedure TSLColorPicker.SetRelVal(V: Double);
259 begin
260 Clamp(V, 0.0, 1.0);
261 if FVal <> V then
262 begin
263 FVal := V;
264 if BrightnessMode = bmValue then
265 begin
266 FSelected := HSVtoColor(FHue, FSat, FVal);
267 UpdateCoords;
268 Invalidate;
269 end;
270 DoChange;
271 end;
272 end;
273
274 procedure TSLColorPicker.UpdateCoords;
275 begin
276 mx := round(FSat * (Width - 1));
277 case BrightnessMode of
278 bmLuminance : my := round((1.0 - FLum) * (Height - 1));
279 bmValue : my := round((1.0 - FVal) * (Height - 1));
280 end;
281 end;
282
283
284 end.