Debian Codegraph @

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

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