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
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll (show other bugs)
Version: XI 9.6 (iOS 9.3)
Hardware: PC All
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-07-19 12:37 UTC by Maria Alekseeva
Modified: 2017-12-06 22:08 UTC (History)
5 users (show)

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


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

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(
     	 (System.nfloat)color.R,
     	 (System.nfloat)color.G,
     	 (System.nfloat)color.B,
     	 (System.nfloat)color.A);
     	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.

Note You need to log in before you can comment on or make changes to this bug.