62 _CPColorPanelToolbar _toolbar;
63 _CPColorPanelSwatches _swatchView;
64 _CPColorPanelPreview _previewView;
84 + (void)provideColorPickerClass:(Class)aColorPickerSubclass
104 + (void)setPickerMode:(CPColorPanelMode)mode
107 [panel setMode:mode];
117 styleMask:(CPTitledWindowMask | CPClosableWindowMask | CPResizableWindowMask)];
124 [
self setLevel:CPFloatingWindowLevel];
142 [_previewView setBackgroundColor:_color];
144 [CPApp sendAction:@selector(changeColor:) to:nil from:self];
146 if (_target && _action)
147 [CPApp sendAction:_action to:_target from:self];
153 [_activePicker setColor:_color];
154 [_opacitySlider setFloatValue:[_color alphaComponent]];
162 - (void)setColor:(
CPColor)aColor updatePicker:(BOOL)bool
167 [_activePicker setColor:_color];
180 return [_opacitySlider floatValue];
187 - (void)setTarget:(
id)aTarget
206 - (void)setAction:(selector)anAction
223 - (void)setMode:(CPColorPanelMode)mode
228 - (void)_setPicker:(
id)sender
230 var picker = _colorPickers[[sender tag]],
231 view = [picker provideNewView:NO];
234 view = [picker provideNewView:YES];
236 if (view == _currentView)
240 [view setFrame:[_currentView frame]];
244 bounds = [[self contentView] bounds];
246 [view setFrameSize:CPSizeMake(bounds.size.width - 10, bounds.size.height - height)];
247 [view setFrameOrigin:CPPointMake(5, height)];
250 [_currentView removeFromSuperview];
251 [[
self contentView] addSubview:view];
254 _activePicker = picker;
256 [picker setColor:[
self color]];
262 - (CPColorPanelMode)mode
267 - (void)orderFront:(
id)aSender
269 [
self _loadContentsIfNecessary];
274 - (void)_loadContentsIfNecessary
284 var count = [ColorPickerClasses count];
285 for (var i = 0; i < count; i++)
288 currentPicker = [[currentPickerClass alloc] initWithPickerMask:0 colorPanel:self];
290 _colorPickers.push(currentPicker);
293 var contentView = [
self contentView],
294 bounds = [contentView bounds];
296 _toolbar = [[
CPView alloc] initWithFrame:CGRectMake(0, 6, CGRectGetWidth(bounds), TOOLBAR_HEIGHT)];
297 [_toolbar setAutoresizingMask:CPViewWidthSizable];
300 leftOffset = (CGRectGetWidth(bounds) - totalToolbarWidth) / 2.0,
301 buttonForLater = nil;
303 for (var i = 0; i < count; i++)
305 var image = [_colorPickers[i] provideNewButtonImage],
306 highlightImage = [_colorPickers[i] provideNewAlternateButtonImage],
307 button = [[
CPButton alloc] initWithFrame:CGRectMake(leftOffset + i * (ICON_WIDTH + ICON_PADDING), 0, ICON_WIDTH, ICON_WIDTH)];
310 [button setTarget:self];
311 [button setAction:@selector(_setPicker:)];
312 [button setBordered:NO];
313 [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin];
315 [button setImage:image];
316 [button setAlternateImage:highlightImage];
318 [_toolbar addSubview:button];
321 buttonForLater = button;
325 var previewBox = [[
CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10, CGRectGetWidth(bounds) - 86, PREVIEW_HEIGHT)];
327 _previewView = [[_CPColorPanelPreview alloc] initWithFrame:CGRectInset([previewBox bounds], 2.0, 2.0)];
329 [_previewView setColorPanel:self];
330 [_previewView setAutoresizingMask:CPViewWidthSizable];
333 [previewBox setAutoresizingMask:CPViewWidthSizable];
335 [previewBox addSubview:_previewView];
337 var _previewLabel = [[
CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + 10, 60, 15)];
338 [_previewLabel setStringValue:"Preview:"];
340 [_previewLabel setAlignment:CPRightTextAlignment];
343 var swatchBox = [[
CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10 + PREVIEW_HEIGHT + 5, CGRectGetWidth(bounds) - 86, SWATCH_HEIGHT + 2.0)];
346 [swatchBox setAutoresizingMask:CPViewWidthSizable];
348 _swatchView = [[_CPColorPanelSwatches alloc] initWithFrame:CGRectInset([swatchBox bounds], 1.0, 1.0)];
350 [_swatchView setColorPanel:self];
351 [_swatchView setAutoresizingMask:CPViewWidthSizable];
353 [swatchBox addSubview:_swatchView];
355 var _swatchLabel = [[
CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + 8 + PREVIEW_HEIGHT + 6, 60, 15)];
356 [_swatchLabel setStringValue:"Swatches:"];
358 [_swatchLabel setAlignment:CPRightTextAlignment];
361 var opacityLabel = [[
CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + PREVIEW_HEIGHT + 35, 60, 20)];
362 [opacityLabel setStringValue:"Opacity:"];
364 [opacityLabel setAlignment:CPRightTextAlignment];
366 _opacitySlider = [[
CPSlider alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + PREVIEW_HEIGHT + 34, CGRectGetWidth(bounds) - 86, 20.0)];
368 [_opacitySlider setMinValue:0.0];
369 [_opacitySlider setMaxValue:1.0];
370 [_opacitySlider setAutoresizingMask:CPViewWidthSizable];
372 [_opacitySlider setTarget:self];
373 [_opacitySlider setAction:@selector(setOpacity:)];
375 [contentView addSubview:_toolbar];
376 [contentView addSubview:previewBox];
377 [contentView addSubview:_previewLabel];
378 [contentView addSubview:swatchBox];
379 [contentView addSubview:_swatchLabel];
380 [contentView addSubview:opacityLabel];
381 [contentView addSubview:_opacitySlider];
387 [_previewView setBackgroundColor:_color];
390 [
self _setPicker:buttonForLater];
393 - (void)setOpacity:(
id)sender
396 alpha = [sender floatValue];
409 @implementation _CPColorPanelSwatches :
CPView
417 - (id)initWithFrame:(CPRect)aFrame
419 self = [
super initWithFrame:aFrame];
427 _swatchCookie = [[
CPCookie alloc] initWithName:CPColorPanelSwatchesCookie];
428 var colorList = [
self startingColorList];
432 for (var i = 0; i < 50; i++)
435 var view = [[
CPView alloc] initWithFrame:CPRectMake(13 * i + 1, 1, 12, 12)],
436 fillView = [[
CPView alloc] initWithFrame:CGRectInset([view bounds], 1.0, 1.0)];
438 [view setBackgroundColor:whiteColor];
439 [fillView setBackgroundColor:(i < colorList.length) ? colorList[i] : whiteColor];
441 [view addSubview:fillView];
443 [
self addSubview:view];
445 _swatches.push(view);
458 var cookieValue = [_swatchCookie value];
475 var cookieValue = eval(cookieValue),
478 for (var i = 0; i < cookieValue.length; i++)
479 result.
push([
CPColor colorWithHexString:cookieValue[i]]);
488 for (var i = 0; i < _swatches.length; i++)
489 result.
push([[[_swatches[i] subviews][0] backgroundColor] hexString]);
491 var future =
new Date();
492 future.setYear(2019);
494 [_swatchCookie setValue:JSON.stringify(result) expires:future domain:nil];
507 - (
CPColor)colorAtIndex:(
int)index
509 return [[_swatches[index] subviews][0] backgroundColor];
512 - (void)setColor:(
CPColor)aColor atIndex:(
int)index
515 [[_swatches[index] subviews][0] setBackgroundColor:aColor];
516 [
self saveColorList];
519 - (void)mouseUp:(
CPEvent)anEvent
522 bounds = [
self bounds];
524 if (!CGRectContainsPoint(bounds, point) || point.x > [
self bounds].size.width - 1 || point.x < 1)
527 [_colorPanel setColor:[
self colorAtIndex:FLOOR(point.x / 13)] updatePicker:YES];
530 - (void)mouseDragged:(
CPEvent)anEvent
534 if (point.x > [
self bounds].size.width - 1 || point.x < 1)
539 var swatch = _swatches[FLOOR(point.x / 13)];
542 _dragColor = [[swatch subviews][0] backgroundColor];
544 var bounds = CGRectMakeCopy([swatch bounds]);
547 var dragView = [[
CPView alloc] initWithFrame:bounds],
548 dragFillView = [[
CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];
551 [dragFillView setBackgroundColor:_dragColor];
553 [dragView addSubview:dragFillView];
555 [
self dragView:dragView
556 at:CGPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
557 offset:CGPointMake(0.0, 0.0)
576 if (![pasteboard availableTypeFromArray:[
CPColorDragType]] || location.x > [
self bounds].size.width - 1 || location.x < 1)
585 @implementation _CPColorPanelPreview :
CPView
590 - (id)initWithFrame:(CPRect)aFrame
592 self = [
super initWithFrame:aFrame];
601 _colorPanel = aPanel;
617 [_colorPanel setColor:color updatePicker:YES];
625 - (void)mouseDragged:(
CPEvent)anEvent
634 var dragView = [[
CPView alloc] initWithFrame:bounds],
635 dragFillView = [[
CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];
638 [dragFillView setBackgroundColor:[
self backgroundColor]];
640 [dragView addSubview:dragFillView];
642 [
self dragView:dragView
643 at:CPPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
644 offset:CPPointMake(0.0, 0.0)