Debian Codegraph @

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

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