Bug 42652 - Convert from Forms.Color to CGColor changes the color on iOS 9.3
Summary: Convert from Forms.Color to CGColor changes the color on iOS 9.3
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.6 (iOS 9.3)
Hardware: PC All
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2016-07-19 12:37 UTC by Maria Alekseeva
Modified: 2018-01-08 13:02 UTC (History)
5 users (show)

Is this bug a regression?: No
Last known good build:

Test project where the bug can be observed (537.89 KB, application/zip)
2016-07-19 12:37 UTC, Maria Alekseeva

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Maria Alekseeva 2016-07-19 12:37:19 UTC
Created attachment 16713 [details]
Test project where the bug can be observed

We have a custom renderer for ContentView, which draws a rounded corners view.
The color is set in PCL, so on iOS it should be converted from Forms.Color to CGColor for drawing.
We found out, that converting the color using extension method toCGColor() and method toUIColor().CGColor produces different results on iOS 9.3 (on other versions the colors are converted equally). And the result, produced by toCGColor() is the wrong one (comparing to setting the color as background for ContentView).

You can reproduce this by running attached test project on a device or a simulator with iOS version 9.3.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2016-07-19 13:41:47 UTC
iOS 9.3 added support for color management, which may be causing this.
Comment 2 Vincent Dondain [MSFT] 2016-07-19 14:50:12 UTC
I can confirm that this is happening.

iOS 8.4 screenshot: https://www.dropbox.com/s/kdnra6sahx180e3/Screenshot%202016-07-19%2016.41.17.png?dl=0

iOS 9.3 screenshot: https://www.dropbox.com/s/4ohjawait073ll9/Screenshot%202016-07-19%2016.42.20.png?dl=0
Comment 3 flywhc 2017-10-12 09:31:25 UTC
I can confirm this too:

e.g. Element.StartColor is a Forms.Color property. Following two lines should have same result: 
 1) Element.StartColor.ToCGColor()
 2) Element.StartColor.ToUIColor().ToCGColor() 

However, 1) returns a wrong value. 2) is correct.

I verified result by using manually convert the value:
     CGColor ToCGColor(Color color)
     	 UIKit.UIColor uicolor = new UIKit.UIColor(
     	return uicolor.CGColor;
Comment 4 Miguel de Icaza [MSFT] 2017-12-06 22:08:10 UTC
This bug is caused because the conversion to a CGColor() from a Color uses the kCGColorSpaceGenericRGB color space, while going through UIKit's UIColor uses the kCGColorSpaceExtendedSRGB.

Basically, new UIColor (r, g, b, a) and new CGColor(r,g,b) do not produce CGColors in the same colorspace.   

From the documentation: https://developer.apple.com/documentation/uikit/uicolor

The UIColor is created on the Device-Dependent RGB color space (which changes across versions and more recently on iOS 10 and higher, does not even clamp the values from 0..1) while CGColor is by default created in the generic RGB color space which is more limited.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2018-01-08 13:02:41 UTC
For reference: http://tirania.org/blog/archive/2017/Dec-07.html