Debian Codegraph @

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

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