diff --git a/Classes/OMColorFrameView.h b/Classes/OMColorFrameView.h deleted file mode 100644 index e724878..0000000 --- a/Classes/OMColorFrameView.h +++ /dev/null @@ -1,18 +0,0 @@ -// -// OMColorFrameView.h -// OMColorHelper -// -// Created by Ole Zorn on 09/07/12. -// -// - -#import - -@interface OMColorFrameView : NSView { - - NSColor *_color; -} - -@property (nonatomic, strong) NSColor *color; - -@end diff --git a/Classes/OMColorFrameView.m b/Classes/OMColorFrameView.m deleted file mode 100644 index 413b766..0000000 --- a/Classes/OMColorFrameView.m +++ /dev/null @@ -1,30 +0,0 @@ -// -// OMColorFrameView.m -// OMColorHelper -// -// Created by Ole Zorn on 09/07/12. -// -// - -#import "OMColorFrameView.h" - -@implementation OMColorFrameView - -@synthesize color=_color; - -- (void)drawRect:(NSRect)dirtyRect -{ - [self.color setStroke]; - [NSBezierPath strokeRect:NSInsetRect(self.bounds, 0.5, 0.5)]; -} - -- (void)setColor:(NSColor *)color -{ - if (color != _color) { - _color = color; - [self setNeedsDisplay:YES]; - } -} - - -@end diff --git a/Classes/OMColorHelper.h b/Classes/OMColorHelper.h index 51b663a..ecda090 100644 --- a/Classes/OMColorHelper.h +++ b/Classes/OMColorHelper.h @@ -1,64 +1,52 @@ -// -// OMColorHelper.h -// OMColorHelper -// -// Created by Ole Zorn on 09/07/12. -// -// -#import -#import +// OMColorHelper.h OMColorHelper Created by Ole Zorn on 09/07/12. + +@import AppKit; + +typedef NS_ENUM(int, OMColorType) { -typedef enum OMColorType { OMColorTypeNone = 0, - OMColorTypeUIRGBA, //[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] - OMColorTypeUIRGBAInit, //[[UIColor alloc] initWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] - OMColorTypeUIWhite, //[UIColor colorWithWhite:0.5 alpha:1.0] - OMColorTypeUIWhiteInit, //[[UIColor alloc] initWithWhite:0.5 alpha:1.0] - OMColorTypeUIConstant, //[UIColor redColor] - - OMColorTypeNSRGBACalibrated, //[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0] - OMColorTypeNSRGBADevice, //[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] - OMColorTypeNSWhiteCalibrated, //[NSColor colorWithCalibratedWhite:0.5 alpha:1.0] - OMColorTypeNSWhiteDevice, //[NSColor colorWithDeviceWhite:0.5 alpha:1.0] - OMColorTypeNSConstant, //[NSColor redColor] + OMColorTypeUIRGBA, // [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] + OMColorTypeUIRGBAInit, // [[UIColor alloc] initWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] + OMColorTypeUIWhite, // [UIColor colorWithWhite:0.5 alpha:1.0] + OMColorTypeUIWhiteInit, // [[UIColor alloc] initWithWhite:0.5 alpha:1.0] + OMColorTypeUIConstant, // [UIColor redColor] -} OMColorType; + OMColorTypeNSRGBACalibrated, // [NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0] + OMColorTypeNSRGBADevice, // [NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] + OMColorTypeNSWhiteCalibrated, // [NSColor colorWithCalibratedWhite:0.5 alpha:1.0] + OMColorTypeNSWhiteDevice, // [NSColor colorWithDeviceWhite:0.5 alpha:1.0] + OMColorTypeNSConstant, // [NSColor redColor] + OMColorType6DigitHex, // #ffffff + OMColorType3DigitHex // #fff + +}; BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTypeNSRGBACalibrated; } //TODO: Maybe support HSB and CMYK color types... -@class OMColorFrameView, OMPlainColorWell; +@class OMColorFrameView , + OMPlainColorWell ; -@interface OMColorHelper : NSObject { - - OMPlainColorWell *_colorWell; - OMColorFrameView *_colorFrameView; - NSRange _selectedColorRange; - OMColorType _selectedColorType; - NSTextView *_textView; - NSDictionary *_constantColorsByName; - - NSRegularExpression *_rgbaUIColorRegex; - NSRegularExpression *_rgbaNSColorRegex; - NSRegularExpression *_whiteNSColorRegex; - NSRegularExpression *_whiteUIColorRegex; - NSRegularExpression *_constantColorRegex; -} +@interface OMColorHelper : NSObject + +@property (nonatomic) OMPlainColorWell * colorWell; +@property (nonatomic) OMColorFrameView * colorFrameView; +@property (nonatomic) NSTextView * textView; +@property (nonatomic) NSRange selectedColorRange; +@property (nonatomic) OMColorType selectedColorType; + +- (void) dismissColorWell; +- (void) activateColorHighlighting; +- (void) deactivateColorHighlighting; + +- (NSColor*) colorInText:(NSString*)x selectedRange:(NSRange)r + type:(OMColorType*)t matchedRange:(NSRangePointer)m; -@property (nonatomic, strong) OMPlainColorWell *colorWell; -@property (nonatomic, strong) OMColorFrameView *colorFrameView; -@property (nonatomic, strong) NSTextView *textView; -@property (nonatomic, assign) NSRange selectedColorRange; -@property (nonatomic, assign) OMColorType selectedColorType; +- (NSString*) colorStringForColor:(NSColor*)c withType:(OMColorType)t; -- (void)dismissColorWell; -- (void)activateColorHighlighting; -- (void)deactivateColorHighlighting; -- (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange type:(OMColorType *)type matchedRange:(NSRangePointer)matchedRange; -- (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorType; -- (double)dividedValue:(double)value withDivisorRange:(NSRange)divisorRange inString:(NSString *)text; +- (double) dividedValue:(double)v withDivisorRange:(NSRange)r inString:(NSString *)text; @end diff --git a/Classes/OMColorHelper.m b/Classes/OMColorHelper.m index a2e0a03..095da26 100755 --- a/Classes/OMColorHelper.m +++ b/Classes/OMColorHelper.m @@ -1,278 +1,306 @@ -// -// OMColorHelper.m -// OMColorHelper -// -// Created by Ole Zorn on 09/07/12. -// -// + +// OMColorHelper.m OMColorHelper Created by Ole Zorn on 09/07/12. #import "OMColorHelper.h" -#import "OMPlainColorWell.h" -#import "OMColorFrameView.h" +#import "OMColorViews.h" #define kOMColorHelperHighlightingDisabled @"OMColorHelperHighlightingDisabled" -#define kOMColorHelperInsertionMode @"OMColorHelperInsertionMode" +#define kOMColorHelperInsertionMode @"OMColorHelperInsertionMode" + +#define kNCENTER [NSNotificationCenter defaultCenter] +#define kUSRDEFS [NSUserDefaults standardUserDefaults] -@implementation OMColorHelper +@implementation OMColorHelper { -@synthesize colorWell=_colorWell, colorFrameView=_colorFrameView, textView=_textView, selectedColorRange=_selectedColorRange, selectedColorType=_selectedColorType; + NSDictionary * _constantColorsByName; + NSRegularExpression * _rgbaUIColorRegex, + * _rgbaNSColorRegex, + * _whiteNSColorRegex, + * _whiteUIColorRegex, + * _constantColorRegex; +} #pragma mark - Plugin Initialization -+ (void)pluginDidLoad:(NSBundle *)plugin -{ ++ (void) pluginDidLoad:(NSBundle *)plugin { + static id sharedPlugin = nil; static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedPlugin = [[self alloc] init]; - }); + dispatch_once(&onceToken, ^{ sharedPlugin = [[self alloc] init]; }); } -- (id)init -{ - if (self = [super init]) { - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidFinishLaunching:) name:NSApplicationDidFinishLaunchingNotification object:nil]; - _selectedColorRange = NSMakeRange(NSNotFound, 0); - _constantColorsByName = [[NSDictionary alloc] initWithObjectsAndKeys: - [[NSColor blackColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]], @"black", - [NSColor darkGrayColor], @"darkGray", - [NSColor lightGrayColor], @"lightGray", - [[NSColor whiteColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]], @"white", - [NSColor grayColor], @"gray", - [NSColor redColor], @"red", - [NSColor greenColor], @"green", - [NSColor blueColor], @"blue", - [NSColor cyanColor], @"cyan", - [NSColor yellowColor], @"yellow", - [NSColor magentaColor], @"magenta", - [NSColor orangeColor], @"orange", - [NSColor purpleColor], @"purple", - [NSColor brownColor], @"brown", - [[NSColor clearColor] colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]], @"clear", nil]; - - _rgbaUIColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*UIColor\\s+colorWith|\\[\\s*\\[\\s*UIColor\\s+alloc\\]\\s*initWith)Red:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+green:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+blue:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; - _whiteUIColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*UIColor\\s+colorWith|\\[\\s*\\[\\s*UIColor\\s+alloc\\]\\s*initWith)White:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; - _rgbaNSColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*NSColor\\s+colorWith(Calibrated|Device)Red:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+green:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+blue:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; - _whiteNSColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*NSColor\\s+colorWith(Calibrated|Device)White:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; - _constantColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*(UI|NS)Color\\s+(black|darkGray|lightGray|white|gray|red|green|blue|cyan|yellow|magenta|orange|purple|brown|clear)Color\\s*\\]" options:0 error:NULL]; - } +- (instancetype) init { if (!(self = super.init)) return nil; + + [kNCENTER addObserver:self selector:@selector(applicationDidFinishLaunching:) + name:NSApplicationDidFinishLaunchingNotification object:nil]; + _selectedColorRange = NSMakeRange(NSNotFound, 0); + _constantColorsByName = @{ + + @"black" : [NSColor.blackColor colorUsingColorSpace:NSColorSpace.genericRGBColorSpace], + @"darkGray" : NSColor.darkGrayColor, + @"lightGray" : NSColor.lightGrayColor, + @"white" : [NSColor.whiteColor colorUsingColorSpace:NSColorSpace.genericRGBColorSpace], + @"gray" : NSColor.grayColor, + @"red" : NSColor.redColor, + @"green" : NSColor.greenColor, + @"blue" : NSColor.blueColor, + @"cyan" : NSColor.cyanColor, + @"yellow" : NSColor.yellowColor, + @"magenta" : NSColor.magentaColor, + @"orange" : NSColor.orangeColor, + @"purple" : NSColor.purpleColor, + @"brown" : NSColor.brownColor, + @"clear" : [NSColor.clearColor colorUsingColorSpace:NSColorSpace.genericRGBColorSpace] + }; + + _rgbaUIColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*UIColor\\s+colorWith|\\[\\s*\\[\\s*UIColor\\s+alloc\\]\\s*initWith)Red:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+green:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+blue:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; + _whiteUIColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*UIColor\\s+colorWith|\\[\\s*\\[\\s*UIColor\\s+alloc\\]\\s*initWith)White:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; + _rgbaNSColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*NSColor\\s+colorWith(Calibrated|Device)Red:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+green:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+blue:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; + _whiteNSColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*NSColor\\s+colorWith(Calibrated|Device)White:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\]" options:0 error:NULL]; + _constantColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*(UI|NS)Color\\s+(black|darkGray|lightGray|white|gray|red|green|blue|cyan|yellow|magenta|orange|purple|brown|clear)Color\\s*\\]" options:0 error:NULL]; return self; } -- (void)applicationDidFinishLaunching:(NSNotification *)notification -{ +- (void)applicationDidFinishLaunching:(NSNotification *)_ { + NSMenuItem *editMenuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"]; if (editMenuItem) { - [[editMenuItem submenu] addItem:[NSMenuItem separatorItem]]; + [editMenuItem.submenu addItem:NSMenuItem.separatorItem]; - NSMenuItem *toggleColorHighlightingMenuItem = [[NSMenuItem alloc] initWithTitle:@"Show Colors Under Caret" action:@selector(toggleColorHighlightingEnabled:) keyEquivalent:@""]; - [toggleColorHighlightingMenuItem setTarget:self]; - [[editMenuItem submenu] addItem:toggleColorHighlightingMenuItem]; + NSMenuItem *toggleHighlightMenuItem = [NSMenuItem.alloc initWithTitle:@"Show Colors Under Caret" + action:@selector(toggleColorHighlightingEnabled:) keyEquivalent:@""]; + toggleHighlightMenuItem.target = self; + + [editMenuItem.submenu addItem:toggleHighlightMenuItem]; - NSMenuItem *colorInsertionModeItem = [[NSMenuItem alloc] initWithTitle:@"Color Insertion Mode" action:nil keyEquivalent:@""]; - NSMenuItem *colorInsertionModeNSItem = [[NSMenuItem alloc] initWithTitle:@"NSColor" action:@selector(selectNSColorInsertionMode:) keyEquivalent:@""]; - [colorInsertionModeNSItem setTarget:self]; - NSMenuItem *colorInsertionModeUIItem = [[NSMenuItem alloc] initWithTitle:@"UIColor" action:@selector(selectUIColorInsertionMode:) keyEquivalent:@""]; - [colorInsertionModeUIItem setTarget:self]; + NSMenuItem *colorInsertionModeItem = [NSMenuItem.alloc initWithTitle:@"Color Insertion Mode" + action:nil + keyEquivalent:@""]; + NSMenuItem *colorInsertionModeNSItem = [NSMenuItem.alloc initWithTitle:@"NSColor" + action:@selector(selectNSColorInsertionMode:) + keyEquivalent:@""]; + colorInsertionModeNSItem.target = self; + NSMenuItem *colorInsertionModeUIItem = [NSMenuItem.alloc initWithTitle:@"UIColor" + action:@selector(selectUIColorInsertionMode:) + keyEquivalent:@""]; + colorInsertionModeUIItem.target = self; - NSMenu *colorInsertionModeMenu = [[NSMenu alloc] initWithTitle:@"Color Insertion Mode"]; - [colorInsertionModeItem setSubmenu:colorInsertionModeMenu]; - [[colorInsertionModeItem submenu] addItem:colorInsertionModeUIItem]; - [[colorInsertionModeItem submenu] addItem:colorInsertionModeNSItem]; - [[editMenuItem submenu] addItem:colorInsertionModeItem]; + NSMenu *colorInsertionModeMenu = [NSMenu.alloc initWithTitle:@"Color Insertion Mode"]; + colorInsertionModeItem.submenu = colorInsertionModeMenu; + + [colorInsertionModeItem.submenu addItem:colorInsertionModeUIItem]; + [colorInsertionModeItem.submenu addItem:colorInsertionModeNSItem]; + [editMenuItem.submenu addItem:colorInsertionModeItem]; - NSMenuItem *insertColorMenuItem = [[NSMenuItem alloc] initWithTitle:@"Insert Color..." action:@selector(insertColor:) keyEquivalent:@""]; - [insertColorMenuItem setTarget:self]; - [[editMenuItem submenu] addItem:insertColorMenuItem]; + NSMenuItem *insertColorMenuItem = [NSMenuItem.alloc initWithTitle:@"Insert Color..." + action:@selector(insertColor:) + keyEquivalent:@""]; + insertColorMenuItem.target = self; + [editMenuItem.submenu addItem:insertColorMenuItem]; } - BOOL highlightingEnabled = ![[NSUserDefaults standardUserDefaults] boolForKey:kOMColorHelperHighlightingDisabled]; - if (highlightingEnabled) { - [self activateColorHighlighting]; - } + [kUSRDEFS boolForKey:kOMColorHelperHighlightingDisabled] ?: + [self activateColorHighlighting]; } #pragma mark - Preferences -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem -{ - if ([menuItem action] == @selector(insertColor:)) { - NSResponder *firstResponder = [[NSApp keyWindow] firstResponder]; - return ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [firstResponder isKindOfClass:[NSTextView class]]); - } else if ([menuItem action] == @selector(toggleColorHighlightingEnabled:)) { - BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:kOMColorHelperHighlightingDisabled]; - [menuItem setState:enabled ? NSOffState : NSOnState]; - return YES; - } else if ([menuItem action] == @selector(selectNSColorInsertionMode:)) { - [menuItem setState:[[NSUserDefaults standardUserDefaults] integerForKey:kOMColorHelperInsertionMode] == 1 ? NSOnState : NSOffState]; - } else if ([menuItem action] == @selector(selectUIColorInsertionMode:)) { - [menuItem setState:[[NSUserDefaults standardUserDefaults] integerForKey:kOMColorHelperInsertionMode] == 0 ? NSOnState : NSOffState]; +- (BOOL) validateMenuItem:(NSMenuItem *)menuItem { + + + if (menuItem.action == @selector(insertColor:)) { + NSResponder *firstResponder = [NSApp keyWindow].firstResponder; + return [firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && + [firstResponder isKindOfClass:NSTextView.class]; + + } else if (menuItem.action == @selector(toggleColorHighlightingEnabled:)) { + + BOOL enabled = [kUSRDEFS boolForKey:kOMColorHelperHighlightingDisabled]; + return [menuItem setState:enabled ? NSOffState : NSOnState], YES; + + } else if (menuItem.action == @selector(selectNSColorInsertionMode:)) { + + menuItem.state = [kUSRDEFS integerForKey:kOMColorHelperInsertionMode] == 1 + ? NSOnState : NSOffState; + + } else if (menuItem.action == @selector(selectUIColorInsertionMode:)) { + + menuItem.state = ![kUSRDEFS integerForKey:kOMColorHelperInsertionMode] + ? NSOnState : NSOffState; } return YES; } -- (void)selectNSColorInsertionMode:(id)sender -{ - [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:kOMColorHelperInsertionMode]; +- (void) selectNSColorInsertionMode:_ { + + + [kUSRDEFS setInteger:1 forKey:kOMColorHelperInsertionMode]; } -- (void)selectUIColorInsertionMode:(id)sender -{ - [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kOMColorHelperInsertionMode]; +- (void) selectUIColorInsertionMode:_ { + + + [kUSRDEFS setInteger:0 forKey:kOMColorHelperInsertionMode]; } -- (void)toggleColorHighlightingEnabled:(id)sender -{ - BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:kOMColorHelperHighlightingDisabled]; - [[NSUserDefaults standardUserDefaults] setBool:!enabled forKey:kOMColorHelperHighlightingDisabled]; - if (enabled) { - [self activateColorHighlighting]; - } else { - [self deactivateColorHighlighting]; - } +- (void) toggleColorHighlightingEnabled:_ { + + + BOOL enabled = [kUSRDEFS boolForKey:kOMColorHelperHighlightingDisabled]; + [kUSRDEFS setBool:!enabled forKey:kOMColorHelperHighlightingDisabled]; + + enabled ? [self activateColorHighlighting] : [self deactivateColorHighlighting]; } -- (void)activateColorHighlighting -{ - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionDidChange:) name:NSTextViewDidChangeSelectionNotification object:nil]; +- (void) activateColorHighlighting { + + + [kNCENTER addObserver:self selector:@selector(selectionDidChange:) name:NSTextViewDidChangeSelectionNotification object:nil]; if (!self.textView) { NSResponder *firstResponder = [[NSApp keyWindow] firstResponder]; if ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [firstResponder isKindOfClass:[NSTextView class]]) { self.textView = (NSTextView *)firstResponder; } } - if (self.textView) { - NSNotification *notification = [NSNotification notificationWithName:NSTextViewDidChangeSelectionNotification object:self.textView]; - [self selectionDidChange:notification]; - - } + if (self.textView) + [self selectionDidChange:[NSNotification notificationWithName:NSTextViewDidChangeSelectionNotification + object:self.textView]]; } -- (void)deactivateColorHighlighting -{ - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSTextViewDidChangeSelectionNotification object:nil]; +- (void) deactivateColorHighlighting { + + + [kNCENTER removeObserver:self name:NSTextViewDidChangeSelectionNotification object:nil]; [self dismissColorWell]; //self.textView = nil; } #pragma mark - Color Insertion -- (void)insertColor:(id)sender -{ +- (void) insertColor:_ { + if (!self.textView) { - NSResponder *firstResponder = [[NSApp keyWindow] firstResponder]; - if ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [firstResponder isKindOfClass:[NSTextView class]]) { + + NSResponder *firstResponder = [NSApp keyWindow].firstResponder; + if ([firstResponder isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && + [firstResponder isKindOfClass:NSTextView.class]) + self.textView = (NSTextView *)firstResponder; - } else { - NSBeep(); - return; - } + + else return NSBeep(); } - if ([[NSUserDefaults standardUserDefaults] boolForKey:kOMColorHelperHighlightingDisabled]) { + if ([kUSRDEFS boolForKey:kOMColorHelperHighlightingDisabled]) { //Inserting a color implicitly activates color highlighting: - [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kOMColorHelperHighlightingDisabled]; + [kUSRDEFS setBool:NO forKey:kOMColorHelperHighlightingDisabled]; [self activateColorHighlighting]; } + [self.textView.undoManager beginUndoGrouping]; - NSInteger insertionMode = [[NSUserDefaults standardUserDefaults] integerForKey:kOMColorHelperInsertionMode]; - if (insertionMode == 0) { - [self.textView insertText:@"[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]" replacementRange:self.textView.selectedRange]; - } else { - [self.textView insertText:@"[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0]" replacementRange:self.textView.selectedRange]; - } + + NSInteger insertionMode = [kUSRDEFS integerForKey:kOMColorHelperInsertionMode]; + + !insertionMode ? [self.textView insertText:@"[UIColor colorWithRed:1 green:0 blue:0 alpha:1]" + replacementRange:self.textView.selectedRange] + : [self.textView insertText:@"[NSColor colorWithCalibratedRed:1 green:0 blue:0 alpha:1]" + replacementRange:self.textView.selectedRange]; + [self.textView.undoManager endUndoGrouping]; [self performSelector:@selector(activateColorWell) withObject:nil afterDelay:0.0]; } -- (void)activateColorWell -{ +- (void) activateColorWell { + + [self.colorWell activate:YES]; } #pragma mark - Text Selection Handling -- (void)selectionDidChange:(NSNotification *)notification -{ - if ([[notification object] isKindOfClass:NSClassFromString(@"DVTSourceTextView")] && [[notification object] isKindOfClass:[NSTextView class]]) { - self.textView = (NSTextView *)[notification object]; - - BOOL disabled = [[NSUserDefaults standardUserDefaults] boolForKey:kOMColorHelperHighlightingDisabled]; - if (disabled) return; - - NSArray *selectedRanges = [self.textView selectedRanges]; - if (selectedRanges.count >= 1) { - NSRange selectedRange = [[selectedRanges objectAtIndex:0] rangeValue]; - NSString *text = self.textView.textStorage.string; - NSRange lineRange = [text lineRangeForRange:selectedRange]; - NSRange selectedRangeInLine = NSMakeRange(selectedRange.location - lineRange.location, selectedRange.length); - NSString *line = [text substringWithRange:lineRange]; - - NSRange colorRange = NSMakeRange(NSNotFound, 0); - OMColorType colorType = OMColorTypeNone; - NSColor *matchedColor = [self colorInText:line selectedRange:selectedRangeInLine type:&colorType matchedRange:&colorRange]; - - if (matchedColor) { - NSColor *backgroundColor = [self.textView.backgroundColor colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; - CGFloat r = 1.0; CGFloat g = 1.0; CGFloat b = 1.0; - [backgroundColor getRed:&r green:&g blue:&b alpha:NULL]; - CGFloat backgroundLuminance = (r + g + b) / 3.0; - - NSColor *strokeColor = (backgroundLuminance > 0.5) ? [NSColor colorWithCalibratedWhite:0.2 alpha:1.0] : [NSColor whiteColor]; - - self.selectedColorType = colorType; - self.colorWell.color = matchedColor; - self.colorWell.strokeColor = strokeColor; - - self.selectedColorRange = NSMakeRange(colorRange.location + lineRange.location, colorRange.length); - NSRect selectionRectOnScreen = [self.textView firstRectForCharacterRange:self.selectedColorRange]; - NSRect selectionRectInWindow = [self.textView.window convertRectFromScreen:selectionRectOnScreen]; - NSRect selectionRectInView = [self.textView convertRect:selectionRectInWindow fromView:nil]; - NSRect colorWellRect = NSMakeRect(NSMaxX(selectionRectInView) - 49, NSMinY(selectionRectInView) - selectionRectInView.size.height - 2, 50, selectionRectInView.size.height + 2); - self.colorWell.frame = NSIntegralRect(colorWellRect); - [self.textView addSubview:self.colorWell]; - self.colorFrameView.frame = NSInsetRect(NSIntegralRect(selectionRectInView), -1, -1); - - self.colorFrameView.color = strokeColor; - - [self.textView addSubview:self.colorFrameView]; - } else { - [self dismissColorWell]; - } - } else { - [self dismissColorWell]; - } - } +- (void) selectionDidChange:(NSNotification *)_ { + + if (![_.object isKindOfClass:NSClassFromString(@"DVTSourceTextView")] || + ![_.object isKindOfClass:NSTextView.class]) return; + + self.textView = (NSTextView *)_.object; + + BOOL disabled = [kUSRDEFS boolForKey:kOMColorHelperHighlightingDisabled]; + if (disabled) return; + + NSArray *selectedRanges = self.textView.selectedRanges; + + if (selectedRanges.count < 1) return [self dismissColorWell]; + + NSRange selectedRange = [selectedRanges[0] rangeValue]; + NSString *text = self.textView.textStorage.string; + NSRange lineRange = [text lineRangeForRange:selectedRange]; + NSRange selectedRangeInLine = NSMakeRange(selectedRange.location - lineRange.location, + selectedRange.length); + NSString *line = [text substringWithRange:lineRange]; + NSRange colorRange = NSMakeRange(NSNotFound, 0); + OMColorType colorType = OMColorTypeNone; + NSColor *matchedColor = [self colorInText:line selectedRange:selectedRangeInLine + type:&colorType matchedRange:&colorRange]; + + if (!matchedColor) return [self dismissColorWell]; + + NSColor *backgroundColor = [self.textView.backgroundColor + colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; + + CGFloat r = 1, g = 1, b = 1; [backgroundColor getRed:&r green:&g blue:&b alpha:NULL]; + + CGFloat backgroundLuminance = (r + g + b) / 3.0; + + NSColor *strokeColor = (backgroundLuminance > 0.5) ? [NSColor colorWithCalibratedWhite:.2 alpha:1] + : NSColor.whiteColor; + self.selectedColorType = colorType; + self.colorWell.color = matchedColor; + self.colorWell.strokeColor = strokeColor; + + self.selectedColorRange = NSMakeRange(colorRange.location + lineRange.location, colorRange.length); + NSRect selectRectOnScrn = [self.textView firstRectForCharacterRange:self.selectedColorRange], + selectRectInWin = [self.textView.window convertRectFromScreen:selectRectOnScrn], + selectRectInView = [self.textView convertRect:selectRectInWin fromView:nil], + colorWellRect = NSMakeRect(NSMaxX(selectRectInView) - 49, + NSMinY(selectRectInView) - selectRectInView.size.height - 2, + 50, selectRectInView.size.height + 2); + self.colorWell.frame = NSIntegralRect(colorWellRect); + [self.textView addSubview:self.colorWell]; + self.colorFrameView.frame = NSInsetRect(NSIntegralRect(selectRectInView), -1, -1); + self.colorFrameView.color = strokeColor; + + [self.textView addSubview:self.colorFrameView]; } -- (void)dismissColorWell -{ +- (void) dismissColorWell { + + if (self.colorWell.isActive) { [self.colorWell deactivate]; - [[NSColorPanel sharedColorPanel] orderOut:nil]; + [NSColorPanel.sharedColorPanel orderOut:nil]; } - [self.colorWell removeFromSuperview]; + [self.colorWell removeFromSuperview]; [self.colorFrameView removeFromSuperview]; self.selectedColorRange = NSMakeRange(NSNotFound, 0); - self.selectedColorType = OMColorTypeNone; + self.selectedColorType = OMColorTypeNone; } -- (void)colorDidChange:(id)sender -{ - if (self.selectedColorRange.location == NSNotFound) { - return; - } +- (void) colorDidChange:_ { + + if (self.selectedColorRange.location == NSNotFound) return; + NSString *colorString = [self colorStringForColor:self.colorWell.color withType:self.selectedColorType]; - if (colorString) { - [self.textView.undoManager beginUndoGrouping]; - [self.textView insertText:colorString replacementRange:self.selectedColorRange]; - [self.textView.undoManager endUndoGrouping]; - } + if (!colorString) return; + + [self.textView.undoManager beginUndoGrouping]; + [self.textView insertText:colorString replacementRange:self.selectedColorRange]; + [self.textView.undoManager endUndoGrouping]; } #pragma mark - View Initialization -- (OMPlainColorWell *)colorWell -{ +- (OMPlainColorWell*) colorWell { + + if (!_colorWell) { _colorWell = [[OMPlainColorWell alloc] initWithFrame:NSMakeRect(0, 0, 50, 30)]; [_colorWell setTarget:self]; @@ -281,8 +309,9 @@ - (OMPlainColorWell *)colorWell return _colorWell; } -- (OMColorFrameView *)colorFrameView -{ +- (OMColorFrameView*) colorFrameView { + + if (!_colorFrameView) { _colorFrameView = [[OMColorFrameView alloc] initWithFrame:NSZeroRect]; } @@ -291,8 +320,10 @@ - (OMColorFrameView *)colorFrameView #pragma mark - Color String Parsing -- (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange type:(OMColorType *)type matchedRange:(NSRangePointer)matchedRange -{ +- (NSColor*) colorInText:(NSString*)text selectedRange:(NSRange)selectedRange + type:(OMColorType*)type matchedRange:(NSRangePointer)matchedRange { + + __block NSColor *foundColor = nil; __block NSRange foundColorRange = NSMakeRange(NSNotFound, 0); __block OMColorType foundColorType = OMColorTypeNone; @@ -300,26 +331,25 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t [_rgbaUIColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { NSRange colorRange = [result range]; if (selectedRange.location >= colorRange.location && NSMaxRange(selectedRange) <= NSMaxRange(colorRange)) { - NSString *typeIndicator = [text substringWithRange:[result rangeAtIndex:1]]; - if ([typeIndicator rangeOfString:@"init"].location != NSNotFound) { - foundColorType = OMColorTypeUIRGBAInit; - } else { - foundColorType = OMColorTypeUIRGBA; - } + + NSString *typeIndicator = [text substringWithRange:[result rangeAtIndex:1]]; + + foundColorType = [typeIndicator rangeOfString:@"init"].location != NSNotFound ? OMColorTypeUIRGBAInit + : OMColorTypeUIRGBA; // [UIColor colorWithRed:128 / 255.0 green:10 / 255 blue:123/255 alpha:128 /255] - double red = [[text substringWithRange:[result rangeAtIndex:2]] doubleValue]; - red = [self dividedValue:red withDivisorRange:[result rangeAtIndex:3] inString:text]; + double red = [text substringWithRange:[result rangeAtIndex:2]].doubleValue; + red = [self dividedValue:red withDivisorRange:[result rangeAtIndex:3] inString:text]; - double green = [[text substringWithRange:[result rangeAtIndex:4]] doubleValue]; - green = [self dividedValue:green withDivisorRange:[result rangeAtIndex:5] inString:text]; + double green = [text substringWithRange:[result rangeAtIndex:4]].doubleValue; + green = [self dividedValue:green withDivisorRange:[result rangeAtIndex:5] inString:text]; - double blue = [[text substringWithRange:[result rangeAtIndex:6]] doubleValue]; - blue = [self dividedValue:blue withDivisorRange:[result rangeAtIndex:7] inString:text]; + double blue = [text substringWithRange:[result rangeAtIndex:6]].doubleValue; + blue = [self dividedValue:blue withDivisorRange:[result rangeAtIndex:7] inString:text]; - double alpha = [[text substringWithRange:[result rangeAtIndex:8]] doubleValue]; - alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:9] inString:text]; + double alpha = [text substringWithRange:[result rangeAtIndex:8]].doubleValue; + alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:9] inString:text]; foundColor = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha]; foundColorRange = colorRange; @@ -328,180 +358,184 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t }]; if (!foundColor) { - [_whiteUIColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { - NSRange colorRange = [result range]; - if (selectedRange.location >= colorRange.location && NSMaxRange(selectedRange) <= NSMaxRange(colorRange)) { - NSString *typeIndicator = [text substringWithRange:[result rangeAtIndex:1]]; - if ([typeIndicator rangeOfString:@"init"].location != NSNotFound) { - foundColorType = OMColorTypeUIWhiteInit; - } else { - foundColorType = OMColorTypeUIWhite; - } - double white = [[text substringWithRange:[result rangeAtIndex:2]] doubleValue]; - white = [self dividedValue:white withDivisorRange:[result rangeAtIndex:3] inString:text]; - - double alpha = [[text substringWithRange:[result rangeAtIndex:4]] doubleValue]; - alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:5] inString:text]; - - foundColor = [NSColor colorWithCalibratedWhite:white alpha:alpha]; - foundColorRange = colorRange; - *stop = YES; - } + + [_whiteUIColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) + usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags _, BOOL *stop) { + + NSRange colorRange = result.range; + if (selectedRange.location < colorRange.location || NSMaxRange(selectedRange) > NSMaxRange(colorRange)) + return; + + NSString *typeIndicator = [text substringWithRange:[result rangeAtIndex:1]]; + foundColorType = [typeIndicator rangeOfString:@"init"].location != NSNotFound ? OMColorTypeUIWhiteInit + : OMColorTypeUIWhite; + + double white = [[text substringWithRange:[result rangeAtIndex:2]] doubleValue]; + white = [self dividedValue:white withDivisorRange:[result rangeAtIndex:3] inString:text]; + + double alpha = [[text substringWithRange:[result rangeAtIndex:4]] doubleValue]; + alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:5] inString:text]; + + foundColor = [NSColor colorWithCalibratedWhite:white alpha:alpha]; + + foundColorRange = colorRange; *stop = YES; + }]; } if (!foundColor) { - [_constantColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { - NSRange colorRange = [result range]; - if (selectedRange.location >= colorRange.location && NSMaxRange(selectedRange) <= NSMaxRange(colorRange)) { - NSString *NS_UI = [text substringWithRange:[result rangeAtIndex:1]]; - NSString *colorName = [text substringWithRange:[result rangeAtIndex:2]]; - foundColor = [_constantColorsByName objectForKey:colorName]; - foundColorRange = colorRange; - if ([NS_UI isEqualToString:@"UI"]) { - foundColorType = OMColorTypeUIConstant; - } else { - foundColorType = OMColorTypeNSConstant; - } - *stop = YES; - } + + [_constantColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) + usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags _, BOOL *stop) { + + NSRange colorRange = result.range; + if (selectedRange.location < colorRange.location || NSMaxRange(selectedRange) > NSMaxRange(colorRange)) + return; + + NSString *NS_UI = [text substringWithRange:[result rangeAtIndex:1]]; + NSString *colorName = [text substringWithRange:[result rangeAtIndex:2]]; + foundColor = _constantColorsByName[colorName]; + foundColorRange = colorRange; + foundColorType = [NS_UI isEqualToString:@"UI"] ? OMColorTypeUIConstant + : OMColorTypeNSConstant; *stop = YES; }]; } if (!foundColor) { - [_rgbaNSColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { - NSRange colorRange = [result range]; - if (selectedRange.location >= colorRange.location && NSMaxRange(selectedRange) <= NSMaxRange(colorRange)) { - NSString *deviceOrCalibrated = [text substringWithRange:[result rangeAtIndex:1]]; - if ([deviceOrCalibrated isEqualToString:@"Device"]) { - foundColorType = OMColorTypeNSRGBADevice; - } else { - foundColorType = OMColorTypeNSRGBACalibrated; - } - double red = [[text substringWithRange:[result rangeAtIndex:2]] doubleValue]; - red = [self dividedValue:red withDivisorRange:[result rangeAtIndex:3] inString:text]; - - double green = [[text substringWithRange:[result rangeAtIndex:4]] doubleValue]; - green = [self dividedValue:green withDivisorRange:[result rangeAtIndex:5] inString:text]; - - double blue = [[text substringWithRange:[result rangeAtIndex:6]] doubleValue]; - blue = [self dividedValue:blue withDivisorRange:[result rangeAtIndex:7] inString:text]; - - double alpha = [[text substringWithRange:[result rangeAtIndex:8]] doubleValue]; - alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:9] inString:text]; - - if (foundColorType == OMColorTypeNSRGBACalibrated) { - foundColor = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha]; - } else { - foundColor = [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha]; - } - foundColorRange = colorRange; - *stop = YES; - } + + [_rgbaNSColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) + usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags _, BOOL *stop) { + + NSRange colorRange = result.range; + + if (selectedRange.location < colorRange.location || NSMaxRange(selectedRange) > NSMaxRange(colorRange)) + return; + NSString *deviceOrCalibrated = [text substringWithRange:[result rangeAtIndex:1]]; + if ([deviceOrCalibrated isEqualToString:@"Device"]) { + foundColorType = OMColorTypeNSRGBADevice; + } else { + foundColorType = OMColorTypeNSRGBACalibrated; + } + double red = [text substringWithRange:[result rangeAtIndex:2]].doubleValue; + red = [self dividedValue:red withDivisorRange:[result rangeAtIndex:3] inString:text]; + + double green = [text substringWithRange:[result rangeAtIndex:4]].doubleValue; + green = [self dividedValue:green withDivisorRange:[result rangeAtIndex:5] inString:text]; + + double blue = [text substringWithRange:[result rangeAtIndex:6]].doubleValue; + blue = [self dividedValue:blue withDivisorRange:[result rangeAtIndex:7] inString:text]; + + double alpha = [text substringWithRange:[result rangeAtIndex:8]].doubleValue; + alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:9] inString:text]; + + foundColor = foundColorType == OMColorTypeNSRGBACalibrated ? + [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha] : + [NSColor colorWithDeviceRed:red green:green blue:blue alpha:alpha] ; + + foundColorRange = colorRange; *stop = YES; }]; } if (!foundColor) { - [_whiteNSColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { - NSRange colorRange = [result range]; - if (selectedRange.location >= colorRange.location && NSMaxRange(selectedRange) <= NSMaxRange(colorRange)) { - NSString *deviceOrCalibrated = [text substringWithRange:[result rangeAtIndex:1]]; - double white = [[text substringWithRange:[result rangeAtIndex:2]] doubleValue]; - white = [self dividedValue:white withDivisorRange:[result rangeAtIndex:3] inString:text]; - - double alpha = [[text substringWithRange:[result rangeAtIndex:4]] doubleValue]; - alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:5] inString:text]; - - if ([deviceOrCalibrated isEqualToString:@"Device"]) { - foundColor = [NSColor colorWithDeviceWhite:white alpha:alpha]; - foundColorType = OMColorTypeNSWhiteDevice; - } else { - foundColor = [NSColor colorWithCalibratedWhite:white alpha:alpha]; - foundColorType = OMColorTypeNSWhiteCalibrated; - } - foundColorRange = colorRange; - *stop = YES; - } + + [_whiteNSColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) + usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags _, BOOL *stop) { + + NSRange colorRange = result.range; + + if (selectedRange.location < colorRange.location || NSMaxRange(selectedRange) > NSMaxRange(colorRange)) + return; + + NSString *deviceOrCalibrated = [text substringWithRange:[result rangeAtIndex:1]]; + double white = [text substringWithRange:[result rangeAtIndex:2]].doubleValue; + white = [self dividedValue:white withDivisorRange:[result rangeAtIndex:3] inString:text]; + + double alpha = [text substringWithRange:[result rangeAtIndex:4]].doubleValue; + alpha = [self dividedValue:alpha withDivisorRange:[result rangeAtIndex:5] inString:text]; + + if ([deviceOrCalibrated isEqualToString:@"Device"]) { + foundColor = [NSColor colorWithDeviceWhite:white alpha:alpha]; + foundColorType = OMColorTypeNSWhiteDevice; + } else { + foundColor = [NSColor colorWithCalibratedWhite:white alpha:alpha]; + foundColorType = OMColorTypeNSWhiteCalibrated; + } + foundColorRange = colorRange; *stop = YES; }]; } - if (foundColor) { - if (matchedRange != NULL) { - *matchedRange = foundColorRange; - } - if (type != NULL) { - *type = foundColorType; - } - return foundColor; - } - - return nil; + if (!foundColor) return nil; + + if (matchedRange != NULL) *matchedRange = foundColorRange; + if (type != NULL) *type = foundColorType; + + return foundColor; } -- (double)dividedValue:(double)value withDivisorRange:(NSRange)divisorRange inString:(NSString *)text -{ - if (divisorRange.location != NSNotFound) { - double divisor = [[[text substringWithRange:divisorRange] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/ "]] doubleValue]; - if (divisor != 0) { - value /= divisor; - } - } +- (double) dividedValue:(double)value withDivisorRange:(NSRange)divisorRange inString:(NSString *)text { + + if (divisorRange.location == NSNotFound) return value; + double divisor = [[[text substringWithRange:divisorRange] + stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/ "]] + doubleValue]; + if (divisor) value /= divisor; return value; } -- (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorType -{ +- (NSString*) colorStringForColor:(NSColor*)color withType:(OMColorType)colorType { + + NSString *colorString = nil; - CGFloat red = -1.0; CGFloat green = -1.0; CGFloat blue = -1.0; CGFloat alpha = -1.0; + CGFloat r = -1.0; CGFloat g = -1.0; CGFloat b = -1.0; CGFloat a = -1.0; color = [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]]; - [color getRed:&red green:&green blue:&blue alpha:&alpha]; + [color getRed:&r green:&g blue:&b alpha:&a]; - if (red >= 0) { - for (NSString *colorName in _constantColorsByName) { - NSColor *constantColor = [_constantColorsByName objectForKey:colorName]; - if ([constantColor isEqual:color]) { - if (OMColorTypeIsNSColor(colorType)) { - colorString = [NSString stringWithFormat:@"[NSColor %@Color]", colorName]; - } else { - colorString = [NSString stringWithFormat:@"[UIColor %@Color]", colorName]; - } - break; - } - } - if (!colorString) { - if (fabs(red - green) < 0.001 && fabs(green - blue) < 0.001) { - if (colorType == OMColorTypeUIRGBA || colorType == OMColorTypeUIWhite || colorType == OMColorTypeUIConstant) { - colorString = [NSString stringWithFormat:@"[UIColor colorWithWhite:%.3f alpha:%.3f]", red, alpha]; - } else if (colorType == OMColorTypeUIRGBAInit || colorType == OMColorTypeUIWhiteInit) { - colorString = [NSString stringWithFormat:@"[[UIColor alloc] initWithWhite:%.3f alpha:%.3f]", red, alpha]; - } - else if (colorType == OMColorTypeNSConstant || colorType == OMColorTypeNSRGBACalibrated || colorType == OMColorTypeNSWhiteCalibrated) { - colorString = [NSString stringWithFormat:@"[NSColor colorWithCalibratedWhite:%.3f alpha:%.3f]", red, alpha]; - } else if (colorType == OMColorTypeNSRGBADevice || colorType == OMColorTypeNSWhiteDevice) { - colorString = [NSString stringWithFormat:@"[NSColor colorWithDeviceWhite:%.3f alpha:%.3f]", red, alpha]; - } - } else { - if (colorType == OMColorTypeUIRGBA || colorType == OMColorTypeUIWhite || colorType == OMColorTypeUIConstant) { - colorString = [NSString stringWithFormat:@"[UIColor colorWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; - } else if (colorType == OMColorTypeUIRGBAInit || colorType == OMColorTypeUIWhiteInit) { - colorString = [NSString stringWithFormat:@"[[UIColor alloc] initWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; - } - else if (colorType == OMColorTypeNSConstant || colorType == OMColorTypeNSRGBACalibrated || colorType == OMColorTypeNSWhiteCalibrated) { - colorString = [NSString stringWithFormat:@"[NSColor colorWithCalibratedRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; - } else if (colorType == OMColorTypeNSRGBADevice || colorType == OMColorTypeNSWhiteDevice) { - colorString = [NSString stringWithFormat:@"[NSColor colorWithDeviceRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; - } - } - } - } - return colorString; + if (r< 0) return colorString; + + for (NSString *colorName in _constantColorsByName) { + + NSColor *constantColor = _constantColorsByName[colorName]; + if ([constantColor isEqual:color]) { + + colorString = OMColorTypeIsNSColor(colorType) ? [NSString stringWithFormat:@"[NSColor %@Color]", colorName] + : [NSString stringWithFormat:@"[UIColor %@Color]", colorName]; + break; + } + } + + return colorString ?: fabs(r - g) < 0.001 && fabs(g - b) < 0.001 ? + + colorType == OMColorTypeUIRGBA || + colorType == OMColorTypeUIWhite || + colorType == OMColorTypeUIConstant + ? [NSString stringWithFormat:@"[UIColor colorWithWhite:%.3f alpha:%.3f]",r,a] + : colorType == OMColorTypeUIRGBAInit || + colorType == OMColorTypeUIWhiteInit + ? [NSString stringWithFormat:@"[[UIColor alloc] initWithWhite:%.3f alpha:%.3f]",r,a] + : colorType == OMColorTypeNSConstant || + colorType == OMColorTypeNSRGBACalibrated || + colorType == OMColorTypeNSWhiteCalibrated + ? [NSString stringWithFormat:@"[NSColor colorWithCalibratedWhite:%.3f alpha:%.3f]",r,a] + : colorType == OMColorTypeNSRGBADevice || + colorType == OMColorTypeNSWhiteDevice + ? [NSString stringWithFormat:@"[NSColor colorWithDeviceWhite:%.3f alpha:%.3f]",r,a] + : colorType == OMColorTypeUIRGBA || + colorType == OMColorTypeUIWhite || + colorType == OMColorTypeUIConstant + ? [NSString stringWithFormat:@"[UIColor colorWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]",r,g,b,a] + : colorType == OMColorTypeUIRGBAInit || + colorType == OMColorTypeUIWhiteInit + ? [NSString stringWithFormat:@"[[UIColor alloc] initWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]",r,g,b,a] + : colorType == OMColorTypeNSConstant || + colorType == OMColorTypeNSRGBACalibrated || + colorType == OMColorTypeNSWhiteCalibrated + ? [NSString stringWithFormat:@"[NSColor colorWithCalibratedRed:%.3f green:%.3f blue:%.3f alpha:%.3f]",r,g,b,a] + : colorType == OMColorTypeNSRGBADevice || + colorType == OMColorTypeNSWhiteDevice + ? [NSString stringWithFormat:@"[NSColor colorWithDeviceRed:%.3f green:%.3f blue:%.3f alpha:%.3f]",r,g,b,a] + : colorString : colorString; } -#pragma mark - - -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} +- (void) dealloc { [kNCENTER removeObserver:self]; } @end diff --git a/Classes/OMColorViews.h b/Classes/OMColorViews.h new file mode 100644 index 0000000..59eaecd --- /dev/null +++ b/Classes/OMColorViews.h @@ -0,0 +1,16 @@ + +// OMColorFrameView.h OMColorHelper Created by Ole Zorn on 09/07/12. + +@import AppKit; + +@interface OMColorFrameView : NSView + +@property (nonatomic) NSColor *color; + +@end + +@interface OMPlainColorWell : NSColorWell + +@property (nonatomic) NSColor *strokeColor; + +@end diff --git a/Classes/OMColorViews.m b/Classes/OMColorViews.m new file mode 100644 index 0000000..195efbc --- /dev/null +++ b/Classes/OMColorViews.m @@ -0,0 +1,47 @@ + +// OMColorFrameView.m OMColorHelper Created by Ole Zorn on 09/07/12. + +#import "OMColorViews.h" + +@implementation OMColorFrameView + +- (void)drawRect:(NSRect)_ { + + [self.color setStroke]; [NSBezierPath strokeRect:NSInsetRect(self.bounds, 0.5, 0.5)]; +} + +- (void)setColor:(NSColor *)color +{ + if (color == _color) return; _color = color; [self setNeedsDisplay:YES]; +} + +@end + +@implementation OMPlainColorWell + +- (void)drawRect:(NSRect)_ { + + [NSGraphicsContext saveGraphicsState]; + NSRect fillRect = NSMakeRect(0, -5, self.bounds.size.width, self.bounds.size.height + 5); + NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:fillRect xRadius:5 yRadius:5]; + [path addClip]; + [self drawWellInside:self.bounds]; + [NSGraphicsContext restoreGraphicsState]; + + if (!self.strokeColor) return; + + NSRect strokeRect = NSInsetRect(NSMakeRect(0, -5, self.bounds.size.width, self.bounds.size.height + 5),.5,.5); + NSBezierPath *strokePath = [NSBezierPath bezierPathWithRoundedRect:strokeRect xRadius:5 yRadius:5]; + [self.strokeColor setStroke]; + [strokePath stroke]; +} + +- (void) deactivate { [super deactivate]; [NSColorPanel.sharedColorPanel orderOut:nil]; } + +- (void)setStrokeColor:(NSColor *)strokeColor { + + if (strokeColor == _strokeColor) return; _strokeColor = strokeColor; [self setNeedsDisplay:YES]; +} + +@end + diff --git a/Classes/OMPlainColorWell.h b/Classes/OMPlainColorWell.h deleted file mode 100644 index 32254b4..0000000 --- a/Classes/OMPlainColorWell.h +++ /dev/null @@ -1,18 +0,0 @@ -// -// OMPlainColorWell.h -// OMColorHelper -// -// Created by Ole Zorn on 09/07/12. -// -// - -#import - -@interface OMPlainColorWell : NSColorWell { - - NSColor *_strokeColor; -} - -@property (nonatomic, strong) NSColor *strokeColor; - -@end diff --git a/Classes/OMPlainColorWell.m b/Classes/OMPlainColorWell.m deleted file mode 100644 index c9c69a4..0000000 --- a/Classes/OMPlainColorWell.m +++ /dev/null @@ -1,45 +0,0 @@ -// -// OMPlainColorWell.m -// OMColorHelper -// -// Created by Ole Zorn on 09/07/12. -// -// - -#import "OMPlainColorWell.h" - -@implementation OMPlainColorWell - -@synthesize strokeColor=_strokeColor; - -- (void)drawRect:(NSRect)dirtyRect -{ - [NSGraphicsContext saveGraphicsState]; - NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0, -5, self.bounds.size.width, self.bounds.size.height + 5) xRadius:5.0 yRadius:5.0]; - [path addClip]; - [self drawWellInside:self.bounds]; - [NSGraphicsContext restoreGraphicsState]; - - if (self.strokeColor) { - NSBezierPath *strokePath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(NSMakeRect(0, -5, self.bounds.size.width, self.bounds.size.height + 5), 0.5, 0.5) xRadius:5.0 yRadius:5.0]; - [self.strokeColor setStroke]; - [strokePath stroke]; - } -} - -- (void)deactivate -{ - [super deactivate]; - [[NSColorPanel sharedColorPanel] orderOut:nil]; -} - -- (void)setStrokeColor:(NSColor *)strokeColor -{ - if (strokeColor != _strokeColor) { - _strokeColor = strokeColor; - [self setNeedsDisplay:YES]; - } -} - - -@end diff --git a/OMColorSense.xcodeproj/project.pbxproj b/OMColorSense.xcodeproj/project.pbxproj index 9bcef85..018bf4b 100755 --- a/OMColorSense.xcodeproj/project.pbxproj +++ b/OMColorSense.xcodeproj/project.pbxproj @@ -7,24 +7,17 @@ objects = { /* Begin PBXBuildFile section */ - 7F2EB89C145057F200E97A87 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7F2EB899145057EA00E97A87 /* AppKit.framework */; }; - 7F6EE2BF15FA6F3B00BA114A /* OMColorFrameView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F6EE2BE15FA6F3B00BA114A /* OMColorFrameView.m */; }; - 7FDADE3A15FA6CA400A847E3 /* OMPlainColorWell.m in Sources */ = {isa = PBXBuildFile; fileRef = 7FDADE3915FA6CA400A847E3 /* OMPlainColorWell.m */; }; - DA1B5D020E64686800921439 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 089C1672FE841209C02AAC07 /* Foundation.framework */; }; + 7F6EE2BF15FA6F3B00BA114A /* OMColorViews.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F6EE2BE15FA6F3B00BA114A /* OMColorViews.m */; }; DA37E2DC0E6291C8001BDFEF /* OMColorHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = DA37E2DB0E6291C8001BDFEF /* OMColorHelper.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 089C1672FE841209C02AAC07 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = ""; }; - 7F2B355F15FA59D000DB3249 /* OMColorHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OMColorHelper.h; sourceTree = ""; }; - 7F2EB899145057EA00E97A87 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; - 7F6EE2BD15FA6F3B00BA114A /* OMColorFrameView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OMColorFrameView.h; sourceTree = ""; }; - 7F6EE2BE15FA6F3B00BA114A /* OMColorFrameView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OMColorFrameView.m; sourceTree = ""; }; - 7FDADE3815FA6CA400A847E3 /* OMPlainColorWell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OMPlainColorWell.h; sourceTree = ""; }; - 7FDADE3915FA6CA400A847E3 /* OMPlainColorWell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OMPlainColorWell.m; sourceTree = ""; }; + 7F2B355F15FA59D000DB3249 /* OMColorHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = OMColorHelper.h; path = Classes/OMColorHelper.h; sourceTree = ""; }; + 7F6EE2BD15FA6F3B00BA114A /* OMColorViews.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OMColorViews.h; path = Classes/OMColorViews.h; sourceTree = ""; }; + 7F6EE2BE15FA6F3B00BA114A /* OMColorViews.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMColorViews.m; path = Classes/OMColorViews.m; sourceTree = ""; }; 8D5B49B6048680CD000E48DA /* OMColorSense.xcplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OMColorSense.xcplugin; sourceTree = BUILT_PRODUCTS_DIR; }; 8D5B49B7048680CD000E48DA /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - DA37E2DB0E6291C8001BDFEF /* OMColorHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OMColorHelper.m; sourceTree = ""; }; + DA37E2DB0E6291C8001BDFEF /* OMColorHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OMColorHelper.m; path = Classes/OMColorHelper.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -32,8 +25,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - DA1B5D020E64686800921439 /* Foundation.framework in Frameworks */, - 7F2EB89C145057F200E97A87 /* AppKit.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -43,52 +34,24 @@ 089C166AFE841209C02AAC07 /* QuietXcode */ = { isa = PBXGroup; children = ( - 7F411B0C15FABAC6002F77B6 /* Classes */, + 7F2B355F15FA59D000DB3249 /* OMColorHelper.h */, + DA37E2DB0E6291C8001BDFEF /* OMColorHelper.m */, + 7F6EE2BD15FA6F3B00BA114A /* OMColorViews.h */, + 7F6EE2BE15FA6F3B00BA114A /* OMColorViews.m */, 089C167CFE841241C02AAC07 /* Resources */, - 089C1671FE841209C02AAC07 /* Frameworks and Libraries */, - 19C28FB8FE9D52D311CA2CBB /* Products */, ); name = QuietXcode; sourceTree = ""; }; - 089C1671FE841209C02AAC07 /* Frameworks and Libraries */ = { - isa = PBXGroup; - children = ( - 7F2EB899145057EA00E97A87 /* AppKit.framework */, - 089C1672FE841209C02AAC07 /* Foundation.framework */, - ); - name = "Frameworks and Libraries"; - sourceTree = ""; - }; 089C167CFE841241C02AAC07 /* Resources */ = { isa = PBXGroup; children = ( + 8D5B49B6048680CD000E48DA /* OMColorSense.xcplugin */, 8D5B49B7048680CD000E48DA /* Info.plist */, ); name = Resources; sourceTree = ""; }; - 19C28FB8FE9D52D311CA2CBB /* Products */ = { - isa = PBXGroup; - children = ( - 8D5B49B6048680CD000E48DA /* OMColorSense.xcplugin */, - ); - name = Products; - sourceTree = ""; - }; - 7F411B0C15FABAC6002F77B6 /* Classes */ = { - isa = PBXGroup; - children = ( - 7F2B355F15FA59D000DB3249 /* OMColorHelper.h */, - DA37E2DB0E6291C8001BDFEF /* OMColorHelper.m */, - 7F6EE2BD15FA6F3B00BA114A /* OMColorFrameView.h */, - 7F6EE2BE15FA6F3B00BA114A /* OMColorFrameView.m */, - 7FDADE3815FA6CA400A847E3 /* OMPlainColorWell.h */, - 7FDADE3915FA6CA400A847E3 /* OMPlainColorWell.m */, - ); - path = Classes; - sourceTree = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -115,7 +78,7 @@ 089C1669FE841209C02AAC07 /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 0440; + LastUpgradeCheck = 0610; }; buildConfigurationList = 1DEB913E08733D840010E9CD /* Build configuration list for PBXProject "OMColorSense" */; compatibilityVersion = "Xcode 3.2"; @@ -142,8 +105,7 @@ buildActionMask = 2147483647; files = ( DA37E2DC0E6291C8001BDFEF /* OMColorHelper.m in Sources */, - 7FDADE3A15FA6CA400A847E3 /* OMPlainColorWell.m in Sources */, - 7F6EE2BF15FA6F3B00BA114A /* OMColorFrameView.m in Sources */, + 7F6EE2BF15FA6F3B00BA114A /* OMColorViews.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -154,7 +116,6 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = "$(ARCHS_STANDARD_64_BIT)"; CLANG_ENABLE_OBJC_ARC = YES; COMBINE_HIDPI_IMAGES = YES; COPY_PHASE_STRIP = NO; @@ -177,7 +138,7 @@ 1DEB913F08733D840010E9CD /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - ARCHS = "$(ARCHS_STANDARD_32_64_BIT)"; + CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = 1.0.1; GCC_C_LANGUAGE_STANDARD = c99; GCC_OPTIMIZATION_LEVEL = 0; @@ -185,6 +146,8 @@ GCC_WARN_UNUSED_VARIABLE = YES; INSTALL_PATH = "$(HOME)/Library/Application Support/Developer/Shared/Xcode/Plug-ins"; MACOSX_DEPLOYMENT_TARGET = 10.7; + ONLY_ACTIVE_ARCH = YES; + OTHER_CFLAGS = "-Wno-deprecated-declarations"; SDKROOT = macosx; }; name = Debug;