From 076042a128933da5e5c6ba1029db536cd70d9eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Nyga=CC=8Ard?= Date: Mon, 1 Dec 2014 17:56:33 +0100 Subject: [PATCH] Adding limited support for SKColors Adding support for SKColor with RGBA, Init + RGBA and constant colors. --- Classes/OMColorHelper.h | 11 ++++++- Classes/OMColorHelper.m | 73 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/Classes/OMColorHelper.h b/Classes/OMColorHelper.h index 51b663a..d01c2e6 100644 --- a/Classes/OMColorHelper.h +++ b/Classes/OMColorHelper.h @@ -23,10 +23,17 @@ typedef enum OMColorType { OMColorTypeNSWhiteCalibrated, //[NSColor colorWithCalibratedWhite:0.5 alpha:1.0] OMColorTypeNSWhiteDevice, //[NSColor colorWithDeviceWhite:0.5 alpha:1.0] OMColorTypeNSConstant, //[NSColor redColor] + + OMColorTypeSKRGBA, //[SKColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] + OMColorTypeSKRGBAInit, //[[SKColor alloc] initWithRed:1.0 green:0.0 blue:0.0 alpha:1.0] + OMColorTypeSKWhite, //[SKColor colorWithWhite:0.5 alpha:1.0] + OMColorTypeSKWhiteInit, //[[SKColor alloc] initWithWhite:0.5 alpha:1.0] + OMColorTypeSKConstant, //[SKColor redColor] } OMColorType; -BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTypeNSRGBACalibrated; } +BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTypeNSRGBACalibrated && colorType <= OMColorTypeNSConstant; } +BOOL OMColorTypeIsSKColor(OMColorType colorType) { return colorType >= OMColorTypeSKRGBA; } //TODO: Maybe support HSB and CMYK color types... @@ -43,8 +50,10 @@ BOOL OMColorTypeIsNSColor(OMColorType colorType) { return colorType >= OMColorTy NSRegularExpression *_rgbaUIColorRegex; NSRegularExpression *_rgbaNSColorRegex; + NSRegularExpression *_rgbaSKColorRegex; NSRegularExpression *_whiteNSColorRegex; NSRegularExpression *_whiteUIColorRegex; + NSRegularExpression *_whiteSKColorRegex; NSRegularExpression *_constantColorRegex; } diff --git a/Classes/OMColorHelper.m b/Classes/OMColorHelper.m index a2e0a03..da86633 100755 --- a/Classes/OMColorHelper.m +++ b/Classes/OMColorHelper.m @@ -54,7 +54,9 @@ - (id)init _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]; + _rgbaSKColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*SKColor\\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]; + _whiteSKColorRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\[\\s*SKColor\\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]; + _constantColorRegex = [NSRegularExpression regularExpressionWithPattern:@"\\[\\s*(UI|NS|SK)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; } @@ -360,6 +362,8 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t foundColorRange = colorRange; if ([NS_UI isEqualToString:@"UI"]) { foundColorType = OMColorTypeUIConstant; + } else if ([NS_UI isEqualToString:@"SK"]) { + foundColorType = OMColorTypeSKConstant; } else { foundColorType = OMColorTypeNSConstant; } @@ -424,6 +428,61 @@ - (NSColor *)colorInText:(NSString *)text selectedRange:(NSRange)selectedRange t } }]; } + + if (!foundColor) { + [_rgbaSKColorRegex 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 = OMColorTypeSKRGBAInit; + } else { + foundColorType = OMColorTypeSKRGBA; + } + + // [SKColor 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 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) { + [_whiteSKColorRegex 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 = OMColorTypeSKWhiteInit; + } else { + foundColorType = OMColorTypeSKWhite; + } + 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) { if (matchedRange != NULL) { @@ -462,6 +521,8 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy if ([constantColor isEqual:color]) { if (OMColorTypeIsNSColor(colorType)) { colorString = [NSString stringWithFormat:@"[NSColor %@Color]", colorName]; + } else if (OMColorTypeIsSKColor(colorType)) { + colorString = [NSString stringWithFormat:@"[SKColor %@Color]", colorName]; } else { colorString = [NSString stringWithFormat:@"[UIColor %@Color]", colorName]; } @@ -480,6 +541,11 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy } else if (colorType == OMColorTypeNSRGBADevice || colorType == OMColorTypeNSWhiteDevice) { colorString = [NSString stringWithFormat:@"[NSColor colorWithDeviceWhite:%.3f alpha:%.3f]", red, alpha]; } + else if (colorType == OMColorTypeSKRGBA || colorType == OMColorTypeSKWhite || colorType == OMColorTypeSKConstant) { + colorString = [NSString stringWithFormat:@"[SKColor colorWithWhite:%.3f alpha:%.3f]", red, alpha]; + } else if (colorType == OMColorTypeSKRGBAInit || colorType == OMColorTypeSKWhiteInit) { + colorString = [NSString stringWithFormat:@"[[SKColor 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]; @@ -491,6 +557,11 @@ - (NSString *)colorStringForColor:(NSColor *)color withType:(OMColorType)colorTy } else if (colorType == OMColorTypeNSRGBADevice || colorType == OMColorTypeNSWhiteDevice) { colorString = [NSString stringWithFormat:@"[NSColor colorWithDeviceRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; } + if (colorType == OMColorTypeSKRGBA || colorType == OMColorTypeSKWhite || colorType == OMColorTypeSKConstant) { + colorString = [NSString stringWithFormat:@"[SKColor colorWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; + } else if (colorType == OMColorTypeSKRGBAInit || colorType == OMColorTypeSKWhiteInit) { + colorString = [NSString stringWithFormat:@"[[SKColor alloc] initWithRed:%.3f green:%.3f blue:%.3f alpha:%.3f]", red, green, blue, alpha]; + } } } }