Debian Codegraph @

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

raw
1 unit HSColorPicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Forms,
11 HTMLColors, mbColorConv, mbColorPickerControl;
12
13 type
14
15 { THSColorPicker }
16
17 THSColorPicker = class(TmbHSLVColorPickerControl)
18 private
19 FLumDisp, FValDisp: Double; // Lum and Value used for display
20 protected
21 procedure CreateWnd; override;
22 procedure DrawMarker(x, y: integer);
23 function GetGradientColor2D(x, y: Integer): TColor; override;
24 function GetSelectedColor: TColor; override;
25 procedure Paint; override;
26 function PredictColor: TColor;
27 procedure Resize; override;
28 procedure SelectColor(x, y: Integer); override;
29 procedure SetMaxHue(H: Integer); override;
30 procedure SetMaxSat(S: Integer); override;
31 procedure SetRelHue(H: Double); override;
32 procedure SetRelSat(S: Double); override;
33 procedure SetSelectedColor(c: TColor); override;
34 procedure UpdateCoords;
35 public
36 constructor Create(AOwner: TComponent); override;
37 function GetColorAtPoint(x, y: Integer): TColor; override;
38 published
39 property SelectedColor default clRed;
40 property Hue default 0;
41 property Saturation default 255;
42 property Luminance default 127;
43 property Value default 255;
44 property MaxHue default 360;
45 property MaxSaturation default 255;
46 property MaxLuminance default 255;
47 property MaxValue default 255;
48 property MarkerStyle default msCross;
49 property OnChange;
50 end;
51
52
53 implementation
54
55 uses
56 Math, mbUtils, PalUtils;
57
58 { THSColorPicker }
59
60 constructor THSColorPicker.Create(AOwner: TComponent);
61 begin
62 inherited;
63 FGradientWidth := FMaxHue; // We want to skip the point at 360° --> no +1
64 FGradientHeight := FMaxSat + 1;
65 SetInitialBounds(0, 0, FGradientWidth, FGradientHeight);
66 FHue := 0;
67 FSat := 1.0;
68 FLum := 0.5;
69 FLumDisp := 0.5;
70 FVal := 1.0;
71 FValDisp := 1.0;
72 FSelected := clRed;
73 CreateGradient;
74 HintFormat := 'H: %h S: %hslS'#13'Hex: %hex';
75 MarkerStyle := msCross;
76 end;
77
78 procedure THSColorPicker.CreateWnd;
79 begin
80 inherited;
81 CreateGradient;
82 end;
83
84 procedure THSColorPicker.DrawMarker(x, y: integer);
85 var
86 c: TColor;
87 dummy: Double = 0;
88 begin
89 CorrectCoords(x, y);
90 ColorToHSLV(FSelected, FHue, FSat, dummy, dummy);
91 if Focused or (csDesigning in ComponentState) then
92 c := clBlack
93 else
94 case BrightnessMode of
95 bmLuminance: c := clWhite;
96 bmValue : c := clGray;
97 end;
98 InternalDrawMarker(x, y, c);
99 end;
100
101 function THSColorPicker.GetColorAtPoint(x, y: Integer): TColor;
102 var
103 H, S: Double;
104 begin
105 if InRange(x, 0, Width - 1) and InRange(y, 0, Height - 1) then
106 begin
107 H := x / Width; // Width = FMaxHue
108 S := 1 - y / (Height - 1);
109 Result := HSLVtoColor(H, S, FLum, FVal);
110 end else
111 Result := clNone;
112 end;
113
114 function THSColorPicker.GetGradientColor2D(x, y: Integer): TColor;
115 var
116 H, S: Double;
117 begin
118 H := x / FMaxHue;
119 S := 1 - y / FMaxSat;
120 Result := HSLVtoColor(H, S, FLumDisp, FValDisp);
121 end;
122
123 function THSColorPicker.GetSelectedColor: TColor;
124 begin
125 Result := HSLVtoColor(FHue, FSat, FLum, FVal);
126 end;
127
128 procedure THSColorPicker.Paint;
129 begin
130 Canvas.StretchDraw(ClientRect, FBufferBmp);
131 DrawMarker(mx, my);
132 end;
133
134 function THSColorPicker.PredictColor: TColor;
135 begin
136 Result := GetColorUnderCursor;
137 end;
138
139 procedure THSColorPicker.Resize;
140 begin
141 SetSelectedColor(FSelected);
142 inherited;
143 end;
144
145 procedure THSColorPicker.SelectColor(x, y: Integer);
146 var
147 H: Double = 0;
148 S: Double = 0;
149 L: Double = 0;
150 V: Double = 0;
151 c: TColor;
152 begin
153 CorrectCoords(x, y);
154 mx := x;
155 my := y;
156 c := GetColorAtPoint(x, y);
157 if WebSafe then c := GetWebSafe(c);
158
159 ColorToHSLV(c, H, S, L, V);
160 {
161 if (H = FHue) and (S = FSat) then
162 exit;
163 }
164 FHue := H;
165 FSat := S;
166 FSelected := HSLVtoColor(FHue, FSat, FLum, FVal);
167
168 Invalidate;
169 DoChange;
170 end;
171
172 procedure THSColorPicker.SetMaxHue(H: Integer);
173 begin
174 if H = FMaxHue then
175 exit;
176 FGradientWidth := H + 1;
177 inherited;
178 end;
179
180 procedure THSColorPicker.SetMaxSat(S: Integer);
181 begin
182 if S = FMaxSat then
183 exit;
184 FGradientHeight := S + 1;
185 inherited;
186 end;
187
188 procedure THSColorPicker.SetRelHue(H: Double);
189 begin
190 Clamp(H, 0, 1 - 1/FMaxHue); // Don't use H=360°
191 if H = FHue then
192 exit;
193
194 FHue := H;
195 FSelected := GetSelectedColor;
196 UpdateCoords;
197 Invalidate;
198 DoChange;
199 end;
200
201 procedure THSColorPicker.SetRelSat(S: Double);
202 begin
203 Clamp(S, 0.0, 1.0);
204 if S = FSat then
205 exit;
206
207 FSat := S;
208 FSelected := GetSelectedColor;
209 UpdateCoords;
210 Invalidate;
211 DoChange;
212 end;
213
214 // NOTE: In the picker display only the hue and the saturation of the input
215 // color are used, the luminance is replaced by the preset value of the picker.
216 // --> The selected color in the üicker display in general is different from the
217 // input color.
218 procedure THSColorPicker.SetSelectedColor(c: TColor);
219 var
220 H: Double = 0;
221 S: Double = 0;
222 L: Double = 0;
223 V: Double = 0;
224 begin
225 if WebSafe then
226 c := GetWebSafe(c);
227
228 ColorToHSLV(c, H, S, L, V);
229 if (H = FHue) and (S = FSat) then
230 exit;
231
232 FSelected := c;
233 FHue := H;
234 FSat := S;
235 case BrightnessMode of
236 bmLuminance : FLum := L;
237 bmValue : FVal := V;
238 end;
239
240 UpdateCoords;
241 Invalidate;
242 DoChange;
243 end;
244
245 procedure THSColorPicker.UpdateCoords;
246 begin
247 mx := Round(FHue * Width);
248 my := Round((1.0 - FSat) * Height);
249 CorrectCoords(mx, my);
250 end;
251
252 end.