API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CPSliderColorPicker.j
Go to the documentation of this file.
1 /*
2  * CPApplication.j
3  * AppKit
4  *
5  * Created by Ross Boucher.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 /*
26  @ignore
27 */
28 @implementation CPSliderColorPicker : CPColorPicker
29 {
30  CPView _contentView;
31 
32  CPSlider _redSlider;
33  CPSlider _greenSlider;
34  CPSlider _blueSlider;
35  CPSlider _hueSlider;
36  CPSlider _saturationSlider;
37  CPSlider _brightnessSlider;
38 
39  CPTextField _rgbLabel;
40  CPTextField _hsbLabel;
41  CPTextField _redLabel;
42  CPTextField _greenLabel;
43  CPTextField _blueLabel;
44  CPTextField _hueLabel;
45  CPTextField _saturationLabel;
46  CPTextField _brightnessLabel;
47  CPTextField _hexLabel;
48  CPTextField _hexValue;
49 
50  CPTextField _hexValue;
51  CPTextField _redValue;
52  CPTextField _greenValue;
53  CPTextField _blueValue;
54  CPTextField _hueValue;
55  CPTextField _saturationValue;
56  CPTextField _brightnessValue;
57 }
58 
59 - (id)initWithPickerMask:(int)mask colorPanel:(CPColorPanel)owningColorPanel
60 {
61  return [super initWithPickerMask:mask colorPanel:owningColorPanel];
62 }
63 
64 - (id)initView
65 {
66  aFrame = CGRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight);
67 
68  _contentView = [[CPView alloc] initWithFrame:aFrame];
69  [_contentView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
70 
71  _rgbLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 10, 100, 20)];
72  [_rgbLabel setStringValue:"Red, Green, Blue"];
73  [_rgbLabel setTextColor:[CPColor blackColor]];
74 
75  _redLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 37, 15, 20)];
76  [_redLabel setStringValue:"R"];
77  [_redLabel setTextColor:[CPColor blackColor]];
78 
79  _redSlider = [[CPSlider alloc] initWithFrame:CGRectMake(15, 35, aFrame.size.width - 70, 20)];
80  [_redSlider setMaxValue:1.0];
81  [_redSlider setMinValue:0.0];
82  [_redSlider setTarget:self];
83  [_redSlider setAction:@selector(sliderChanged:)];
84  [_redSlider setAutoresizingMask:CPViewWidthSizable];
85 
86  // red value input box
87  _redValue = [[CPTextField alloc] initWithFrame:CGRectMake(aFrame.size.width - 45, 30, 45, 28)];
88  [_redValue setAutoresizingMask:CPViewMinXMargin];
89  [_redValue setEditable:YES];
90  [_redValue setBezeled:YES];
91  [_redValue setDelegate:self];
92  [_contentView addSubview:_redValue];
93 
94  _greenLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 63, 15, 20)];
95  [_greenLabel setStringValue:"G"];
96  [_greenLabel setTextColor:[CPColor blackColor]];
97 
98  _greenSlider = [[CPSlider alloc] initWithFrame:CGRectMake(15, 61, aFrame.size.width - 70, 20)];
99  [_greenSlider setMaxValue:1.0];
100  [_greenSlider setMinValue:0.0];
101  [_greenSlider setTarget:self];
102  [_greenSlider setAction:@selector(sliderChanged:)];
103  [_greenSlider setAutoresizingMask:CPViewWidthSizable];
104 
105  // green value input box
106  _greenValue = [[CPTextField alloc] initWithFrame:CGRectMake(aFrame.size.width - 45, 56, 45, 28)];
107  [_greenValue setAutoresizingMask:CPViewMinXMargin];
108  [_greenValue setEditable:YES];
109  [_greenValue setBezeled:YES];
110  [_greenValue setDelegate:self];
111  [_contentView addSubview:_greenValue];
112 
113  _blueLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 90, 15, 20)];
114  [_blueLabel setStringValue:"B"];
115  [_blueLabel setTextColor:[CPColor blackColor]];
116 
117  _blueSlider = [[CPSlider alloc] initWithFrame:CGRectMake(15, 87, aFrame.size.width - 70, 20)];
118  [_blueSlider setMaxValue:1.0];
119  [_blueSlider setMinValue:0.0];
120  [_blueSlider setTarget:self];
121  [_blueSlider setAction:@selector(sliderChanged:)];
122  [_blueSlider setAutoresizingMask:CPViewWidthSizable];
123 
124  // blue value input box
125  _blueValue = [[CPTextField alloc] initWithFrame:CGRectMake(aFrame.size.width - 45, 82, 45, 28)];
126  [_blueValue setAutoresizingMask:CPViewMinXMargin];
127  [_blueValue setEditable:YES];
128  [_blueValue setBezeled:YES];
129  [_blueValue setDelegate:self];
130  [_contentView addSubview:_blueValue];
131 
132  _hsbLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 120, 190, 20)];
133  [_hsbLabel setStringValue:"Hue, Saturation, Brightness"];
134  [_hsbLabel setTextColor:[CPColor blackColor]];
135 
136  _hueLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 145, 15, 20)];
137  [_hueLabel setStringValue:"H"];
138  [_hueLabel setTextColor:[CPColor blackColor]];
139 
140  _hueSlider = [[CPSlider alloc] initWithFrame:CGRectMake(15, 143, aFrame.size.width - 70, 20)];
141  [_hueSlider setMaxValue:359.0];
142  [_hueSlider setMinValue:0.0];
143  [_hueSlider setTarget:self];
144  [_hueSlider setAction:@selector(sliderChanged:)];
145  [_hueSlider setAutoresizingMask:CPViewWidthSizable];
146 
147  // hue value input box
148  _hueValue = [[CPTextField alloc] initWithFrame:CGRectMake(aFrame.size.width - 45, 138, 45, 28)];
149  [_hueValue setAutoresizingMask:CPViewMinXMargin];
150  [_hueValue setEditable:YES];
151  [_hueValue setBezeled:YES];
152  [_hueValue setDelegate:self];
153  [_contentView addSubview:_hueValue];
154 
155  _saturationLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 170, 15, 20)];
156  [_saturationLabel setStringValue:"S"];
157  [_saturationLabel setTextColor:[CPColor blackColor]];
158 
159  _saturationSlider = [[CPSlider alloc] initWithFrame:CGRectMake(15, 168, aFrame.size.width - 70, 20)];
160  [_saturationSlider setMaxValue:100.0];
161  [_saturationSlider setMinValue:0.0];
162  [_saturationSlider setTarget:self];
163  [_saturationSlider setAction:@selector(sliderChanged:)];
164  [_saturationSlider setAutoresizingMask:CPViewWidthSizable];
165 
166  // saturation value input box
167  _saturationValue = [[CPTextField alloc] initWithFrame:CGRectMake(aFrame.size.width - 45, 164, 45, 28)];
168  [_saturationValue setAutoresizingMask:CPViewMinXMargin];
169  [_saturationValue setEditable:YES];
170  [_saturationValue setBezeled:YES];
171  [_saturationValue setDelegate:self];
172  [_contentView addSubview:_saturationValue];
173 
174  _brightnessLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 196, 15, 20)];
175  [_brightnessLabel setStringValue:"B"];
176  [_brightnessLabel setTextColor:[CPColor blackColor]];
177 
178  _brightnessSlider = [[CPSlider alloc] initWithFrame:CGRectMake(15, 194, aFrame.size.width - 70, 20)];
179  [_brightnessSlider setMaxValue:100.0];
180  [_brightnessSlider setMinValue:0.0];
181  [_brightnessSlider setTarget:self];
182  [_brightnessSlider setAction:@selector(sliderChanged:)];
183  [_brightnessSlider setAutoresizingMask:CPViewWidthSizable];
184 
185  // brightness value input box
186  _brightnessValue = [[CPTextField alloc] initWithFrame:CGRectMake(aFrame.size.width - 45, 190, 45, 28)];
187  [_brightnessValue setAutoresizingMask:CPViewMinXMargin];
188  [_brightnessValue setEditable:YES];
189  [_brightnessValue setBezeled:YES];
190  [_brightnessValue setDelegate:self];
191  [_contentView addSubview:_brightnessValue];
192 
193  _hexLabel = [[CPTextField alloc] initWithFrame:CGRectMake(0, 230, 30, 20)];
194  [_hexLabel setStringValue:"Hex"];
195  [_hexLabel setTextColor:[CPColor blackColor]];
196 
197  //hex input box
198  _hexValue = [[CPTextField alloc] initWithFrame:CGRectMake(32, 225, 80, 28)];
199  [_hexValue setEditable:YES];
200  [_hexValue setBezeled:YES];
201  [_hexValue setDelegate:self];
202  [_contentView addSubview:_hexValue];
203 
204  [_contentView addSubview:_rgbLabel];
205  [_contentView addSubview:_redLabel];
206  [_contentView addSubview:_greenLabel];
207  [_contentView addSubview:_blueLabel];
208  [_contentView addSubview:_redSlider];
209  [_contentView addSubview:_greenSlider];
210  [_contentView addSubview:_blueSlider];
211 
212  [_contentView addSubview:_hsbLabel];
213  [_contentView addSubview:_hueLabel];
214  [_contentView addSubview:_saturationLabel];
215  [_contentView addSubview:_brightnessLabel];
216  [_contentView addSubview:_hueSlider];
217  [_contentView addSubview:_saturationSlider];
218  [_contentView addSubview:_brightnessSlider];
219 
220  [_contentView addSubview:_hexLabel];
221 }
222 
223 - (CPView)provideNewView:(BOOL)initialRequest
224 {
225  if (initialRequest)
226  [self initView];
227 
228  return _contentView;
229 }
230 
231 - (int)currentMode
232 {
234 }
235 
236 - (BOOL)supportsMode:(int)mode
237 {
238  return (mode == CPSliderColorPickerMode) ? YES : NO;
239 }
240 
241 - (void)sliderChanged:(id)sender
242 {
243  var newColor,
244  colorPanel = [self colorPanel],
245  alpha = [colorPanel opacity];
246 
247  switch (sender)
248  {
249  case _hueSlider:
250  case _saturationSlider:
251  case _brightnessSlider: newColor = [CPColor colorWithHue:[_hueSlider floatValue]
252  saturation:[_saturationSlider floatValue]
253  brightness:[_brightnessSlider floatValue]
254  alpha:alpha];
255 
256  [self updateRGBSliders:newColor];
257  break;
258 
259  case _redSlider:
260  case _greenSlider:
261  case _blueSlider: newColor = [CPColor colorWithCalibratedRed:[_redSlider floatValue]
262  green:[_greenSlider floatValue]
263  blue:[_blueSlider floatValue]
264  alpha:alpha];
265 
266  [self updateHSBSliders:newColor];
267  break;
268  }
269 
270  [self updateLabels];
271  [self updateHex:newColor];
272  [colorPanel setColor:newColor];
273 }
274 
275 - (void)setColor:(CPColor)aColor
276 {
277  [self updateRGBSliders:aColor];
278  [self updateHSBSliders:aColor];
279  [self updateHex:aColor];
280  [self updateLabels];
281 }
282 
283 - (void)updateHSBSliders:(CPColor)aColor
284 {
285  var hsb = [aColor hsbComponents];
286 
287  [_hueSlider setFloatValue:hsb[0]];
288  [_saturationSlider setFloatValue:hsb[1]];
289  [_brightnessSlider setFloatValue:hsb[2]];
290 }
291 
292 - (void)updateHex:(CPColor)aColor
293 {
294  [_hexValue setStringValue:[aColor hexString]];
295 }
296 
297 - (void)updateRGBSliders:(CPColor)aColor
298 {
299  var rgb = [aColor components];
300 
301  [_redSlider setFloatValue:rgb[0]];
302  [_greenSlider setFloatValue:rgb[1]];
303  [_blueSlider setFloatValue:rgb[2]];
304 }
305 
306 - (void)updateLabels
307 {
308  [_hueValue setStringValue:ROUND([_hueSlider floatValue])];
309  [_saturationValue setStringValue:ROUND([_saturationSlider floatValue])];
310  [_brightnessValue setStringValue:ROUND([_brightnessSlider floatValue])];
311 
312  [_redValue setStringValue:ROUND([_redSlider floatValue] * 255)];
313  [_greenValue setStringValue:ROUND([_greenSlider floatValue] * 255)];
314  [_blueValue setStringValue:ROUND([_blueSlider floatValue] * 255)];
315 }
316 
317 - (CPImage)provideNewButtonImage
318 {
319  return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"slider_button.png"] size:CGSizeMake(32, 32)];
320 }
321 
322 - (CPImage)provideNewAlternateButtonImage
323 {
324  return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"slider_button_h.png"] size:CGSizeMake(32, 32)];
325 }
326 
327 - (void)controlTextDidEndEditing:(CPNotification)aNotification
328 {
329  var field = [aNotification object],
330  value = [[field stringValue] stringByTrimmingWhitespace];
331 
332  if (field === _hexValue)
333  {
334  var newColor = [CPColor colorWithHexString:value];
335 
336  if (newColor)
337  {
338  [self setColor:newColor];
339  [[self colorPanel] setColor:newColor];
340  }
341  }
342  else
343  {
344  switch (field)
345  {
346  case _redValue: [_redSlider setFloatValue:MAX(MIN(ROUND(value), 255) / 255.0, 0)];
347  [self sliderChanged:_redSlider];
348  break;
349 
350  case _greenValue: [_greenSlider setFloatValue:MAX(MIN(ROUND(value), 255) / 255.0, 0)];
351  [self sliderChanged:_greenSlider];
352  break;
353 
354  case _blueValue: [_blueSlider setFloatValue:MAX(MIN(ROUND(value), 255) / 255.0, 0)];
355  [self sliderChanged:_blueSlider];
356  break;
357 
358  case _hueValue: [_hueSlider setFloatValue:MAX(MIN(ROUND(value), 360), 0)];
359  [self sliderChanged:_hueSlider];
360  break;
361 
362  case _saturationValue: [_saturationSlider setFloatValue:MAX(MIN(ROUND(value), 100), 0)];
363  [self sliderChanged:_saturationSlider];
364  break;
365 
366  case _brightnessValue: [_brightnessSlider setFloatValue:MAX(MIN(ROUND(value), 100), 0)];
367  [self sliderChanged:_brightnessSlider];
368  break;
369  }
370  }
371 }
372 
373 @end