47 - (id)initWithPickerMask:(
int)aMask colorPanel:(
CPColorPanel)aPanel
49 if (
self = [super
init])
80 - (void)setMode:(CPColorPanelMode)mode
102 __CPColorWheel _hueSaturationView;
107 - (id)initWithPickerMask:(
int)mask colorPanel:(
CPColorPanel)owningColorPanel
116 _pickerView = [[
CPView alloc] initWithFrame:aFrame];
117 [_pickerView setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
119 _brightnessSlider = [[
CPSlider alloc] initWithFrame:CGRectMake(0, (aFrame.size.height - 34), aFrame.size.width, 15)];
121 [_brightnessSlider setValue:15.0 forThemeAttribute:@"track-width"];
124 [_brightnessSlider setMinValue:0.0];
125 [_brightnessSlider setMaxValue:100.0];
126 [_brightnessSlider setFloatValue:100.0];
128 [_brightnessSlider setTarget:self];
129 [_brightnessSlider setAction:@selector(brightnessSliderDidChange:)];
130 [_brightnessSlider setAutoresizingMask:CPViewWidthSizable | CPViewMinYMargin];
132 _hueSaturationView = [[__CPColorWheel alloc] initWithFrame:CGRectMake(0, 0, aFrame.size.width, aFrame.size.height - 38)];
133 [_hueSaturationView setDelegate:self];
134 [_hueSaturationView setAutoresizingMask:(CPViewWidthSizable | CPViewHeightSizable)];
136 [_pickerView addSubview:_hueSaturationView];
137 [_pickerView addSubview:_brightnessSlider];
140 - (void)brightnessSliderDidChange:(
id)sender
145 - (void)colorWheelDidChange:(
id)sender
152 var hue = [_hueSaturationView angle],
153 saturation = [_hueSaturationView distance],
154 brightness = [_brightnessSlider floatValue];
156 [_hueSaturationView setWheelBrightness:brightness / 100.0];
167 - (BOOL)supportsMode:(
int)mode
177 - (
CPView)provideNewView:(BOOL)initialRequest
187 if ([newColor
isEqual:_cachedColor])
192 [_hueSaturationView setPositionToColor:newColor];
193 [_brightnessSlider setFloatValue:hsb[2] * 100.0];
194 [_hueSaturationView setWheelBrightness:hsb[2]];
212 @implementation __CPColorWheel :
CPView
214 DOMElement _wheelImage;
215 DOMElement _blackWheelImage;
227 - (id)initWithFrame:(CGRect)aFrame
229 if (
self = [super initWithFrame:aFrame])
234 _wheelImage =
new Image();
235 _wheelImage.src = path;
236 _wheelImage.style.position =
"absolute";
240 _blackWheelImage =
new Image();
241 _blackWheelImage.src = path;
242 _blackWheelImage.style.opacity =
"0";
243 _blackWheelImage.style.filter =
"alpha(opacity=0)"
244 _blackWheelImage.style.position =
"absolute";
246 _DOMElement.appendChild(_wheelImage);
247 _DOMElement.appendChild(_blackWheelImage);
250 [
self setWheelSize:aFrame.size];
252 _crosshair = [[
CPView alloc] initWithFrame:CGRectMake(_radius - 2, _radius - 2, 4, 4)];
255 var view = [[
CPView alloc] initWithFrame:CGRectInset([_crosshair bounds], 1.0, 1.0)];
258 [_crosshair addSubview:view];
260 [
self addSubview:_crosshair];
266 - (void)setWheelBrightness:(
float)brightness
269 _blackWheelImage.style.opacity = 1.0 - brightness;
270 _blackWheelImage.style.filter =
"alpha(opacity=" + (1.0 - brightness) * 100 + ")"
274 - (void)setFrameSize:(CGSize)aSize
276 [
super setFrameSize:aSize];
277 [
self setWheelSize:aSize];
280 - (void)setWheelSize:(CGSize)aSize
282 var min = MIN(aSize.width, aSize.height);
285 _blackWheelImage.style.width = min;
286 _blackWheelImage.style.height = min;
287 _blackWheelImage.width = min;
288 _blackWheelImage.height = min;
289 _blackWheelImage.style.top = (aSize.height - min) / 2.0 + "px";
290 _blackWheelImage.style.left = (aSize.width - min) / 2.0 + "px";
292 _wheelImage.style.width = min;
293 _wheelImage.style.height = min;
294 _wheelImage.width = min;
295 _wheelImage.height = min;
296 _wheelImage.style.top = (aSize.height - min) / 2.0 + "px";
297 _wheelImage.style.left = (aSize.width - min) / 2.0 + "px";
302 [
self setAngle:[
self degreesToRadians:_angle] distance:(_distance / 100.0) * _radius];
305 - (void)setDelegate:(
id)aDelegate
307 _delegate = aDelegate;
325 - (void)mouseDown:(
CPEvent)anEvent
327 [
self reposition:anEvent];
330 - (void)mouseDragged:(
CPEvent)anEvent
332 [
self reposition:anEvent];
335 - (void)reposition:(
CPEvent)anEvent
337 var
bounds = [
self bounds],
339 midX = CGRectGetMidX(
bounds),
340 midY = CGRectGetMidY(
bounds),
341 distance = MIN(SQRT((location.x - midX) * (location.x - midX) + (location.y - midY) * (location.y - midY)), _radius),
342 angle = ATAN2(location.y - midY, location.x - midX);
344 [
self setAngle:angle distance:distance];
346 [_delegate colorWheelDidChange:self];
349 - (void)setAngle:(
int)angle distance:(
float)distance
351 var
bounds = [
self bounds],
352 midX = CGRectGetMidX(bounds),
353 midY = CGRectGetMidY(bounds);
355 _angle = [
self radiansToDegrees:angle];
356 _distance = (distance / _radius) * 100.0;
358 [_crosshair setFrameOrigin:CGPointMake(COS(angle) * distance + midX - 2.0, SIN(angle) * distance + midY - 2.0)];
361 - (void)setPositionToColor:(
CPColor)aColor
365 angle = [
self degreesToRadians:hsb[0] * 360.0],
366 distance = hsb[1] * _radius;
368 [
self setAngle:angle distance:distance];
371 - (int)radiansToDegrees:(
float)radians
373 return ((-radians / PI) * 180 + 360) % 360;
376 - (float)degreesToRadians:(
float)degrees
378 return -(((degrees - 360) / 180) * PI);