diff options
author | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-04-30 10:56:29 -0400 |
---|---|---|
committer | Fabian Guerra <fabian.guerra@mapbox.com> | 2018-05-04 13:48:09 -0400 |
commit | 40164edaee30c8f0d5642170765de337e96569d1 (patch) | |
tree | 4108be7e4475e297d38fb800a5d38e9cff17611f | |
parent | 73fe7ac9d3380b8038294b5f7982033fb0cc4ea3 (diff) | |
download | qtlocation-mapboxgl-40164edaee30c8f0d5642170765de337e96569d1.tar.gz |
[ios, macos] Enable to-rgba operator for MGLColor or key path expressions.
-rw-r--r-- | platform/darwin/docs/guides/For Style Authors.md.ejs | 8 | ||||
-rw-r--r-- | platform/darwin/src/NSExpression+MGLAdditions.mm | 12 | ||||
-rw-r--r-- | platform/darwin/test/MGLExpressionTests.mm | 13 | ||||
-rw-r--r-- | platform/ios/docs/guides/For Style Authors.md | 2 | ||||
-rw-r--r-- | platform/ios/src/UIColor+MGLAdditions.mm | 2 | ||||
-rw-r--r-- | platform/macos/docs/guides/For Style Authors.md | 4 | ||||
-rw-r--r-- | platform/macos/src/NSColor+MGLAdditions.mm | 10 |
7 files changed, 35 insertions, 16 deletions
diff --git a/platform/darwin/docs/guides/For Style Authors.md.ejs b/platform/darwin/docs/guides/For Style Authors.md.ejs index dca6c2905c..60177e57c2 100644 --- a/platform/darwin/docs/guides/For Style Authors.md.ejs +++ b/platform/darwin/docs/guides/For Style Authors.md.ejs @@ -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` | | @@ -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` diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm index 7a1d73f43a..362a4986f8 100644 --- a/platform/darwin/src/NSExpression+MGLAdditions.mm +++ b/platform/darwin/src/NSExpression+MGLAdditions.mm @@ -817,7 +817,7 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } 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]; @@ -1191,7 +1191,15 @@ NSArray *MGLSubexpressionsWithJSONObjects(NSArray *objects) { } #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]; diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 4980599985..4ed1f61eb1 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -685,12 +685,21 @@ using namespace std::string_literals; 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 { diff --git a/platform/ios/docs/guides/For Style Authors.md b/platform/ios/docs/guides/For Style Authors.md index cc92ee0433..d4fb17eb6a 100644 --- a/platform/ios/docs/guides/For Style Authors.md +++ b/platform/ios/docs/guides/For Style Authors.md @@ -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` diff --git a/platform/ios/src/UIColor+MGLAdditions.mm b/platform/ios/src/UIColor+MGLAdditions.mm index 9ca39acda4..7c1fbddc20 100644 --- a/platform/ios/src/UIColor+MGLAdditions.mm +++ b/platform/ios/src/UIColor+MGLAdditions.mm @@ -66,7 +66,7 @@ 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 diff --git a/platform/macos/docs/guides/For Style Authors.md b/platform/macos/docs/guides/For Style Authors.md index e257b1fdb8..08385a66d9 100644 --- a/platform/macos/docs/guides/For Style Authors.md +++ b/platform/macos/docs/guides/For Style Authors.md @@ -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` | | @@ -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` diff --git a/platform/macos/src/NSColor+MGLAdditions.mm b/platform/macos/src/NSColor+MGLAdditions.mm index 8c9086ccf7..c73c1a41b7 100644 --- a/platform/macos/src/NSColor+MGLAdditions.mm +++ b/platform/macos/src/NSColor+MGLAdditions.mm @@ -79,8 +79,14 @@ 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) { |