API  1.0.0
CPColorPicker.j
Go to the documentation of this file.
1 /*
2  * CPColorPicker.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 
29 
30 
36 @implementation CPColorPicker : CPObject
37 {
38  CPColorPanel _panel;
39  int _mask;
40 }
41 
47 - (id)initWithPickerMask:(int)aMask colorPanel:(CPColorPanel)aPanel
48 {
49  if (self = [super init])
50  {
51  _panel = aPanel;
52  _mask = aMask;
53  }
54 
55  return self;
56 }
57 
61 - (CPColorPanel)colorPanel
62 {
63  return _panel;
64 }
65 
66 /*
67  FIXME Not implemented.
68  @return \c nil
69  @ignore
70 */
71 - (CPImage)provideNewButtonImage
72 {
73  return nil;
74 }
75 
80 - (void)setMode:(CPColorPanelMode)mode
81 {
82 }
83 
88 - (void)setColor:(CPColor)aColor
89 {
90 }
91 
92 @end
93 
94 /*
95  The wheel mode color picker.
96  @ignore
97 */
99 {
100  CPView _pickerView;
101  CPView _brightnessSlider;
102  __CPColorWheel _hueSaturationView;
103 
104  CPColor _cachedColor;
105 }
106 
107 - (id)initWithPickerMask:(int)mask colorPanel:(CPColorPanel)owningColorPanel
108 {
109  return [super initWithPickerMask:mask colorPanel: owningColorPanel];
110 }
111 
112 - (id)initView
113 {
114  var aFrame = CGRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight);
115 
116  _pickerView = [[CPView alloc] initWithFrame:aFrame];
117  [_pickerView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
118 
119  _brightnessSlider = [[CPSlider alloc] initWithFrame:CGRectMake(0, (aFrame.size.height - 34), aFrame.size.width, 15)];
120 
121  [_brightnessSlider setValue:15.0 forThemeAttribute:@"track-width"];
122  var brightnessImage = [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[CPColorPicker class]] pathForResource:@"brightness_bar.png"] size:CGSizeMake(272, 20)];
123  [_brightnessSlider setValue:[CPColor colorWithPatternImage:brightnessImage] forThemeAttribute:@"track-color"];
124 
125  [_brightnessSlider setMinValue:0.0];
126  [_brightnessSlider setMaxValue:100.0];
127  [_brightnessSlider setFloatValue:100.0];
128 
129  [_brightnessSlider setTarget:self];
130  [_brightnessSlider setAction:@selector(brightnessSliderDidChange:)];
131  [_brightnessSlider setAutoresizingMask:CPViewWidthSizable | CPViewMinYMargin];
132 
133  _hueSaturationView = [[__CPColorWheel alloc] initWithFrame:CGRectMake(0, 0, aFrame.size.width, aFrame.size.height - 38)];
134  [_hueSaturationView setDelegate:self];
135  [_hueSaturationView setAutoresizingMask:(CPViewWidthSizable | CPViewHeightSizable)];
136 
137  [_pickerView addSubview:_hueSaturationView];
138  [_pickerView addSubview:_brightnessSlider];
139 }
140 
141 - (void)brightnessSliderDidChange:(id)sender
142 {
143  [self updateColor];
144 }
145 
146 - (void)colorWheelDidChange:(id)sender
147 {
148  [self updateColor];
149 }
150 
151 - (void)updateColor
152 {
153  var hue = [_hueSaturationView angle],
154  saturation = [_hueSaturationView distance],
155  brightness = [_brightnessSlider floatValue];
156 
157  [_hueSaturationView setWheelBrightness:brightness / 100.0];
158  [_brightnessSlider setBackgroundColor:[CPColor colorWithHue:hue / 360.0 saturation:saturation / 100.0 brightness:1]];
159 
160  var colorPanel = [self colorPanel],
162 
163  _cachedColor = [CPColor colorWithHue:hue / 360.0 saturation:saturation / 100.0 brightness:brightness / 100.0 alpha:opacity];
164 
165  [[self colorPanel] setColor:_cachedColor];
166 }
167 
168 - (BOOL)supportsMode:(int)mode
169 {
170  return (mode == CPWheelColorPickerMode) ? YES : NO;
171 }
172 
173 - (int)currentMode
174 {
175  return CPWheelColorPickerMode;
176 }
177 
178 - (CPView)provideNewView:(BOOL)initialRequest
179 {
180  if (initialRequest)
181  [self initView];
182 
183  return _pickerView;
184 }
185 
186 - (void)setColor:(CPColor)newColor
187 {
188  if ([newColor isEqual:_cachedColor])
189  return;
190 
191  var hsb = [newColor hsbComponents];
192 
193  [_hueSaturationView setPositionToColor:newColor];
194  [_brightnessSlider setFloatValue:hsb[2] * 100.0];
195  [_hueSaturationView setWheelBrightness:hsb[2]];
196 
197  [_brightnessSlider setBackgroundColor:[CPColor colorWithHue:hsb[0] saturation:hsb[1] brightness:1]];
198 }
199 
200 - (CPImage)provideNewButtonImage
201 {
202  return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"wheel_button.png"] size:CGSizeMake(32, 32)];
203 }
204 
205 - (CPImage)provideNewAlternateButtonImage
206 {
207  return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"wheel_button_h.png"] size:CGSizeMake(32, 32)];
208 }
209 
210 @end
211 
212 /* @ignore */
213 @implementation __CPColorWheel : CPView
214 {
215  DOMElement _wheelImage;
216  DOMElement _blackWheelImage;
217 
218  CPView _crosshair;
219 
220  id _delegate;
221 
222  float _angle;
223  float _distance;
224 
225  float _radius;
226 }
227 
228 - (id)initWithFrame:(CGRect)aFrame
229 {
230  if (self = [super initWithFrame:aFrame])
231  {
232 #if PLATFORM(DOM)
233  var path = [[CPBundle bundleForClass:CPColorPicker] pathForResource:@"wheel.png"];
234 
235  _wheelImage = new Image();
236  _wheelImage.src = path;
237  _wheelImage.style.position = "absolute";
238 
239  path = [[CPBundle bundleForClass:CPColorPicker] pathForResource:@"wheel_black.png"];
240 
241  _blackWheelImage = new Image();
242  _blackWheelImage.src = path;
243  _blackWheelImage.style.opacity = "0";
244  _blackWheelImage.style.filter = "alpha(opacity=0)";
245  _blackWheelImage.style.position = "absolute";
246 
247  _DOMElement.appendChild(_wheelImage);
248  _DOMElement.appendChild(_blackWheelImage);
249 #endif
250 
251  [self setWheelSize:aFrame.size];
252 
253  _crosshair = [[CPView alloc] initWithFrame:CGRectMake(_radius - 2, _radius - 2, 4, 4)];
254  [_crosshair setBackgroundColor:[CPColor blackColor]];
255 
256  var view = [[CPView alloc] initWithFrame:CGRectInset([_crosshair bounds], 1.0, 1.0)];
257  [view setBackgroundColor:[CPColor whiteColor]];
258 
259  [_crosshair addSubview:view];
260 
261  [self addSubview:_crosshair];
262  }
263 
264  return self;
265 }
266 
267 - (void)setWheelBrightness:(float)brightness
268 {
269 #if PLATFORM(DOM)
270  _blackWheelImage.style.opacity = 1.0 - brightness;
271  _blackWheelImage.style.filter = "alpha(opacity=" + (1.0 - brightness) * 100 + ")"
272 #endif
273 }
274 
275 - (void)setFrameSize:(CGSize)aSize
276 {
277  [super setFrameSize:aSize];
278  [self setWheelSize:aSize];
279 }
280 
281 - (void)setWheelSize:(CGSize)aSize
282 {
283  var min = MIN(aSize.width, aSize.height);
284 
285 #if PLATFORM(DOM)
286  _blackWheelImage.style.width = min;
287  _blackWheelImage.style.height = min;
288  _blackWheelImage.width = min;
289  _blackWheelImage.height = min;
290  _blackWheelImage.style.top = (aSize.height - min) / 2.0 + "px";
291  _blackWheelImage.style.left = (aSize.width - min) / 2.0 + "px";
292 
293  _wheelImage.style.width = min;
294  _wheelImage.style.height = min;
295  _wheelImage.width = min;
296  _wheelImage.height = min;
297  _wheelImage.style.top = (aSize.height - min) / 2.0 + "px";
298  _wheelImage.style.left = (aSize.width - min) / 2.0 + "px";
299 #endif
300 
301  _radius = min / 2.0;
302 
303  [self setAngle:[self degreesToRadians:_angle] distance:(_distance / 100.0) * _radius];
304 }
305 
306 - (void)setDelegate:(id)aDelegate
307 {
308  _delegate = aDelegate;
309 }
310 
311 - (id)delegate
312 {
313  return _delegate;
314 }
315 
316 - (float)angle
317 {
318  return _angle;
319 }
320 
321 - (float)distance
322 {
323  return _distance;
324 }
325 
326 - (void)mouseDown:(CPEvent)anEvent
327 {
328  [self reposition:anEvent];
329 }
330 
331 - (void)mouseDragged:(CPEvent)anEvent
332 {
333  [self reposition:anEvent];
334 }
335 
336 - (void)reposition:(CPEvent)anEvent
337 {
338  var bounds = [self bounds],
339  location = [self convertPoint:[anEvent locationInWindow] fromView:nil],
340  midX = CGRectGetMidX(bounds),
341  midY = CGRectGetMidY(bounds),
342  distance = MIN(SQRT((location.x - midX) * (location.x - midX) + (location.y - midY) * (location.y - midY)), _radius),
343  angle = ATAN2(location.y - midY, location.x - midX);
344 
345  [self setAngle:angle distance:distance];
346 
347  [_delegate colorWheelDidChange:self];
348 }
349 
350 - (void)setAngle:(int)angle distance:(float)distance
351 {
352  var bounds = [self bounds],
353  midX = CGRectGetMidX(bounds),
354  midY = CGRectGetMidY(bounds);
355 
356  _angle = [self radiansToDegrees:angle];
357  _distance = (distance / _radius) * 100.0;
358 
359  [_crosshair setFrameOrigin:CGPointMake(COS(angle) * distance + midX - 2.0, SIN(angle) * distance + midY - 2.0)];
360 }
361 
362 - (void)setPositionToColor:(CPColor)aColor
363 {
364  var hsb = [aColor hsbComponents],
365  bounds = [self bounds],
366  angle = [self degreesToRadians:hsb[0] * 360.0],
367  distance = hsb[1] * _radius;
368 
369  [self setAngle:angle distance:distance];
370 }
371 
372 - (int)radiansToDegrees:(float)radians
373 {
374  return ((-radians / PI) * 180 + 360) % 360;
375 }
376 
377 - (float)degreesToRadians:(float)degrees
378 {
379  return -(((degrees - 360) / 180) * PI);
380 }
381 
382 @end
383 
id init()
Definition: CALayer.j:126
var isEqual
CPColor colorWithPatternImage:(CPImage anImage)
Definition: CPColor.j:482
CGPoint locationInWindow()
Definition: CPEvent.j:290
CPArray hsbComponents()
Definition: CPColor.j:679
CPColor whiteColor()
Definition: CPColor.j:361
CPColorPickerViewWidth
Definition: CPColorPanel.j:41
id delegate()
Definition: CALayer.j:965
void setColor:(CPColor aColor)
Definition: CPColorPanel.j:140
CGRect bounds()
Definition: CALayer.j:203
CPColor blackColor()
Definition: CPColor.j:284
CPString pathForResource:(CPString aFilename)
Definition: CPBundle.j:158
CPColorPanel colorPanel()
Definition: CPColorPicker.j:61
Definition: CPImage.h:2
float opacity()
Definition: CALayer.j:556
id initWithContentsOfFile:size:(CPString aFilename, [size] CGSize aSize)
Definition: CPImage.j:176
CPColor colorWithHue:saturation:brightness:alpha:(float hue, [saturation] float saturation, [brightness] float brightness, [alpha] float alpha)
Definition: CPColor.j:220
CPColor colorWithHue:saturation:brightness:(float hue, [saturation] float saturation, [brightness] float brightness)
Definition: CPColor.j:191
global CPApp CPWheelColorPickerMode
Definition: CPColorPanel.j:32
CPColorPickerViewHeight
Definition: CPColorPanel.j:42
CPBundle bundleForClass:(Class aClass)
Definition: CPBundle.j:77
Definition: CPEvent.h:2
Class class()
Definition: CPObject.j:179
id initWithPickerMask:colorPanel:(int aMask, [colorPanel] CPColorPanel aPanel)
Definition: CPColorPicker.j:47
id alloc()
Definition: CPObject.j:130
Definition: CPView.j:137