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
11 changes: 10 additions & 1 deletion Classes/OMColorHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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...

Expand All @@ -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;
}

Expand Down
73 changes: 72 additions & 1 deletion Classes/OMColorHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
}
Expand All @@ -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];
Expand All @@ -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];
}
}
}
}
Expand Down