From a3649667ecbf1127ee83f433cebaa1fd8d184c7b Mon Sep 17 00:00:00 2001 From: Kevin CATHALY Date: Mon, 6 Oct 2014 14:59:40 +0200 Subject: [PATCH 1/2] Add Swift UIColor(red: x, green: y, blue: z, alpha: a) support --- Classes/OMColorHelper.h | 3 ++- Classes/OMColorHelper.m | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Classes/OMColorHelper.h b/Classes/OMColorHelper.h index 51b663a..151283e 100644 --- a/Classes/OMColorHelper.h +++ b/Classes/OMColorHelper.h @@ -17,7 +17,7 @@ typedef enum OMColorType { OMColorTypeUIWhite, //[UIColor colorWithWhite:0.5 alpha:1.0] OMColorTypeUIWhiteInit, //[[UIColor alloc] initWithWhite:0.5 alpha:1.0] OMColorTypeUIConstant, //[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] @@ -46,6 +46,7 @@ BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTy NSRegularExpression *_whiteNSColorRegex; NSRegularExpression *_whiteUIColorRegex; NSRegularExpression *_constantColorRegex; + NSRegularExpression *_rgbaUIColorSwiftRegex; } @property (nonatomic, strong) OMPlainColorWell *colorWell; diff --git a/Classes/OMColorHelper.m b/Classes/OMColorHelper.m index a2e0a03..fa8fc7d 100755 --- a/Classes/OMColorHelper.m +++ b/Classes/OMColorHelper.m @@ -55,6 +55,7 @@ - (id)init _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]; } return self; } @@ -326,6 +327,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) { @@ -479,7 +508,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) { + 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]; @@ -490,7 +521,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) { + colorString = [NSString stringWithFormat:@"UIColor(red: %.3f, green: %.3f, blue: %.3f, alpha: %.3f)", red, green, blue, alpha]; + } } } } From a067c622309cfe1aea197a839b302a05afdb59f0 Mon Sep 17 00:00:00 2001 From: Kevin CATHALY Date: Tue, 7 Oct 2014 17:46:41 +0200 Subject: [PATCH 2/2] Add UIColor.redColor() Support and UIColor(white: 0.5, alpha: 1.0) Support --- Classes/OMColorHelper.h | 7 ++++- Classes/OMColorHelper.m | 63 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/Classes/OMColorHelper.h b/Classes/OMColorHelper.h index 151283e..805d993 100644 --- a/Classes/OMColorHelper.h +++ b/Classes/OMColorHelper.h @@ -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; @@ -47,6 +50,8 @@ BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTy NSRegularExpression *_whiteUIColorRegex; NSRegularExpression *_constantColorRegex; NSRegularExpression *_rgbaUIColorSwiftRegex; + NSRegularExpression *_whiteUIColorSwiftRegex; + NSRegularExpression *_constantColorSwiftRegex; } @property (nonatomic, strong) OMPlainColorWell *colorWell; diff --git a/Classes/OMColorHelper.m b/Classes/OMColorHelper.m index fa8fc7d..0904abd 100755 --- a/Classes/OMColorHelper.m +++ b/Classes/OMColorHelper.m @@ -52,10 +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; } @@ -378,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) { @@ -396,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) { @@ -489,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; } } @@ -508,7 +557,7 @@ - (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) { + } else if (colorType == OMColorTypeUIRGBASwift || colorType == OMColorTypeUIWhiteSwift || colorType == OMColorTypeUIConstantSwift) { colorString = [NSString stringWithFormat:@"UIColor(white: %.3f, alpha: %.3f)", red, alpha]; } } else { @@ -521,7 +570,7 @@ - (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) { + } else if (colorType == OMColorTypeUIRGBASwift || colorType == OMColorTypeUIWhiteSwift || colorType == OMColorTypeUIConstantSwift) { colorString = [NSString stringWithFormat:@"UIColor(red: %.3f, green: %.3f, blue: %.3f, alpha: %.3f)", red, green, blue, alpha]; } }