Debian Codegraph @

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

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