Debian Codegraph @

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

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