UIColors的比较

时间:2010-11-26 15:16:30

标签: iphone uicolor

我有一些代码,我无法弄清楚它为什么不起作用,

UIColor *lastColor, *red;
red = [UIColor colorWithRed:0.993 green:0.34444 blue:0.0 alpha:1.0]; 

NSString *chosenColor;

if([lastColor isEqual: red])
{
   chosenColor = @"red";
}

我还发现有些人重写了isEqual方法isEqualtoColor: 但这也不起作用,我喂了一个lastColor.CGColor;

我读过的所有地方都是isEqual,你需要比较UIColors,我目前正在使用红色,绿色和蓝色的字符串数组,并将浮点值与CGColorGetComponents(lastColor)进行比较;

但这也不起作用。

5 个答案:

答案 0 :(得分:6)

gist.github.com/mtabini/716917

上面的链接就像一个魅力。非常感谢“Count Chocula”巧克力和iphone答案的美味来源:P

我点击回复你的帖子,但它会更多地误导任何想在这里的人。

一定是浮动容差。

答案 1 :(得分:2)

这是接受答案的Swift版本。我放弃了CGColorSpaceRef比较,因为我无法在swift中找到直接的方法,但除了性能方面外,它似乎不会影响结果。

class func colorsConsideredSimilar(color: UIColor, otherColor: UIColor) -> Bool {
    let tolerance: CGFloat = 0.05 // 5%

    let colorRef: CGColorRef = color.CGColor
    let otherColorRef: CGColorRef = otherColor.CGColor

    let componentCount = CGColorGetNumberOfComponents(colorRef)

    let components = CGColorGetComponents(colorRef)
    let otherComponents = CGColorGetComponents(otherColorRef)

    for var i = 0; i < componentCount; i++ {
        let difference = components[i] / otherComponents[i]

        if (fabs(difference - 1) > tolerance) {
            return false
        }
    }

    return true
}

答案 2 :(得分:1)

如果从具有RGB颜色空间的CGContext中获取像素颜色,则它可能与UIColor类使用的颜色不匹配。直接存储器引用到CGContext中的像素值将为0-255(在RGB中)。根据您想要比较的颜色调色板,我可能会使用uint8_t值来处理这个问题,以避免浮动并根据RGB中的显性值执行广义比较

答案 3 :(得分:0)

你实际上正在初始化红色变量吗?这应该有效:

if ([lastColor isEqual:[UIColor redColor]]) {
  /* … */
}

答案 4 :(得分:0)

-[UIColor isEqual:]是比较颜色的内置方式。但是,您必须记住,虽然两种颜色在特定设备上可能看起来与您的眼睛相同,但它们可能在数学上有所不同,因此比较不等。两种颜色看起来与您的眼睛不同(例如,RGB中的“纯红色”与CMYK中的“纯红色”),在转换为中性色空间进行比较时,可以比较相等。

颜色空间就是b_ _ 。 :)

如果您告诉我们为什么您想要比较两种颜色,我们可以为您提供比直接比较更好的解决方案。

回复您的评论:不要将用户界面点击测试基于单个或甚至多个像素的颜色。它们的绝对值可能会根据设备颜色空间而改变,从而导致误报和否定。相反,您可以通过在界面中放置不可见的UIButton对象来定义“热点”区域,或者更好的是,通过使用更灵活的内容(如CGPath)并测试触摸是否在其范围内。以下内容可能有效:

@interface HotSpottyView : UIView {
    @private NSMutableDictionary *hotspots;
}
- (void)addHotspot: (CGPathRef)path withBlock: (void (^ handler)(void));
@end


@implementation HotSpottyView
- (id)initWithFrame: (CGRect)frameRect {
    self = [super initWithFrame: frameRect];

    if (self) {
        self->hotspots = [[NSMutableDictionary alloc] init];
    }

    return self;
}

- (id)initWithCoder: (NSCoder *)aDecoder {
    self = [super initWithFrame: frameRect];

    if (self) {
        self->hotspots = [[NSMutableDictionary alloc] init];
    }

    return self;
}

- (void)dealloc {
    [self->hotspots release];
    [super dealloc];
}

- (void)addHotspot: (CGPathRef)path withBlock: (void (^ handler)(void)) {
    CGPathRef immutablePath = CGPathCreateCopy(path);
    if (immutablePath) {
        [self->hotspots setObject: handler forKey: (id)immutablePath];
        CGPathRelease(immutablePath);
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *t = [touches anyObject];
    CGPoint loc = [t locationInView: self];
    if (CGRectContainsPoint(self.bounds, loc)) {
        for (id path in self->hotspots) {
            if (CGPathContainsPoint((CGPathRef)path, NULL, loc, FALSE)) {
                void (^ handler)(void) = [self->hotspots objectForKey: path];
                handler();
            }
        }
    }
}
@end