33 CPWheelColorPickerMode = 1;
40 CPSliderColorPickerMode = 2;
42 CPColorPickerViewWidth = 265;
43 CPColorPickerViewHeight = 370;
45 CPColorPanelColorDidChangeNotification =
@"CPColorPanelColorDidChangeNotification";
47 var PREVIEW_HEIGHT = 20.0,
48 TOOLBAR_HEIGHT = 32.0,
53 var SharedColorPanel = nil,
54 ColorPickerClasses = [];
66 _CPColorPanelToolbar _toolbar;
67 _CPColorPanelSwatches _swatchView;
68 _CPColorPanelPreview _previewView;
72 CPArray _colorPickers;
88 + (void)provideColorPickerClass:(Class)aColorPickerSubclass
90 ColorPickerClasses.push(aColorPickerSubclass);
98 if (!SharedColorPanel)
101 return SharedColorPanel;
108 + (void)setPickerMode:(CPColorPanelMode)mode
111 [panel setMode:mode];
120 self = [
super initWithContentRect:CGRectMake(500.0, 50.0, 219.0, 370.0)
121 styleMask:(CPTitledWindowMask | CPClosableWindowMask | CPResizableWindowMask)];
127 [
self setTitle:@"Color Panel"];
128 [
self setLevel:CPFloatingWindowLevel];
130 [
self setFloatingPanel:YES];
131 [
self setBecomesKeyOnlyIfNeeded:YES];
133 [
self setMinSize:CGSizeMake(219.0, 363.0)];
134 [
self setMaxSize:CGSizeMake(323.0, 537.0)];
143 - (void)setColor:(
CPColor)aColor
146 [_previewView setBackgroundColor:_color];
148 [CPApp sendAction:@selector(changeColor:) to:nil from:self];
150 if (_target && _action)
151 [CPApp sendAction:_action to:_target from:self];
157 [_activePicker setColor:_color];
158 [_opacitySlider setFloatValue:[_color alphaComponent]];
166 - (void)setColor:(
CPColor)aColor updatePicker:(BOOL)bool
168 [
self setColor:aColor];
171 [_activePicker setColor:_color];
184 return [_opacitySlider floatValue];
191 - (void)setTarget:(
id)aTarget
210 - (void)setAction:(selector)anAction
227 - (void)setMode:(CPColorPanelMode)mode
232 - (void)_setPicker:(
id)sender
234 var picker = _colorPickers[[sender tag]],
235 view = [picker provideNewView:NO];
238 view = [picker provideNewView:YES];
240 if (view == _currentView)
244 [view setFrame:[_currentView frame]];
247 var height = (TOOLBAR_HEIGHT + 10 + PREVIEW_HEIGHT + 5 + SWATCH_HEIGHT + 32),
248 bounds = [[self contentView] bounds];
250 [view setFrameSize:CGSizeMake(bounds.size.width - 10, bounds.size.height - height)];
251 [view setFrameOrigin:CGPointMake(5, height)];
254 [_currentView removeFromSuperview];
255 [[
self contentView] addSubview:view];
258 _activePicker = picker;
260 [picker setColor:[
self color]];
266 - (CPColorPanelMode)mode
271 - (void)orderFront:(
id)aSender
273 [
self _loadContentsIfNecessary];
274 [
super orderFront:aSender];
278 - (void)_loadContentsIfNecessary
288 var count = [ColorPickerClasses count];
289 for (var i = 0; i < count; i++)
291 var currentPickerClass = ColorPickerClasses[i],
292 currentPicker = [[currentPickerClass alloc] initWithPickerMask:0 colorPanel:self];
294 _colorPickers.push(currentPicker);
297 var contentView = [
self contentView],
298 bounds = [contentView bounds];
300 _toolbar = [[
CPView alloc] initWithFrame:CGRectMake(0, 6, CGRectGetWidth(bounds), TOOLBAR_HEIGHT)];
301 [_toolbar setAutoresizingMask:CPViewWidthSizable];
303 var totalToolbarWidth = count * ICON_WIDTH + (count - 1) * ICON_PADDING,
304 leftOffset = (CGRectGetWidth(bounds) - totalToolbarWidth) / 2.0,
305 buttonForLater = nil;
307 for (var i = 0; i < count; i++)
309 var image = [_colorPickers[i] provideNewButtonImage],
310 highlightImage = [_colorPickers[i] provideNewAlternateButtonImage],
311 button = [[
CPButton alloc] initWithFrame:CGRectMake(leftOffset + i * (ICON_WIDTH + ICON_PADDING), 0, ICON_WIDTH, ICON_WIDTH)];
314 [button setTarget:self];
315 [button setAction:@selector(_setPicker:)];
316 [button setBordered:NO];
317 [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin];
319 [button setImage:image];
320 [button setAlternateImage:highlightImage];
322 [_toolbar addSubview:button];
325 buttonForLater = button;
329 var previewBox = [[
CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10, CGRectGetWidth(bounds) - 86, PREVIEW_HEIGHT)];
331 _previewView = [[_CPColorPanelPreview alloc] initWithFrame:CGRectInset([previewBox bounds], 2.0, 2.0)];
333 [_previewView setColorPanel:self];
334 [_previewView setAutoresizingMask:CPViewWidthSizable];
337 [previewBox setAutoresizingMask:CPViewWidthSizable];
339 [previewBox addSubview:_previewView];
341 var _previewLabel = [[
CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + 10, 60, 15)];
342 [_previewLabel setStringValue:"Preview:"];
344 [_previewLabel setAlignment:CPRightTextAlignment];
347 var swatchBox = [[
CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10 + PREVIEW_HEIGHT + 5, CGRectGetWidth(bounds) - 86, SWATCH_HEIGHT + 2.0)];
350 [swatchBox setAutoresizingMask:CPViewWidthSizable];
352 _swatchView = [[_CPColorPanelSwatches alloc] initWithFrame:CGRectInset([swatchBox bounds], 1.0, 1.0)];
354 [_swatchView setColorPanel:self];
355 [_swatchView setAutoresizingMask:CPViewWidthSizable];
357 [swatchBox addSubview:_swatchView];
359 var _swatchLabel = [[
CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + 8 + PREVIEW_HEIGHT + 6, 60, 15)];
360 [_swatchLabel setStringValue:"Swatches:"];
362 [_swatchLabel setAlignment:CPRightTextAlignment];
365 var opacityLabel = [[
CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + PREVIEW_HEIGHT + 35, 60, 20)];
366 [opacityLabel setStringValue:"Opacity:"];
368 [opacityLabel setAlignment:CPRightTextAlignment];
370 _opacitySlider = [[
CPSlider alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + PREVIEW_HEIGHT + 34, CGRectGetWidth(bounds) - 86, 20.0)];
372 [_opacitySlider setMinValue:0.0];
373 [_opacitySlider setMaxValue:1.0];
374 [_opacitySlider setAutoresizingMask:CPViewWidthSizable];
376 [_opacitySlider setTarget:self];
377 [_opacitySlider setAction:@selector(setOpacity:)];
379 [contentView addSubview:_toolbar];
380 [contentView addSubview:previewBox];
381 [contentView addSubview:_previewLabel];
382 [contentView addSubview:swatchBox];
383 [contentView addSubview:_swatchLabel];
384 [contentView addSubview:opacityLabel];
385 [contentView addSubview:_opacitySlider];
391 [_previewView setBackgroundColor:_color];
394 [
self _setPicker:buttonForLater];
397 - (void)setOpacity:(
id)sender
399 var components = [[
self color] components],
400 alpha = [sender floatValue];
402 [
self setColor:[_color colorWithAlphaComponent:alpha] updatePicker:YES];
408 CPColorDragType =
"CPColorDragType";
410 var CPColorPanelSwatchesCookie =
"CPColorPanelSwatchesCookie";
413 @implementation _CPColorPanelSwatches :
CPView
421 - (id)initWithFrame:(CGRect)aFrame
423 self = [
super initWithFrame:aFrame];
427 [
self registerForDraggedTypes:[CPArray arrayWithObjects:CPColorDragType]];
431 _swatchCookie = [[
CPCookie alloc] initWithName:CPColorPanelSwatchesCookie];
432 var colorList = [
self startingColorList];
436 for (var i = 0; i < 50; i++)
439 var view = [[
CPView alloc] initWithFrame:CGRectMake(13 * i + 1, 1, 12, 12)],
440 fillView = [[
CPView alloc] initWithFrame:CGRectInset([view bounds], 1.0, 1.0)];
442 [view setBackgroundColor:whiteColor];
443 [fillView setBackgroundColor:(i < colorList.length) ? colorList[i] : whiteColor];
445 [view addSubview:fillView];
447 [
self addSubview:view];
449 _swatches.push(view);
460 - (CPArray)startingColorList
462 var cookieValue = [_swatchCookie value];
479 var cookieValue = eval(cookieValue),
482 for (var i = 0; i < cookieValue.length; i++)
483 result.push([
CPColor colorWithHexString:cookieValue[i]]);
488 - (CPArray)saveColorList
492 for (var i = 0; i < _swatches.length; i++)
493 result.push([[[_swatches[i] subviews][0] backgroundColor] hexString]);
495 var future =
new Date();
496 future.setYear(2019);
498 [_swatchCookie setValue:JSON.stringify(result) expires:future domain:nil];
511 - (
CPColor)colorAtIndex:(
int)index
513 return [[_swatches[index] subviews][0] backgroundColor];
516 - (void)setColor:(
CPColor)aColor atIndex:(
int)index
519 [[_swatches[index] subviews][0] setBackgroundColor:aColor];
520 [
self saveColorList];
523 - (void)mouseUp:(
CPEvent)anEvent
525 var point = [
self convertPoint:[anEvent locationInWindow] fromView:nil],
526 bounds = [
self bounds];
528 if (!CGRectContainsPoint(bounds, point) || point.x > [
self bounds].size.width - 1 || point.x < 1)
531 [_colorPanel setColor:[
self colorAtIndex:FLOOR(point.x / 13)] updatePicker:YES];
534 - (void)mouseDragged:(
CPEvent)anEvent
536 var point = [
self convertPoint:[anEvent locationInWindow] fromView:nil];
538 if (point.x > [
self bounds].size.width - 1 || point.x < 1)
543 var swatch = _swatches[FLOOR(point.x / 13)];
546 _dragColor = [[swatch subviews][0] backgroundColor];
548 var bounds = CGRectMakeCopy([swatch bounds]);
551 var dragView = [[
CPView alloc] initWithFrame:bounds],
552 dragFillView = [[
CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];
555 [dragFillView setBackgroundColor:_dragColor];
557 [dragView addSubview:dragFillView];
559 [
self dragView:dragView
560 at:CGPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
561 offset:CGPointMake(0.0, 0.0)
570 if (aType == CPColorDragType)
574 - (void)performDragOperation:(
id )aSender
576 var location = [
self convertPoint:[aSender draggingLocation] fromView:nil],
577 pasteboard = [aSender draggingPasteboard],
580 if (![pasteboard availableTypeFromArray:[CPColorDragType]] || location.x > [
self bounds].size.width - 1 || location.x < 1)
589 @implementation _CPColorPanelPreview :
CPView
594 - (id)initWithFrame:(CGRect)aFrame
596 self = [
super initWithFrame:aFrame];
598 [
self registerForDraggedTypes:[CPArray arrayWithObjects:CPColorDragType]];
605 _colorPanel = aPanel;
613 - (void)performDragOperation:(
id )aSender
615 var pasteboard = [aSender draggingPasteboard];
617 if (![pasteboard availableTypeFromArray:[CPColorDragType]])
621 [_colorPanel setColor:color updatePicker:YES];
629 - (void)mouseDragged:(
CPEvent)anEvent
631 var point = [
self convertPoint:[anEvent locationInWindow] fromView:nil];
635 var bounds = CGRectMake(0, 0, 15, 15);
638 var dragView = [[
CPView alloc] initWithFrame:bounds],
639 dragFillView = [[
CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];
642 [dragFillView setBackgroundColor:[
self backgroundColor]];
644 [dragView addSubview:dragFillView];
646 [
self dragView:dragView
647 at:CGPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
648 offset:CGPointMake(0.0, 0.0)
657 if (aType == CPColorDragType)
664 [
CPColorPanel provideColorPickerClass:CPColorWheelColorPicker];
665 [
CPColorPanel provideColorPickerClass:CPSliderColorPicker];