Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Swift UIColor(red: x, green: y, blue: z, alpha: a) support #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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: 8 additions & 2 deletions Classes/OMColorHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ typedef enum OMColorType {
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]
OMColorTypeUIWhiteSwift, //UIColor(white: 0.5, alpha: 1.0)
OMColorTypeUIWhiteInit, //[[UIColor alloc] initWithWhite:0.5 alpha:1.0]
OMColorTypeUIConstant, //[UIColor redColor]

OMColorTypeUIConstantSwift, //UIColor.redColor()
OMColorTypeUIRGBASwift, //UIColor(red: 0.670, green: 0.821, blue: 0.294, alpha: 1.0)
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]
OMColorTypeNSConstant, //[NSColor redColor]
OMColorTypeNSConstantSwift, //NSColor.redColor()

} OMColorType;

Expand All @@ -46,6 +49,9 @@ BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTy
NSRegularExpression *_whiteNSColorRegex;
NSRegularExpression *_whiteUIColorRegex;
NSRegularExpression *_constantColorRegex;
NSRegularExpression *_rgbaUIColorSwiftRegex;
NSRegularExpression *_whiteUIColorSwiftRegex;
NSRegularExpression *_constantColorSwiftRegex;
}

@property (nonatomic, strong) OMPlainColorWell *colorWell;
Expand Down
96 changes: 89 additions & 7 deletions Classes/OMColorHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ - (id)init

_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];
_whiteUIColorSwiftRegex = [NSRegularExpression regularExpressionWithPattern:@"(UIColor\\(white:)\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*,\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\).?[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];
_rgbaUIColorSwiftRegex = [NSRegularExpression regularExpressionWithPattern:@"(UIColor\\()red:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*,\\s+green:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*,\\s+blue:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*,\\s+alpha:\\s*([0-9]*\\.?[0-9]*f?)\\s*(\\/\\s*[0-9]*\\.?[0-9]*f?)?\\s*\\)" options:0 error:NULL];
_constantColorSwiftRegex = [NSRegularExpression regularExpressionWithPattern:@"(UI|NS)Color.(black|darkGray|lightGray|white|gray|red|green|blue|cyan|yellow|magenta|orange|purple|brown|clear)Color\\(\\)" options:0 error:NULL];
}
return self;
}
Expand Down Expand Up @@ -326,6 +329,34 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t
*stop = YES;
}
}];

if (!foundColor) {
[_rgbaUIColorSwiftRegex 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)) {
foundColorType = OMColorTypeUIRGBASwift;


// UIColor(red: 0.670/255, green: 0.821/255, blue: 0.294/255, alpha: 1/255)

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 = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
foundColorRange = colorRange;
*stop = YES;
}
}];
}

if (!foundColor) {
[_whiteUIColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
Expand All @@ -349,6 +380,26 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t
}
}];
}

if (!foundColor) {
[_whiteUIColorSwiftRegex 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)) {

foundColorType = OMColorTypeUIWhiteSwift;

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) {
Expand All @@ -367,6 +418,24 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t
}
}];
}

if (!foundColor) {
[_constantColorSwiftRegex 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 = OMColorTypeUIConstantSwift;
} else {
foundColorType = OMColorTypeNSConstantSwift;
}
*stop = YES;
}
}];
}

if (!foundColor) {
[_rgbaNSColorRegex enumerateMatchesInString:text options:0 range:NSMakeRange(0, text.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
Expand Down Expand Up @@ -460,11 +529,20 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy
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];
}
if (OMColorTypeIsNSColor(colorType)) {
if (colorType == OMColorTypeNSConstantSwift) {
colorString = [NSString stringWithFormat:@"NSColor.%@Color()", colorName];
} else {
colorString = [NSString stringWithFormat:@"[NSColor %@Color]", colorName];
}
} else {
if (colorType == OMColorTypeUIConstantSwift || colorType == OMColorTypeUIRGBASwift || colorType == OMColorTypeUIWhiteSwift) {
colorString = [NSString stringWithFormat:@"UIColor.%@Color()", colorName];
} else {
colorString = [NSString stringWithFormat:@"[UIColor %@Color]", colorName];
}
}

break;
}
}
Expand All @@ -479,7 +557,9 @@ - (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 == OMColorTypeUIRGBASwift || colorType == OMColorTypeUIWhiteSwift || colorType == OMColorTypeUIConstantSwift) {
colorString = [NSString stringWithFormat:@"UIColor(white: %.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 +570,9 @@ - (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 == OMColorTypeUIRGBASwift || colorType == OMColorTypeUIWhiteSwift || colorType == OMColorTypeUIConstantSwift) {
colorString = [NSString stringWithFormat:@"UIColor(red: %.3f, green: %.3f, blue: %.3f, alpha: %.3f)", red, green, blue, alpha];
}
}
}
}
Expand Down