Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Enable to-rgba operator for MGLColor or key path express…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
fabian-guerra committed Apr 30, 2018
1 parent 43414c3 commit 40564fd
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
8 changes: 2 additions & 6 deletions platform/darwin/docs/guides/For Style Authors.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,7 @@ In style specification | Method, function, or predicate type | Format string syn
`number` | |
`string` | |
`to-boolean` | `boolValue` |
<% if (macOS) { -%>
`to-color` | | `CAST(var, 'NScolor')`
<% } else { -%>
`to-color` | | `CAST(var, 'UIColor')`
<% } -%>
`to-color` | | `CAST(var, '<%- cocoaPrefix %>Color')`
`to-number` | `mgl_numberWithFallbackValues:` | `CAST(zipCode, 'NSNumber')`
`to-string` | `stringValue` | `CAST(ele, 'NSString')`
`typeof` | |
Expand Down Expand Up @@ -376,7 +372,7 @@ In style specification | Method, function, or predicate type | Format string syn
`rgb` | `+[UIColor colorWithRed:green:blue:alpha:]` |
`rgba` | `+[UIColor colorWithRed:green:blue:alpha:]` |
<% } -%>
`to-rgba` | | `CAST(var, 'NSArray')`
`to-rgba` | | `CAST(noindex(var), 'NSArray')`
`-` | `from:subtract:` | `2 - 1`
`*` | `multiply:by:` | `1 * 2`
`/` | `divide:by:` | `1 / 2`
Expand Down
12 changes: 10 additions & 2 deletions platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ + (instancetype)expressionWithMGLJSONObject:(id)object {

} else if ([op isEqualToString:@"to-rgba"]) {
NSExpression *operand = [NSExpression expressionWithMGLJSONObject:argumentObjects.firstObject];
return [NSExpression expressionWithFormat:@"CAST(%@, 'NSArray')", operand];
return [NSExpression expressionWithFormat:@"CAST(noindex(%@), 'NSArray')", operand];
} else if ([op isEqualToString:@"get"]) {
if (argumentObjects.count == 2) {
NSExpression *operand = [NSExpression expressionWithMGLJSONObject:argumentObjects.lastObject];
Expand Down Expand Up @@ -1176,7 +1176,15 @@ - (id)mgl_jsonExpressionObject {
}
#endif
else if ([type isEqualToString:@"NSArray"]) {
return @[@"to-rgba", object];
NSExpression *operand = self.arguments.firstObject;
if ([operand expressionType] == NSFunctionExpressionType ) {
operand = self.arguments.firstObject.arguments.firstObject;
}
if (([operand expressionType] != NSConstantValueExpressionType) ||
([operand expressionType] == NSConstantValueExpressionType &&
[[operand constantValue] isKindOfClass:[MGLColor class]])) {
return @[@"to-rgba", object];
}
}
[NSException raise:NSInvalidArgumentException
format:@"Casting expression to %@ not yet implemented.", type];
Expand Down
13 changes: 11 additions & 2 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -685,12 +685,21 @@ - (void)testTypeConversionExpressionObject {
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
}
{
NSExpression *expression = [NSExpression expressionWithFormat:@"CAST(x, 'NSArray')"];
NSExpression *expression = [NSExpression expressionWithFormat:@"CAST(noindex(x), 'NSArray')"];
NSArray *jsonExpression = @[@"to-rgba", @[@"get", @"x"]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}

{
NSExpression *expression = [NSExpression expressionWithFormat:@"CAST(noindex(%@), 'NSArray')", MGLConstantExpression(MGLColor.blueColor)];
NSArray *jsonExpression = @[@"to-rgba", @[@"rgb", @0, @0, @255]];
XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression);
XCTAssertEqualObjects([NSExpression expressionWithMGLJSONObject:jsonExpression], expression);
}
{
NSExpression *expression = [NSExpression expressionWithFormat:@"CAST(noindex('x'), 'NSArray')"];
XCTAssertThrowsSpecificNamed(expression.mgl_jsonExpressionObject, NSException, NSInvalidArgumentException);
}
}

- (void)testInterpolationExpressionObject {
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/docs/guides/For Style Authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ In style specification | Method, function, or predicate type | Format string syn
`upcase` | `uppercase:` | `uppercase('Elysian Fields')`
`rgb` | `+[UIColor colorWithRed:green:blue:alpha:]` |
`rgba` | `+[UIColor colorWithRed:green:blue:alpha:]` |
`to-rgba` | | `CAST(var, 'NSArray')`
`to-rgba` | | `CAST(noindex(var), 'NSArray')`
`-` | `from:subtract:` | `2 - 1`
`*` | `multiply:by:` | `1 * 2`
`/` | `divide:by:` | `1 / 2`
Expand Down
2 changes: 1 addition & 1 deletion platform/ios/src/UIColor+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ + (UIColor *)mgl_colorWithRGBComponents:(NSArray<NSExpression *> *)components {
return [UIColor colorWithRed:[components[0].constantValue doubleValue] / 255.0
green:[components[1].constantValue doubleValue] / 255.0
blue:[components[2].constantValue doubleValue] / 255.0
alpha:components.count == 3 ? [components[3].constantValue doubleValue] : 1.0];
alpha:components.count == 3 ? 1.0 : [components[3].constantValue doubleValue]];
}

@end
4 changes: 2 additions & 2 deletions platform/macos/docs/guides/For Style Authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ In style specification | Method, function, or predicate type | Format string syn
`number` | |
`string` | |
`to-boolean` | `boolValue` |
`to-color` | | `CAST(var, 'NScolor')`
`to-color` | | `CAST(var, 'NSColor')`
`to-number` | `mgl_numberWithFallbackValues:` | `CAST(zipCode, 'NSNumber')`
`to-string` | `stringValue` | `CAST(ele, 'NSString')`
`typeof` | |
Expand Down Expand Up @@ -350,7 +350,7 @@ In style specification | Method, function, or predicate type | Format string syn
`upcase` | `uppercase:` | `uppercase('Elysian Fields')`
`rgb` | `+[NSColor colorWithCalibratedRed:green:blue:alpha:]` |
`rgba` | `+[NSColor colorWithCalibratedRed:green:blue:alpha:]` |
`to-rgba` | | `CAST(var, 'NSArray')`
`to-rgba` | | `CAST(noindex(var), 'NSArray')`
`-` | `from:subtract:` | `2 - 1`
`*` | `multiply:by:` | `1 * 2`
`/` | `divide:by:` | `1 / 2`
Expand Down
10 changes: 8 additions & 2 deletions platform/macos/src/NSColor+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ + (NSColor *)mgl_colorWithComponentExpressions:(NSArray<NSExpression *> *)compon

components.push_back(component.doubleValue / 255.0);
}
// Alpha
components.back() *= 255.0;

if (components.size() < 4) {
components.push_back(1.0);
} else {
// Alpha
components.back() *= 255.0;
}


// macOS 10.12 Sierra and below uses calibrated RGB by default.
if ([NSColor redColor].colorSpaceName == NSCalibratedRGBColorSpace) {
Expand Down

0 comments on commit 40564fd

Please sign in to comment.