From 837e42e07d52333bd790f81df3c2f5600b115faa Mon Sep 17 00:00:00 2001 From: Nadia Barbosa Date: Thu, 11 Apr 2019 17:00:10 -0700 Subject: [ios, macos] Premultiply color values for mgl_color Fixes https://github.com/mapbox/mapbox-gl-native/issues/14329 --- platform/darwin/test/MGLExpressionTests.mm | 9 +++++++-- platform/ios/CHANGELOG.md | 1 + platform/ios/src/UIColor+MGLAdditions.mm | 11 +++++++++-- platform/macos/CHANGELOG.md | 1 + platform/macos/src/NSColor+MGLAdditions.mm | 13 ++++++++++--- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/platform/darwin/test/MGLExpressionTests.mm b/platform/darwin/test/MGLExpressionTests.mm index 06f06d8c6e..e7d1608018 100644 --- a/platform/darwin/test/MGLExpressionTests.mm +++ b/platform/darwin/test/MGLExpressionTests.mm @@ -282,9 +282,14 @@ using namespace std::string_literals; XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], color); } { - MGLColor *color = [MGLColor mgl_colorWithColor:{ 255.0/255, 239.0/255, 213.0/255, 0.5 }]; // papayawhip + // Transform color components to non-premultiplied values + float alpha = 0.5; + float red = (255.0 * alpha) / 255; + float green = (239.0 * alpha) / 255; + float blue = (213.0 * alpha) / 255; + MGLColor *color = [MGLColor mgl_colorWithColor:{ red, green, blue, alpha }]; // papayawhip NSExpression *expression = [NSExpression expressionForConstantValue:color]; - NSArray *jsonExpression = @[@"rgba", @255, @239, @213, @0.5]; + NSArray *jsonExpression = @[@"rgba", @127.5, @119.5, @106.5, @0.5]; XCTAssertEqualObjects(expression.mgl_jsonExpressionObject, jsonExpression); XCTAssertEqualObjects([expression expressionValueWithObject:nil context:nil], color); } diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index 72e3e75cb2..86b501e13f 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -6,6 +6,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Fixed an Interface Builder crash when using an `MGLMapView` in a storyboard. ([#14379](https://github.com/mapbox/mapbox-gl-native/pull/14379)) * Fix a bug that wrong position of attribution dialog after rotation. ([#14185](https://github.com/mapbox/mapbox-gl-native/pull/14185)) +* Fixed a bug where using `MGLSymbolStyleLayer.textHaloColor` with an alpha value would result in incorrect color values. ([#14406](https://github.com/mapbox/mapbox-gl-native/pull/14406)) ## 4.10.0 diff --git a/platform/ios/src/UIColor+MGLAdditions.mm b/platform/ios/src/UIColor+MGLAdditions.mm index 7c1fbddc20..5a4b5cf06e 100644 --- a/platform/ios/src/UIColor+MGLAdditions.mm +++ b/platform/ios/src/UIColor+MGLAdditions.mm @@ -6,7 +6,8 @@ { CGFloat r, g, b, a; [self getRed:&r green:&g blue:&b alpha:&a]; - return { (float)r, (float)g, (float)b, (float)a }; + // Premultiply color components + return { static_cast(r*a), static_cast(g*a), static_cast(b*a), static_cast(a) }; } - (mbgl::style::PropertyValue)mgl_colorPropertyValue @@ -17,7 +18,13 @@ + (UIColor *)mgl_colorWithColor:(mbgl::Color)color { - return [UIColor colorWithRed:color.r green:color.g blue:color.b alpha:color.a]; + // Since CIColor expects color values to be non-multiplied, we transform them here + // to non-premultiplied values + float red = static_cast((color.r / color.a)); + float green = static_cast((color.g / color.a)); + float blue = static_cast((color.b / color.a)); + + return [UIColor colorWithRed:red green:green blue:blue alpha:color.a]; } @end diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index ebfb3f335a..26f93377b2 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -12,6 +12,7 @@ * Added `mgl_attributed:` expression operator, which concatenate `MGLAttributedExpression` objects for specifying rich text in the `MGLSymbolStyleLayer.text` property. ([#14094](https://github.com/mapbox/mapbox-gl-native/pull/14094)) * Fixed an issue that caused conditional expressions to crash when passed nested conditional expressions as parameters. ([#14181](https://github.com/mapbox/mapbox-gl-native/pull/14181)) * Added `-[MGLMapViewDelegate mapView:didFailToLoadImage:]` to load missing symbol icons in the style if they are not found. ([#14302](https://github.com/mapbox/mapbox-gl-native/pull/14302)) +* Fixed a bug where using `MGLSymbolStyleLayer.textHaloColor` with an alpha value would result in incorrect color values. ([#14406](https://github.com/mapbox/mapbox-gl-native/pull/14406)) ### Offline diff --git a/platform/macos/src/NSColor+MGLAdditions.mm b/platform/macos/src/NSColor+MGLAdditions.mm index ea7d99f66d..0714ac1ada 100644 --- a/platform/macos/src/NSColor+MGLAdditions.mm +++ b/platform/macos/src/NSColor+MGLAdditions.mm @@ -15,15 +15,22 @@ } [srgbColor getRed:&r green:&g blue:&b alpha:&a]; - return { (float)r, (float)g, (float)b, (float)a }; + // Premultiply color components + return { static_cast(r*a), static_cast(g*a), static_cast(b*a), static_cast(a) }; } + (NSColor *)mgl_colorWithColor:(mbgl::Color)color { + // Since CIColor expects color values to be non-multiplied, we transform them here + // to non-premultiplied values + float red = static_cast((color.r / color.a)); + float green = static_cast((color.g / color.a)); + float blue = static_cast((color.b / color.a)); + // macOS 10.12 Sierra and below uses calibrated RGB by default. if ([NSColor redColor].colorSpaceName == NSCalibratedRGBColorSpace) { - return [NSColor colorWithCalibratedRed:color.r green:color.g blue:color.b alpha:color.a]; + return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:color.a]; } else { - return [NSColor colorWithRed:color.r green:color.g blue:color.b alpha:color.a]; + return [NSColor colorWithRed:red green:green blue:blue alpha:color.a]; } } -- cgit v1.2.1