iOS中的十六进制颜色不准确 [英] hex colors in iOS are not accurate

查看:398
本文介绍了iOS中的十六进制颜色不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在xib中设置视图的backgroundColor时,显示的颜色与以编程方式设置backgroundColor时的颜色明显不同。

I find that when you set a view's backgroundColor in xib, the color displayed is distinctly different from when you set backgroundColor programmatically.

这是一个例子。

我在这个简单的演示中有两个视图。我在xib中设置了上部视图的backgroundColor,如下所示:

I have two views in this simple demo. I set the upper view's backgroundColor in xib, like this:

十六进制颜色值为0x1BA9BA。然后我以相同的十六进制颜色值以编程方式设置下部视图的backgroundColor。我使用以下代码:

The hex color value is 0x1BA9BA. Then I set the lower view's backgroundColor programmatically with the same hex color value. I use the following code:

NSInteger hexValue = 0x1ba9ba;
self.testView.backgroundColor = [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16)) / 255.0 
                                                  green:((float)((hexValue & 0xFF00) >> 8)) / 255.0 
                                                  blue:((float)(hexValue & 0xFF))/255.0 
                                                  alpha:1.0];

结果如下:

如您所见,颜色有明显的差异。我在这里缺少什么?

As you can see, there's a clear difference in color. What am I missing here?

推荐答案

我将RGB滑块应用到第一个视图,将代码应用到第二个视图和颜色在模拟器中完全相同(在我看来);这是模拟器的屏幕截图:

I applied your RGB sliders to the first view and your code to the second view and the colors are absolutely identical (to my eye) in the simulator; this is a screen shot from the simulator:

然而,当颜色选择器的颜色空间设置为Generic RGB时,我将这些数字输入颜色选择器(27,169,186)。如果我切换到sRGB和然后输入这些数字,我可以重现你的问题。毕竟这是一个色彩空间问题;首先将颜色空间设置为Generic RGB,现在数字将匹配。

However, I entered those numbers into the color picker (27, 169, 186) when the color picker's color space was set to Generic RGB. If I switch to sRGB and then enter those numbers, I can reproduce your problem. So it is a color space issue after all; just start with the color space set to Generic RGB and now the numbers will match up.

这篇关于iOS中的十六进制颜色不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆