Debian Codegraph @

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

raw
1 unit BColorPicker;
2
3 {$IFDEF FPC}
4 {$MODE DELPHI}
5 {$ENDIF}
6
7 interface
8
9 uses
10 LCLIntf, LCLType,
11 SysUtils, Classes, Controls, Graphics, Forms,
12 HTMLColors, mbTrackBarPicker;
13
14 type
15
16 { TBColorPicker }
17
18 TBColorPicker = class(TmbTrackBarPicker)
19 private
20 FRed, FGreen, FBlue: integer;
21 function ArrowPosFromBlue(b: integer): integer;
22 function BlueFromArrowPos(p: integer): integer;
23 procedure SetBlue(b: integer);
24 procedure SetGreen(g: integer);
25 procedure SetRed(r: integer);
26 protected
27 procedure Execute(tbaAction: integer); override;
28 function GetArrowPos: integer; override;
29 function GetGradientColor(AValue: Integer): TColor; override;
30 function GetSelectedColor: TColor; override;
31 function GetSelectedValue: integer; override;
32 procedure SetSelectedColor(c: TColor); override;
33 public
34 constructor Create(AOwner: TComponent); override;
35 published
36 property Blue: integer read FBlue write SetBlue default 255;
37 property Green: integer read FGreen write SetGreen default 128;
38 property Red: integer read FRed write SetRed default 128;
39 property SelectedColor default clRed;
40 property Layout default lyVertical;
41 property HintFormat;
42 end;
43
44
45 implementation
46
47 uses
48 mbUtils;
49
50
51 {TBColorPicker}
52
53 constructor TBColorPicker.Create(AOwner: TComponent);
54 begin
55 inherited;
56 FGradientWidth := 256;
57 FGradientHeight := 1;
58 FRed := 128;
59 FGreen := 128;
60 SetBlue(255);
61 Layout := lyVertical;
62 HintFormat := 'Blue: %value (selected)';
63 end;
64
65 function TBColorPicker.ArrowPosFromBlue(b: integer): integer;
66 var
67 a: integer;
68 begin
69 if Layout = lyHorizontal then
70 begin
71 a := Round((Width - 12) * b / 255);
72 if a > Width - FLimit then a := Width - FLimit;
73 end
74 else
75 begin
76 a := Round((Height - 12) * (255 - b) / 255 );
77 if a > Height - FLimit then a := Height - FLimit;
78 end;
79 if a < 0 then a := 0;
80 Result := a;
81 end;
82
83 function TBColorPicker.BlueFromArrowPos(p: integer): integer;
84 var
85 b: integer;
86 begin
87 case Layout of
88 lyHorizontal:
89 b := Round(p * 255 / (Width - 12));
90 lyVertical:
91 b := Round(255 - p * 255 / (Height - 12));
92 end;
93 Clamp(b, 0, 255);
94 Result := b;
95 end;
96
97 procedure TBColorPicker.Execute(tbaAction: integer);
98 begin
99 case tbaAction of
100 TBA_Resize:
101 SetBlue(FBlue);
102 TBA_MouseMove:
103 SetBlue(BlueFromArrowPos(FArrowPos));
104 TBA_MouseDown:
105 SetBlue(BlueFromArrowPos(FArrowPos));
106 TBA_MouseUp:
107 SetBlue(BlueFromArrowPos(FArrowPos));
108 TBA_WheelUp:
109 SetBlue(FBlue + Increment);
110 TBA_WheelDown:
111 SetBlue(FBlue - Increment);
112 TBA_VKRight:
113 SetBlue(FBlue + Increment);
114 TBA_VKCtrlRight:
115 SetBlue(255);
116 TBA_VKLeft:
117 SetBlue(FBlue - Increment);
118 TBA_VKCtrlLeft:
119 SetBlue(0);
120 TBA_VKUp:
121 SetBlue(FBlue + Increment);
122 TBA_VKCtrlUp:
123 SetBlue(255);
124 TBA_VKDown:
125 SetBlue(FBlue - Increment);
126 TBA_VKCtrlDown:
127 SetBlue(0);
128 else
129 inherited;
130 end;
131 end;
132
133 function TBColorPicker.GetArrowPos: integer;
134 begin
135 Result := ArrowPosFromBlue(FBlue);
136 end;
137
138 // Note: AValue is restricted to the range 0..255 by the size of the trackbar.
139 function TBColorPicker.GetGradientColor(AValue: Integer): TColor;
140 begin
141 Result := RGB(FRed, FGreen, AValue);
142 end;
143
144 function TBColorPicker.GetSelectedColor: TColor;
145 begin
146 Result := RGB(FRed, FGreen, FBlue);
147 if WebSafe then
148 Result := GetWebSafe(Result);
149 end;
150
151 function TBColorPicker.GetSelectedValue: integer;
152 begin
153 Result := FBlue;
154 end;
155
156 procedure TBColorPicker.SetBlue(b: integer);
157 begin
158 Clamp(b, 0, 255);
159 if FBlue <> b then
160 begin
161 FBlue := b;
162 FArrowPos := ArrowPosFromBlue(b);
163 Invalidate;
164 DoChange;
165 end;
166 end;
167
168 procedure TBColorPicker.SetGreen(g: integer);
169 begin
170 Clamp(g, 0, 255);
171 if FGreen <> g then
172 begin
173 FGreen := g;
174 CreateGradient;
175 Invalidate;
176 DoChange;
177 end;
178 end;
179
180 procedure TBColorPicker.SetRed(r: integer);
181 begin
182 Clamp(r, 0, 255);
183 if FRed <> r then
184 begin
185 FRed := r;
186 CreateGradient;
187 Invalidate;
188 DoChange;
189 end;
190 end;
191
192 procedure TBColorPicker.SetSelectedColor(c: TColor);
193 var
194 r, g, b: Integer;
195 newGradient: Boolean;
196 begin
197 if WebSafe then
198 c := GetWebSafe(c);
199 if c = GetSelectedColor then
200 exit;
201
202 r := GetRValue(c);
203 g := GetGValue(c);
204 b := GetBValue(c);
205 newGradient := (r <> FRed) or (g <> FGreen);
206 FGreen := g;
207 FBlue := b;
208 FRed := r;
209 if newGradient then
210 CreateGradient;
211 Invalidate;
212 DoChange;
213 end;
214
215 end.