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
00030 @implementation CPColorPicker : CPObject
00031 {
00032 CPColorPanel _panel;
00033 int _mask;
00034 }
00035
00041 - (id)initWithPickerMask:(int)aMask colorPanel:(CPColorPanel)aPanel
00042 {
00043 self = [super init];
00044
00045 _panel = aPanel;
00046 _mask = aMask;
00047
00048 return self;
00049 }
00050
00054 - (CPColorPanel)colorPanel
00055 {
00056 return _panel;
00057 }
00058
00059
00060
00061
00062
00063
00064 - (CPImage)provideNewButtonImage
00065 {
00066 return nil;
00067 }
00068
00073 - (void)setMode:(CPColorPanelMode)mode
00074 {
00075 return;
00076 }
00077
00082 - (void)setColor:(CPColor)aColor
00083 {
00084 return;
00085 }
00086
00087 @end
00088
00089
00090
00091
00092
00093 @implementation CPColorWheelColorPicker : CPColorPicker
00094 {
00095 CPView _pickerView;
00096 CPView _brightnessSlider;
00097 DOMElement _brightnessBarImage;
00098 __CPColorWheel _hueSaturationView;
00099 }
00100
00101 - (id)initWithPickerMask:(int)mask colorPanel:(CPColorPanel)owningColorPanel
00102 {
00103 return [super initWithPickerMask:mask colorPanel: owningColorPanel];
00104 }
00105
00106 -(id)initView
00107 {
00108 aFrame = CPRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight);
00109
00110 _pickerView = [[CPView alloc] initWithFrame:aFrame];
00111 [_pickerView setAutoresizingMask:CPViewWidthSizable|CPViewHeightSizable];
00112
00113 var path = [[CPBundle bundleForClass: CPColorPicker] pathForResource:@"brightness_bar.png"];
00114
00115 _brightnessBarImage = new Image();
00116 _brightnessBarImage.src = path;
00117 _brightnessBarImage.style.width = "100%";
00118 _brightnessBarImage.style.height = "100%";
00119 _brightnessBarImage.style.position = "absolute";
00120 _brightnessBarImage.style.top = "0px";
00121 _brightnessBarImage.style.left = "0px";
00122
00123 var brightnessBarView = [[CPView alloc] initWithFrame: CPRectMake(0, (aFrame.size.height - 34), aFrame.size.width, 15)];
00124 [brightnessBarView setAutoresizingMask: (CPViewWidthSizable | CPViewMinYMargin)];
00125 brightnessBarView._DOMElement.appendChild(_brightnessBarImage);
00126
00127 _brightnessSlider = [[CPSlider alloc] initWithFrame: CPRectMake(0, aFrame.size.height - 22, aFrame.size.width, 12)];
00128 [_brightnessSlider setMaxValue: 100.0];
00129 [_brightnessSlider setMinValue: 0.0];
00130 [_brightnessSlider setFloatValue:100.0];
00131
00132 [_brightnessSlider setTarget: self];
00133 [_brightnessSlider setAction: @selector(brightnessSliderDidChange:)];
00134 [_brightnessSlider setAutoresizingMask: (CPViewWidthSizable | CPViewMinYMargin)];
00135
00136 _hueSaturationView = [[__CPColorWheel alloc] initWithFrame: CPRectMake(0, 0, aFrame.size.width, aFrame.size.height - 38)];
00137 [_hueSaturationView setDelegate: self];
00138 [_hueSaturationView setAutoresizingMask: (CPViewWidthSizable | CPViewHeightSizable)];
00139
00140 [_pickerView addSubview: brightnessBarView];
00141 [_pickerView addSubview: _hueSaturationView];
00142 [_pickerView addSubview: _brightnessSlider];
00143 }
00144
00145 -(void)brightnessSliderDidChange:(id)sender
00146 {
00147 [self updateColor];
00148 }
00149
00150 -(void)colorWheelDidChange:(id)sender
00151 {
00152 [self updateColor];
00153 }
00154
00155 -(void)updateColor
00156 {
00157 var hue = [_hueSaturationView angle],
00158 saturation = [_hueSaturationView distance],
00159 brightness = [_brightnessSlider floatValue];
00160
00161 [_hueSaturationView setWheelBrightness: brightness / 100.0];
00162 _brightnessBarImage.style.backgroundColor = "#"+[[CPColor colorWithHue: hue saturation: saturation brightness: 100] hexString];
00163
00164 [[self colorPanel] setColor:[CPColor colorWithHue: hue saturation: saturation brightness: brightness]];
00165 }
00166
00167 - (BOOL)supportsMode:(int)mode
00168 {
00169 return (mode == CPWheelColorPickerMode) ? YES : NO;
00170 }
00171
00172 - (int)currentMode
00173 {
00174 return CPWheelColorPickerMode;
00175 }
00176
00177 - (CPView)provideNewView:(BOOL)initialRequest
00178 {
00179 if (initialRequest)
00180 [self initView];
00181
00182 return _pickerView;
00183 }
00184
00185 - (void)setColor:(CPColor)newColor
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 _brightnessBarImage.style.backgroundColor = "#"+[[CPColor colorWithHue:hsb[0] saturation:hsb[1] brightness:100] hexString];
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 if(_delegate)
00339 [_delegate colorWheelDidChange: self];
00340 }
00341
00342 -(void)setAngle:(int)angle distance:(float)distance
00343 {
00344 var bounds = [self bounds];
00345 var midX = CGRectGetMidX(bounds);
00346 var midY = CGRectGetMidY(bounds);
00347
00348 _angle = [self radiansToDegrees: angle];
00349 _distance = (distance / _radius) * 100.0;
00350
00351 [_crosshair setFrameOrigin:CPPointMake(COS(angle) * distance + midX - 2.0, SIN(angle) * distance + midY - 2.0)];
00352 }
00353
00354 -(void)setPositionToColor:(CPColor)aColor
00355 {
00356 var hsb = [aColor hsbComponents],
00357 bounds = [self bounds];
00358
00359 var angle = [self degreesToRadians: hsb[0]],
00360 distance = (hsb[1] / 100.0) * _radius;
00361
00362 [self setAngle: angle distance: distance];
00363 }
00364
00365 -(int)radiansToDegrees:(float)radians
00366 {
00367 return ((-radians / PI) * 180 + 360) % 360;
00368 }
00369
00370 -(float)degreesToRadians:(float)degrees
00371 {
00372 return -(((degrees - 360) / 180) * PI);
00373 }
00374
00375 @end
00376