43 - (id)initWithPickerMask:(
int)aMask colorPanel:(
CPColorPanel)aPanel
45 if (
self = [super init])
76 - (void)setMode:(CPColorPanelMode)mode
98 __CPColorWheel _hueSaturationView;
103 - (id)initWithPickerMask:(
int)mask colorPanel:(
CPColorPanel)owningColorPanel
112 _pickerView = [[
CPView alloc] initWithFrame:aFrame];
113 [_pickerView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
115 _brightnessSlider = [[
CPSlider alloc] initWithFrame:_CGRectMake(0, (aFrame.size.height - 34), aFrame.size.width, 15)];
117 [_brightnessSlider setValue:15.0 forThemeAttribute:@"track-width"];
120 [_brightnessSlider setMinValue:0.0];
121 [_brightnessSlider setMaxValue:100.0];
122 [_brightnessSlider setFloatValue:100.0];
124 [_brightnessSlider setTarget:self];
125 [_brightnessSlider setAction:@selector(brightnessSliderDidChange:)];
126 [_brightnessSlider setAutoresizingMask:CPViewWidthSizable | CPViewMinYMargin];
128 _hueSaturationView = [[__CPColorWheel alloc] initWithFrame:_CGRectMake(0, 0, aFrame.size.width, aFrame.size.height - 38)];
129 [_hueSaturationView setDelegate:self];
130 [_hueSaturationView setAutoresizingMask:(CPViewWidthSizable | CPViewHeightSizable)];
132 [_pickerView addSubview:_hueSaturationView];
133 [_pickerView addSubview:_brightnessSlider];
136 - (void)brightnessSliderDidChange:(
id)sender
141 - (void)colorWheelDidChange:(
id)sender
148 var hue = [_hueSaturationView angle],
149 saturation = [_hueSaturationView distance],
150 brightness = [_brightnessSlider floatValue];
152 [_hueSaturationView setWheelBrightness:brightness / 100.0];
163 - (BOOL)supportsMode:(
int)mode
173 - (
CPView)provideNewView:(BOOL)initialRequest
183 if ([newColor
isEqual:_cachedColor])
188 [_hueSaturationView setPositionToColor:newColor];
189 [_brightnessSlider setFloatValue:hsb[2]];
190 [_hueSaturationView setWheelBrightness:hsb[2] / 100.0];
208 @implementation __CPColorWheel :
CPView
210 DOMElement _wheelImage;
211 DOMElement _blackWheelImage;
223 - (id)initWithFrame:(CPRect)aFrame
225 if (
self = [super initWithFrame:aFrame])
230 _wheelImage =
new Image();
231 _wheelImage.src = path;
232 _wheelImage.style.position =
"absolute";
236 _blackWheelImage =
new Image();
237 _blackWheelImage.src = path;
238 _blackWheelImage.style.opacity =
"0";
239 _blackWheelImage.style.filter =
"alpha(opacity=0)"
240 _blackWheelImage.style.position =
"absolute";
242 _DOMElement.appendChild(_wheelImage);
243 _DOMElement.appendChild(_blackWheelImage);
246 [
self setWheelSize:aFrame.size];
248 _crosshair = [[
CPView alloc] initWithFrame:CGRectMake(_radius - 2, _radius - 2, 4, 4)];
251 var view = [[
CPView alloc] initWithFrame:CGRectInset([_crosshair bounds], 1.0, 1.0)];
254 [_crosshair addSubview:view];
256 [
self addSubview:_crosshair];
262 - (void)setWheelBrightness:(
float)brightness
265 _blackWheelImage.style.opacity = 1.0 - brightness;
266 _blackWheelImage.style.filter =
"alpha(opacity=" + (1.0 - brightness) * 100 + ")"
270 - (void)setFrameSize:(CPSize)aSize
272 [
super setFrameSize:aSize];
273 [
self setWheelSize:aSize];
276 - (void)setWheelSize:(CPSize)aSize
278 var min = MIN(aSize.width, aSize.height);
281 _blackWheelImage.style.width = min;
282 _blackWheelImage.style.height = min;
283 _blackWheelImage.width = min;
284 _blackWheelImage.height = min;
285 _blackWheelImage.style.top = (aSize.height - min) / 2.0 + "px";
286 _blackWheelImage.style.left = (aSize.width - min) / 2.0 + "px";
288 _wheelImage.style.width = min;
289 _wheelImage.style.height = min;
290 _wheelImage.width = min;
291 _wheelImage.height = min;
292 _wheelImage.style.top = (aSize.height - min) / 2.0 + "px";
293 _wheelImage.style.left = (aSize.width - min) / 2.0 + "px";
298 [
self setAngle:[
self degreesToRadians:_angle] distance:(_distance / 100.0) * _radius];
301 - (void)setDelegate:(
id)aDelegate
303 _delegate = aDelegate;
321 - (void)mouseDown:(
CPEvent)anEvent
323 [
self reposition:anEvent];
326 - (void)mouseDragged:(
CPEvent)anEvent
328 [
self reposition:anEvent];
331 - (void)reposition:(
CPEvent)anEvent
333 var bounds = [
self bounds],
335 midX = CGRectGetMidX(bounds),
336 midY = CGRectGetMidY(bounds),
337 distance = MIN(SQRT((location.x - midX) * (location.x - midX) + (location.y - midY) * (location.y - midY)), _radius),
338 angle = ATAN2(location.y - midY, location.x - midX);
340 [
self setAngle:angle distance:distance];
342 [_delegate colorWheelDidChange:self];
345 - (void)setAngle:(
int)angle distance:(
float)distance
347 var bounds = [
self bounds],
348 midX = CGRectGetMidX(bounds),
349 midY = CGRectGetMidY(bounds);
351 _angle = [
self radiansToDegrees:angle];
352 _distance = (distance / _radius) * 100.0;
354 [_crosshair setFrameOrigin:CPPointMake(COS(angle) * distance + midX - 2.0, SIN(angle) * distance + midY - 2.0)];
357 - (void)setPositionToColor:(
CPColor)aColor
360 bounds = [
self bounds],
361 angle = [
self degreesToRadians:hsb[0]],
362 distance = (hsb[1] / 100.0) * _radius;
364 [
self setAngle:angle distance:distance];
367 - (int)radiansToDegrees:(
float)radians
369 return ((-radians / PI) * 180 + 360) % 360;
372 - (float)degreesToRadians:(
float)degrees
374 return -(((degrees - 360) / 180) * PI);