00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import <Foundation/CPObject.j>
00024 @import "CPColorPanel.j"
00025
00032 @implementation CPColorPicker : CPObject
00033 {
00034 CPColorPanel _panel;
00035 int _mask;
00036 }
00037
00043 - (id)initWithPickerMask:(int)aMask colorPanel:(CPColorPanel)aPanel
00044 {
00045 self = [super init];
00046
00047 _panel = aPanel;
00048 _mask = aMask;
00049
00050 return self;
00051 }
00052
00056 - (CPColorPanel)colorPanel
00057 {
00058 return _panel;
00059 }
00060
00061
00062
00063
00064
00065
00066 - (CPImage)provideNewButtonImage
00067 {
00068 return nil;
00069 }
00070
00075 - (void)setMode:(CPColorPanelMode)mode
00076 {
00077 return;
00078 }
00079
00084 - (void)setColor:(CPColor)aColor
00085 {
00086 return;
00087 }
00088
00089 @end
00090
00091
00092
00093
00094
00095 @implementation CPColorWheelColorPicker : CPColorPicker
00096 {
00097 CPView _pickerView;
00098 CPView _brightnessSlider;
00099 __CPColorWheel _hueSaturationView;
00100
00101 CPColor _cachedColor;
00102 }
00103
00104 - (id)initWithPickerMask:(int)mask colorPanel:(CPColorPanel)owningColorPanel
00105 {
00106 return [super initWithPickerMask:mask colorPanel: owningColorPanel];
00107 }
00108
00109 -(id)initView
00110 {
00111 aFrame = CPRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight);
00112
00113 _pickerView = [[CPView alloc] initWithFrame:aFrame];
00114 [_pickerView setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
00115
00116 _brightnessSlider = [[CPSlider alloc] initWithFrame:CGRectMake(0, (aFrame.size.height - 34), aFrame.size.width, 15)];
00117
00118 [_brightnessSlider setValue:15.0 forThemeAttribute:@"track-width"];
00119 [_brightnessSlider setValue:[CPColor colorWithPatternImage:[[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[CPColorPicker class]] pathForResource:@"brightness_bar.png"]]] forThemeAttribute:@"track-color"];
00120
00121 [_brightnessSlider setMinValue:0.0];
00122 [_brightnessSlider setMaxValue:100.0];
00123 [_brightnessSlider setFloatValue:100.0];
00124
00125 [_brightnessSlider setTarget:self];
00126 [_brightnessSlider setAction:@selector(brightnessSliderDidChange:)];
00127 [_brightnessSlider setAutoresizingMask:CPViewWidthSizable | CPViewMinYMargin];
00128
00129 _hueSaturationView = [[__CPColorWheel alloc] initWithFrame: CPRectMake(0, 0, aFrame.size.width, aFrame.size.height - 38)];
00130 [_hueSaturationView setDelegate: self];
00131 [_hueSaturationView setAutoresizingMask: (CPViewWidthSizable | CPViewHeightSizable)];
00132
00133 [_pickerView addSubview:_hueSaturationView];
00134 [_pickerView addSubview:_brightnessSlider];
00135 }
00136
00137 -(void)brightnessSliderDidChange:(id)sender
00138 {
00139 [self updateColor];
00140 }
00141
00142 -(void)colorWheelDidChange:(id)sender
00143 {
00144 [self updateColor];
00145 }
00146
00147 -(void)updateColor
00148 {
00149 var hue = [_hueSaturationView angle],
00150 saturation = [_hueSaturationView distance],
00151 brightness = [_brightnessSlider floatValue];
00152
00153 [_hueSaturationView setWheelBrightness:brightness / 100.0];
00154 [_brightnessSlider setBackgroundColor:[CPColor colorWithHue:hue saturation:saturation brightness:100]];
00155
00156 var colorPanel = [self colorPanel],
00157 opacity = [colorPanel opacity];
00158
00159 _cachedColor = [CPColor colorWithHue:hue saturation:saturation brightness:brightness alpha:opacity];
00160
00161 [[self colorPanel] setColor:_cachedColor];
00162 }
00163
00164 - (BOOL)supportsMode:(int)mode
00165 {
00166 return (mode == CPWheelColorPickerMode) ? YES : NO;
00167 }
00168
00169 - (int)currentMode
00170 {
00171 return CPWheelColorPickerMode;
00172 }
00173
00174 - (CPView)provideNewView:(BOOL)initialRequest
00175 {
00176 if (initialRequest)
00177 [self initView];
00178
00179 return _pickerView;
00180 }
00181
00182 - (void)setColor:(CPColor)newColor
00183 {
00184 if ([newColor isEqual:_cachedColor])
00185 return;
00186
00187 var hsb = [newColor hsbComponents];
00188
00189 [_hueSaturationView setPositionToColor:newColor];
00190 [_brightnessSlider setFloatValue:hsb[2]];
00191 [_hueSaturationView setWheelBrightness:hsb[2] / 100.0];
00192
00193 [_brightnessSlider setBackgroundColor:[CPColor colorWithHue:hsb[0] saturation:hsb[1] brightness:100]];
00194 }
00195
00196 - (CPImage)provideNewButtonImage
00197 {
00198 return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"wheel_button.png"] size:CGSizeMake(32, 32)];
00199 }
00200
00201 - (CPImage)provideNewAlternateButtonImage
00202 {
00203 return [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:CPColorPicker] pathForResource:"wheel_button_h.png"] size:CGSizeMake(32, 32)];
00204 }
00205
00206 @end
00207
00208
00209 @implementation __CPColorWheel : CPView
00210 {
00211 DOMElement _wheelImage;
00212 DOMElement _blackWheelImage;
00213
00214 CPView _crosshair;
00215
00216 id _delegate;
00217
00218 float _angle;
00219 float _distance;
00220
00221 float _radius;
00222 }
00223
00224 -(id)initWithFrame:(CPRect)aFrame
00225 {
00226 self = [super initWithFrame:aFrame];
00227
00228 var path = [[CPBundle bundleForClass:CPColorPicker] pathForResource:@"wheel.png"];
00229
00230 _wheelImage = new Image();
00231 _wheelImage.src = path;
00232 _wheelImage.style.position = "absolute";
00233
00234 path = [[CPBundle bundleForClass:CPColorPicker] pathForResource:@"wheel_black.png"];
00235
00236 _blackWheelImage = new Image();
00237 _blackWheelImage.src = path;
00238 _blackWheelImage.style.opacity = "0";
00239 _blackWheelImage.style.filter = "alpha(opacity=0)"
00240 _blackWheelImage.style.position = "absolute";
00241
00242 _DOMElement.appendChild(_wheelImage);
00243 _DOMElement.appendChild(_blackWheelImage);
00244
00245 [self setWheelSize:aFrame.size];
00246
00247 _crosshair = [[CPView alloc] initWithFrame:CPRectMake(_radius - 2, _radius - 2, 4, 4)];
00248 [_crosshair setBackgroundColor:[CPColor blackColor]];
00249
00250 var view = [[CPView alloc] initWithFrame:CGRectInset([_crosshair bounds], 1.0, 1.0)];
00251 [view setBackgroundColor:[CPColor whiteColor]];
00252
00253 [_crosshair addSubview:view];
00254
00255 [self addSubview:_crosshair];
00256
00257 return self;
00258 }
00259
00260 -(void)setWheelBrightness:(float)brightness
00261 {
00262 _blackWheelImage.style.opacity = 1.0 - brightness;
00263 _blackWheelImage.style.filter = "alpha(opacity=" + (1.0 - brightness)*100 + ")"
00264 }
00265
00266 -(void)setFrameSize:(CPSize)aSize
00267 {
00268 [super setFrameSize:aSize];
00269 [self setWheelSize:aSize];
00270 }
00271
00272 -(void)setWheelSize:(CPSize)aSize
00273 {
00274 var min = MIN(aSize.width, aSize.height);
00275
00276 _blackWheelImage.style.width = min;
00277 _blackWheelImage.style.height = min;
00278 _blackWheelImage.width = min;
00279 _blackWheelImage.height = min;
00280 _blackWheelImage.style.top = (aSize.height - min) / 2.0 + "px";
00281 _blackWheelImage.style.left = (aSize.width - min) / 2.0 + "px";
00282
00283 _wheelImage.style.width = min;
00284 _wheelImage.style.height = min;
00285 _wheelImage.width = min;
00286 _wheelImage.height = min;
00287 _wheelImage.style.top = (aSize.height - min) / 2.0 + "px";
00288 _wheelImage.style.left = (aSize.width - min) / 2.0 + "px";
00289
00290 _radius = min / 2.0;
00291
00292 [self setAngle:[self degreesToRadians:_angle] distance:(_distance / 100.0) * _radius];
00293 }
00294
00295 -(void)setDelegate:(id)aDelegate
00296 {
00297 _delegate = aDelegate;
00298 }
00299
00300 -(id)delegate
00301 {
00302 return _delegate;
00303 }
00304
00305 -(float)angle
00306 {
00307 return _angle;
00308 }
00309
00310 -(float)distance
00311 {
00312 return _distance;
00313 }
00314
00315 -(void)mouseDown:(CPEvent)anEvent
00316 {
00317 [self reposition:anEvent];
00318 }
00319
00320 -(void)mouseDragged:(CPEvent)anEvent
00321 {
00322 [self reposition:anEvent];
00323 }
00324
00325 -(void)reposition:(CPEvent)anEvent
00326 {
00327 var bounds = [self bounds],
00328 location = [self convertPoint:[anEvent locationInWindow] fromView:nil];
00329
00330 var midX = CGRectGetMidX(bounds);
00331 var midY = CGRectGetMidY(bounds);
00332
00333 var distance = MIN(SQRT((location.x - midX)*(location.x - midX) + (location.y - midY)*(location.y - midY)), _radius);
00334 var angle = ATAN2(location.y - midY, location.x - midX);
00335
00336 [self setAngle:angle distance:distance];
00337
00338 [_delegate colorWheelDidChange:self];
00339 }
00340
00341 -(void)setAngle:(int)angle distance:(float)distance
00342 {
00343 var bounds = [self bounds];
00344 var midX = CGRectGetMidX(bounds);
00345 var midY = CGRectGetMidY(bounds);
00346
00347 _angle = [self radiansToDegrees:angle];
00348 _distance = (distance / _radius) * 100.0;
00349
00350 [_crosshair setFrameOrigin:CPPointMake(COS(angle) * distance + midX - 2.0, SIN(angle) * distance + midY - 2.0)];
00351 }
00352
00353 -(void)setPositionToColor:(CPColor)aColor
00354 {
00355 var hsb = [aColor hsbComponents],
00356 bounds = [self bounds];
00357
00358 var angle = [self degreesToRadians:hsb[0]],
00359 distance = (hsb[1] / 100.0) * _radius;
00360
00361 [self setAngle:angle distance:distance];
00362 }
00363
00364 -(int)radiansToDegrees:(float)radians
00365 {
00366 return ((-radians / PI) * 180 + 360) % 360;
00367 }
00368
00369 -(float)degreesToRadians:(float)degrees
00370 {
00371 return -(((degrees - 360) / 180) * PI);
00372 }
00373
00374 @end
00375