Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Classes/OMColorHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ typedef enum OMColorType {
OMColorTypeNSWhiteCalibrated, //[NSColor colorWithCalibratedWhite:0.5 alpha:1.0]
OMColorTypeNSWhiteDevice, //[NSColor colorWithDeviceWhite:0.5 alpha:1.0]
OMColorTypeNSConstant, //[NSColor redColor]

OMColorTypeCCRGBA, //[CCColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]
OMColorTypeCCRGBAInit, //[[CCColor alloc] initWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]
OMColorTypeCCWhite, //[CCColor colorWithWhite:1.0 alpha:1.0]
OMColorTypeCCWhiteInit, //[[CCColor alloc] initWithWhite:0.5 alpha:1.0]
OMColorTypeCCConstant, //[CCColor redColor]

} OMColorType;

BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTypeNSRGBACalibrated; }
BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTypeNSRGBACalibrated && colorType <= OMColorTypeNSConstant; }

BOOL OMColorTypeIsUIColor(OMColorType colorType) { return colorType < OMColorTypeNSRGBACalibrated; }

//TODO: Maybe support HSB and CMYK color types...

Expand Down
59 changes: 38 additions & 21 deletions Classes/OMColorHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ - (id)init
[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];
_rgbaUIColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*(?:UI|CC)Color\\s+colorWith|\\[\\s*\\[\\s*(?:UI|CC)Color\\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*(?:UI|CC)Color\\s+colorWith|\\[\\s*\\[\\s*(?:UI|CC)Color\\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];
_constantColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*(UI|NS|CC)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;
}
Expand Down Expand Up @@ -300,12 +300,13 @@ - (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]];
BOOL UI_Prefix = [typeIndicator rangeOfString:@"UI"].location != NSNotFound;
if ([typeIndicator rangeOfString:@"init"].location != NSNotFound) {
foundColorType = (UI_Prefix ? OMColorTypeUIRGBAInit : OMColorTypeCCRGBAInit);
} else {
foundColorType = (UI_Prefix ? OMColorTypeUIRGBA : OMColorTypeCCRGBA);
}

// [UIColor colorWithRed:128 / 255.0 green:10 / 255 blue:123/255 alpha:128 /255]

Expand All @@ -331,12 +332,14 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t
[_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;
}
NSString *typeIndicator = [text substringWithRange:[result rangeAtIndex:1]];
BOOL UI_Prefix = [typeIndicator rangeOfString:@"UI"].location != NSNotFound;
if ([typeIndicator rangeOfString:@"init"].location != NSNotFound) {
foundColorType = (UI_Prefix ? OMColorTypeUIWhiteInit : OMColorTypeCCWhiteInit);
} else {
foundColorType = (UI_Prefix ? OMColorTypeUIWhite : OMColorTypeCCWhite);
}

double white = [[text substringWithRange:[result rangeAtIndex:2]] doubleValue];
white = [self dividedValue:white withDivisorRange:[result rangeAtIndex:3] inString:text];

Expand All @@ -360,9 +363,11 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t
foundColorRange = colorRange;
if ([NS_UI isEqualToString:@"UI"]) {
foundColorType = OMColorTypeUIConstant;
} else {
} else if ([NS_UI isEqualToString:@"NS"]) {
foundColorType = OMColorTypeNSConstant;
}
} else {
foundColorType = OMColorTypeCCConstant;
}
*stop = YES;
}
}];
Expand Down Expand Up @@ -462,9 +467,11 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy
if ([constantColor isEqual:color]) {
if (OMColorTypeIsNSColor(colorType)) {
colorString = [NSString stringWithFormat:@"[NSColor %@Color]", colorName];
} else {
} else if (OMColorTypeIsUIColor(colorType)) {
colorString = [NSString stringWithFormat:@"[UIColor %@Color]", colorName];
}
} else {
colorString = [NSString stringWithFormat:@"[CCColor %@Color]", colorName];
}
break;
}
}
Expand All @@ -479,7 +486,11 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy
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 == OMColorTypeCCConstant || colorType == OMColorTypeCCRGBA || colorType == OMColorTypeCCWhite) {
colorString = [NSString stringWithFormat:@"[CCColor colorWithWhite:%.3f alpha:%.3f]", red, alpha];
} else if (colorType == OMColorTypeCCRGBAInit || colorType == OMColorTypeCCWhiteInit) {
colorString = [NSString stringWithFormat:@"[[CCColor alloc] initWithWhite:%.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];
Expand All @@ -490,7 +501,13 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy
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];
}
} else if (colorType == OMColorTypeCCConstant || colorType == OMColorTypeCCRGBA || colorType == OMColorTypeCCWhite) {
colorString = [NSString stringWithFormat:@"[CCColor colorWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha];

} else if (colorType == OMColorTypeCCRGBAInit || colorType == OMColorTypeCCWhiteInit) {
colorString = [NSString stringWithFormat:@"[[CCColor alloc] initWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha];
}

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Overview

ColorSense is an Xcode plugin that makes working with `UIColor` (and `NSColor`) more visual.
ColorSense is an Xcode plugin that makes working with `UIColor` (and `NSColor` and `CCColor`) more visual.

There are [many](http://www.colorchooserapp.com) [tools](http://iconfactory.com/software/xscope) that allow you to insert a `UIColor`/`NSColor` from a color picker or by picking a color from the screen. But once you've inserted it, it can be hard to remember which color you're actually looking at in your code because you basically just have a series of numbers.

Expand Down Expand Up @@ -55,4 +55,4 @@ This is tested on OS X 10.8 with Xcode 4.4.1 and 4.5.
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.