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