在 Objective-C 中比较颜色
我试图使用 Objective-C 编写的代码来确定两种颜色是否相等。
我正在使用这段代码来确定两种颜色是否等效(当前用于调试目的)
NSLog(@"currentColor is %@", currentColor);
NSLog(@"Adjacent Color is %@",[[buttonArray objectAtIndex:1] backgroundColor]);
NSLog(@"%i",[[buttonArray objectAtIndex:1] backgroundColor]==currentColor);
我的控制台显示
2009-10-20 00:27:10.814 colorGame[13588:207] currentColor is kCGColorSpaceModelRGB 0 0 1 1
2009-10-20 00:27:10.815 colorGame[13588:207] Adjacent Color is kCGColorSpaceModelRGB 0 0 1 1
2009-10-20 00:27:10.815 colorGame[13588:207] 0
我可以根据要求发布更多代码(我不知道是否真的有必要)。当前颜色最初被定义为
UIColor *currentColor;
好像有帮助。
我相当确定我只是进行了错误的比较,并且可能有一些内置方法可以比较颜色,但我只是不知道。
I'm trying to determine if two colors are equivalent, using code written in Objective-C.
I'm using this snippet of code to determine if the two colors are equivalent (currently for debugging purposes)
NSLog(@"currentColor is %@", currentColor);
NSLog(@"Adjacent Color is %@",[[buttonArray objectAtIndex:1] backgroundColor]);
NSLog(@"%i",[[buttonArray objectAtIndex:1] backgroundColor]==currentColor);
My console is showing
2009-10-20 00:27:10.814 colorGame[13588:207] currentColor is kCGColorSpaceModelRGB 0 0 1 1
2009-10-20 00:27:10.815 colorGame[13588:207] Adjacent Color is kCGColorSpaceModelRGB 0 0 1 1
2009-10-20 00:27:10.815 colorGame[13588:207] 0
I can post more code if asked (I don't know if any more is really necessary). Current color was initially defined as
UIColor *currentColor;
if that is any help.
I'm fairly sure I'm just doing the compare wrong, and that there is probably some built-in method that can compare colors, that I'm just not aware of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请记住,看起来相同的两种颜色可能返回 TRUE,也可能不返回 TRUE,因为组件保留为浮点数,并且它们的差异可能小于显示硬件可以解析的值。
另请记住,如果它们是在不同的颜色空间中定义的,则此方法将永远不会返回 TRUE。
Keep in mind that two colors that look the same may or may not return TRUE, since the components are kept as floats and they may differ by a value that's less than the display hardware can resolve.
Also keep in mind that if they're defined in different color spaces, this method will never return TRUE.
对象必须使用
isEqual:
方法进行比较,而不是==
,它只是比较指针地址objects must be compared using the
isEqual:
method, not==
, which simply compares the pointer address您正在测试对象指针的等效性,这可能永远不会返回 true。如果您想使用实际的颜色值,那么您需要获取底层的 CGColor 引用。
You're testing object pointers for equivalency, which is probably never going to return true. If you want to work with the actual color values then you'll need to get the underlying CGColor reference.