比较Objective-C中的颜色 [英] Comparing colors in 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是kCGColorSpaceModelRGB 0 0 1 1
2009-10-20 00:27:10.815 colorGame [13588:207]相邻颜色是kCGColorSpaceModelRGB 0 0 1 1
2009-10-20 00:27:10.815 colorGame [13588:207] 0
如果问我可以发布更多的代码(我不知道是否真的有必要)。当前颜色最初定义为
UIColor * currentColor;
$ b 我相信我只是做比较错误,并且可能有一些内置的方法可以比较颜色,我只是不知道。解决方案
@implementation UIColor(compare)
- (BOOL)isEqualToColor:(UIColor *)otherColor
{
return CGColorEqualToColor(self.CGColor,otherColor.CGColor);
}
@end
请记住两种颜色看起来相同可能或可能不返回TRUE,因为组件保持为浮动,并且它们可能不同的值小于显示硬件可以解析的值。
请记住,如果它们在不同的颜色空间中定义,此方法将永远不会返回TRUE。
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.
解决方案 @implementation UIColor (compare)
- (BOOL) isEqualToColor:(UIColor *) otherColor
{
return CGColorEqualToColor(self.CGColor, otherColor.CGColor);
}
@end
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.
这篇关于比较Objective-C中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!