我需要允许用户在iOS上选择颜色。我使用以下代码启动颜色选择器:
var picker = new UIColorPickerViewController();
picker.SupportsAlpha = true;
picker.Delegate = this;
picker.SelectedColor = color.ToUIColor();
PresentViewController(picker, true, null);
当颜色选择器显示时,颜色总是略微偏离。例如:
input RGBA: (220, 235, 92, 255)
颜色选择器中的初始颜色可以是:
selected color: (225, 234, 131, 255)
(这些都是来自测试的真实的值)。离得不远...但如果你正在寻找它,足以注意到。
我想知道颜色选择器网格是否强制颜色为最近的颜色条目-但如果是真的,您会期望某些颜色保持固定(即,如果输入颜色与网格颜色之一完全匹配,它应该保持不变)。
另外,我使用简单的RGBA值以跨平台方式存储颜色。ToUIColor使用
new UIColor((nfloat)rgb.r, (nfloat)rgb.g, (nfloat)rgb.b, (nfloat)rgb.a);
1条答案
按热度按时间gjmwrych1#
From the hints in comments by @DonMag, I've got some way towards an answer, and also a set of resources that can help if you are struggling with this.
The key challenge is that mac and iOS use displayP3 as the ColorSpace, but most people use default {UI,NS,CG}Color objects, which use the sRGB ColorSpace (actually... technically they are Extended sRGB so they can cover the wider gamut of DisplayP3). If you want to know the difference between these three - there's resources below.
When you use the UIColorPickerViewController, it allows the user to choose colors in DisplayP3 color space (I show an image of the picker below, and you can see the "Display P3 Hex Colour" at the bottom).
If you give it a color in sRGB, I think it gets converted to DisplayP3. When you read the color, you need to convert back to sRGB, which is the step I missed.
However I found that using CGColor.CreateByMatchingToColorSpace, to convert from DisplayP3 to sRGB never quite worked. In the code below I convert to and from DisplayP3 and should have got back my original color, but I never did. I tried removing Gamma by converting to a Linear space on the way but that didn't help.
Then I found an excellent resource: http://endavid.com/index.php?entry=79 that included a set of matrices that can perform the conversions. And that seems to work.
So now I have extended CGColor as follows:
Note: there's lots of assumptions in the matrices w.r.t white point and gammas. But it works for me. Let me know if there are better approaches out there, or if you can tell me why my use of CGColor.CreateByMatchingToColorSpace didn't quite work.
Reading Resources: Reading this: https://stackoverflow.com/a/49040628/6257435 then this: https://bjango.com/articles/colourmanagementgamut/ are essential starting points.
Image of the iOS Color Picker: