Debian Codegraph @

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

raw
1 unit CIELColorPicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType, SysUtils, Classes, Controls, Graphics, Math, Forms,
11 HTMLColors, RGBCIEUtils, mbColorPickerControl;
12
13 type
14 TCIELColorPicker = class(TmbColorPickerControl)
15 private
16 FL, FA, FB: integer;
17 procedure SetLValue(l: integer);
18 procedure SetAValue(a: integer);
19 procedure SetBValue(b: integer);
20 protected
21 procedure CorrectCoords(var x, y: integer);
22 procedure CreateWnd; override;
23 procedure DrawMarker(x, y: integer);
24 function GetGradientColor2D(x, y: Integer): TColor; override;
25 procedure KeyDown(var Key: Word; Shift: TShiftState); override;
26 procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
27 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
28 procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
29 procedure Paint; override;
30 procedure Resize; override;
31 procedure SelectColor(X, Y: Integer);
32 procedure SetSelectedColor(c: TColor); override;
33 public
34 constructor Create(AOwner: TComponent); override;
35 function GetColorAtPoint(X, Y: Integer): TColor; override;
36 published
37 property AValue: integer read FA write SetAValue default -128;
38 property BValue: integer read FB write SetBValue default 127;
39 property LValue: integer read FL write SetLValue default 100;
40 property MarkerStyle default msCircle;
41 property SelectedColor default clAqua;
42 property OnChange;
43 end;
44
45
46 implementation
47
48 uses
49 mbUtils;
50
51 {TCIELColorPicker}
52
53 constructor TCIELColorPicker.Create(AOwner: TComponent);
54 begin
55 inherited;
56 FGradientWidth := 256;
57 FGradientHeight := 256;
58 SetInitialBounds(0, 0, 256, 256);
59 HintFormat := 'A: %cieA B: %cieB'#13'Hex: %hex';
60 FSelected := clAqua;
61 FL := 100;
62 FA := -128;
63 FB := 127;
64 MarkerStyle := msCircle;
65 end;
66
67 procedure TCIELColorPicker.CorrectCoords(var x, y: integer);
68 begin
69 Clamp(x, 0, Width - 1);
70 clamp(y, 0, Height - 1);
71 end;
72
73 procedure TCIELColorPicker.CreateWnd;
74 begin
75 inherited;
76 CreateGradient;
77 end;
78
79 procedure TCIELColorPicker.DrawMarker(x, y: integer);
80 var
81 c: TColor;
82 begin
83 CorrectCoords(x, y);
84 FL := Round(GetCIELValue(FSelected));
85 FA := Round(GetCIEAValue(FSelected));
86 FB := Round(GetCIEBValue(FSelected));
87 if Focused or (csDesigning in ComponentState) then
88 c := clBlack
89 else
90 c := clWhite;
91 InternalDrawMarker(x, y, c);
92 end;
93
94 function TCIELColorPicker.GetColorAtPoint(x, y: Integer): TColor;
95 var
96 a, b: Double;
97 begin
98 a := (y / (Height - 1) - 0.5) * 255;
99 b := (x / (Width - 1) - 0.5) * 255;
100 Result := LabToRGB(FL, a, b);
101 end;
102 {
103 var
104 a, b: Integer;
105 begin
106 a := round(255 * (y / (Height - 1))) - 128;
107 b := round(255 * (x / (Width - 1))) - 128;
108 Result := LabToRGB(FL, a, b);
109 end;
110 }
111
112 { Original code: for A ... for B ---> LabToRGB(FL, A - 128, B - 128) }
113 function TCIELColorPicker.GetGradientColor2D(x, y: Integer): TColor;
114 begin
115 Result := LabToRGB(FL, y - 128, x - 128);
116 end;
117
118 procedure TCIELColorPicker.KeyDown(var Key: Word; Shift: TShiftState);
119 var
120 eraseKey: Boolean;
121 delta: Integer;
122 begin
123 erasekey := true;
124 delta := IfThen(ssCtrl in Shift, 10, 1);
125
126 case Key of
127 VK_LEFT : SelectColor(mx - delta, my);
128 VK_Right : SelectColor(mx + delta, my);
129 VK_UP : SelectColor(mx, my - delta);
130 VK_DOWN : SelectColor(mx, my + delta);
131 else eraseKey := false;
132 end;
133
134 if eraseKey then
135 Key := 0;
136
137 inherited;
138 end;
139
140 procedure TCIELColorPicker.MouseDown(Button: TMouseButton; Shift: TShiftState;
141 X, Y: Integer);
142 begin
143 inherited;
144 if Button = mbLeft then
145 SelectColor(X, Y);
146 SetFocus;
147 end;
148
149 procedure TCIELColorPicker.MouseMove(Shift: TShiftState; X, Y: Integer);
150 begin
151 inherited;
152 if ssLeft in Shift then
153 SelectColor(X, Y);
154 end;
155
156 procedure TCIELColorPicker.MouseUp(Button: TMouseButton; Shift: TShiftState;
157 X, Y: Integer);
158 begin
159 inherited;
160 if Button = mbLeft then
161 SelectColor(X, Y);
162 end;
163
164 procedure TCIELColorPicker.Paint;
165 begin
166 Canvas.StretchDraw(ClientRect, FBufferBmp);
167 CorrectCoords(mx, my);
168 DrawMarker(mx, my);
169 end;
170
171 procedure TCIELColorPicker.Resize;
172 begin
173 mx := Round((FA + 128) * Width / 255);
174 my := Round((255 - (FB + 128)) * Height / 255);
175 inherited;
176 end;
177
178 procedure TCIELColorPicker.SelectColor(x, y: Integer);
179 var
180 c: TColor;
181 l, a, b: Integer;
182 needNewGradient: Boolean;
183 begin
184 CorrectCoords(x, y);
185 c := GetColorAtPoint(x, y);
186 if WebSafe then
187 c := GetWebSafe(c);
188 if c = FSelected then
189 exit;
190
191 mx := x;
192 my := y;
193 l := Round(GetCIELValue(c));
194 a := Round(GetCIEAValue(c));
195 b := Round(GetCIEBValue(c));
196 needNewGradient := l <> FL;
197 FSelected := c;
198 FL := l;
199 FA := a;
200 FB := b;
201 if needNewGradient then
202 CreateGradient;
203 Invalidate;
204 DoChange;
205 end;
206
207 procedure TCIELColorPicker.SetAValue(a: integer);
208 begin
209 Clamp(A, -128, 127);
210 FA := a;
211 SetSelectedColor(LabToRGB(FL, FA, FB));
212 end;
213
214 procedure TCIELColorPicker.SetBValue(b: integer);
215 begin
216 Clamp(b, -128, 127);
217 FB := b;
218 SetSelectedColor(LabToRGB(FL, FA, FB));
219 end;
220
221 procedure TCIELColorPicker.SetLValue(l: integer);
222 begin
223 Clamp(L, 0, 100);
224 FL := L;
225 SetSelectedColor(LabToRGB(FL, FA, FB));
226 end;
227
228 procedure TCIELColorPicker.SetSelectedColor(c: TColor);
229 var
230 L, a, b: Integer;
231 needNewGradient: Boolean;
232 begin
233 if WebSafe then
234 c := GetWebSafe(c);
235 if c = FSelected then
236 exit;
237
238 L := Round(GetCIELValue(c));
239 a := Round(GetCIEAValue(c));
240 b := Round(GetCIEBValue(c));
241 needNewGradient := L <> FL;
242 FL := L;
243 FA := a;
244 FB := b;
245 FSelected := c;
246 mx := Round((FA + 128) * Width / 255);
247 my := Round((255 - (FB + 128)) * Height / 255);
248 if needNewGradient then
249 CreateGradient;
250 Invalidate;
251 DoChange;
252 end;
253
254 end.