diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2018-03-30 16:43:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-30 16:43:55 -0700 |
commit | 59e92904c360e5aad94a1b789276bf6147120d7c (patch) | |
tree | fcb675741c492692324d841f8aaae905a3ccf8f4 /platform/ios | |
parent | b6866f764e5a804399ed60bbbce43a379b568ece (diff) | |
download | qtlocation-mapboxgl-59e92904c360e5aad94a1b789276bf6147120d7c.tar.gz |
Eviscerate mbgl expression to Foundation JSON object conversion (#11389)
* [ios, macos] Eviscerate mbgl expression to Foundation JSON object conversion
* [ios, macos] Remove literal special case
MGLJSONObjectFromMBGLValue() is just a literal translation from C++ types to Foundation types; no need for expression-specific literal wrapping.
* [ios, macos] Implemented RGB(A) operators
* [ios, macos] More robust color conversion
* [macos] Create calibrated RGB from expression on Sierra
* [macos] Calibrate colors in expressions at runtime
* [ios, macos] Special-case number conversion without fallbacks
* [ios, macos] Test mgl_number expressions
* [ios, macos] Updated tests for aftermarket expressions
* [ios, macos] Relax roundtripping requirement for serialize()
* [ios, macos] Fixed interpolation expression tests
* [ios, macos] Removed dead code
* [ios, macos] Fixed color conversion on Sierra
Diffstat (limited to 'platform/ios')
-rw-r--r-- | platform/ios/src/UIColor+MGLAdditions.h | 7 | ||||
-rw-r--r-- | platform/ios/src/UIColor+MGLAdditions.mm | 49 |
2 files changed, 56 insertions, 0 deletions
diff --git a/platform/ios/src/UIColor+MGLAdditions.h b/platform/ios/src/UIColor+MGLAdditions.h index ea415d9db9..60cfe1c58b 100644 --- a/platform/ios/src/UIColor+MGLAdditions.h +++ b/platform/ios/src/UIColor+MGLAdditions.h @@ -12,3 +12,10 @@ + (UIColor *)mgl_colorWithColor:(mbgl::Color)color; @end + +@interface NSExpression (MGLColorAdditions) + ++ (NSExpression *)mgl_expressionForRGBComponents:(NSArray<NSExpression *> *)components; ++ (NSExpression *)mgl_expressionForRGBAComponents:(NSArray<NSExpression *> *)components; + +@end diff --git a/platform/ios/src/UIColor+MGLAdditions.mm b/platform/ios/src/UIColor+MGLAdditions.mm index 41c066c206..9ca39acda4 100644 --- a/platform/ios/src/UIColor+MGLAdditions.mm +++ b/platform/ios/src/UIColor+MGLAdditions.mm @@ -21,3 +21,52 @@ } @end + +@implementation NSExpression (MGLColorAdditions) + ++ (NSExpression *)mgl_expressionForRGBComponents:(NSArray<NSExpression *> *)components { + if (UIColor *color = [self mgl_colorWithRGBComponents:components]) { + return [NSExpression expressionForConstantValue:color]; + } + + NSExpression *color = [NSExpression expressionForConstantValue:[UIColor class]]; + NSExpression *alpha = [NSExpression expressionForConstantValue:@1.0]; + return [NSExpression expressionForFunction:color + selectorName:@"colorWithRed:green:blue:alpha:" + arguments:[components arrayByAddingObject:alpha]]; +} + ++ (NSExpression *)mgl_expressionForRGBAComponents:(NSArray<NSExpression *> *)components { + if (UIColor *color = [self mgl_colorWithRGBComponents:components]) { + return [NSExpression expressionForConstantValue:color]; + } + + NSExpression *color = [NSExpression expressionForConstantValue:[UIColor class]]; + return [NSExpression expressionForFunction:color + selectorName:@"colorWithRed:green:blue:alpha:" + arguments:components]; +} + ++ (UIColor *)mgl_colorWithRGBComponents:(NSArray<NSExpression *> *)components { + if (components.count < 3 || components.count > 4) { + return nil; + } + + for (NSExpression *component in components) { + if (component.expressionType != NSConstantValueExpressionType) { + return nil; + } + + NSNumber *number = (NSNumber *)component.constantValue; + if (![number isKindOfClass:[NSNumber class]]) { + return nil; + } + } + + 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]; +} + +@end |