Debian Codegraph @

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

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