美文网首页
比较color1 和 color2 是否一致 (判断两个UIco

比较color1 和 color2 是否一致 (判断两个UIco

作者: coding_Liu | 来源:发表于2019-07-16 13:57 被阅读0次

    方法一:
    CGColorEqualToColor(color1.CGColor,color2.CGColor)

    备注:有时候不好用(不好用场景通过转换rgb后,确实有细微偏差;但是通过转换成十六进制却一样),单独测试(同一个类中)没问题

    方法二:

        //颜色值个数,rgb和alpha
        NSInteger cpts = CGColorGetNumberOfComponents(color.CGColor);
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        CGFloat r = components[0];//红色
        CGFloat g = components[1];//绿色
        CGFloat b = components[2];//蓝色
        if (cpts == 4) {
            CGFloat a = components[3];//透明度
            return [NSString stringWithFormat:@"#%02lX%02lX%02lX%02lX", lroundf(a * 255), lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)];
        } else {
            return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255)];
        }
    }
    

    备注:方法一中未知不好用场景,可以转化成十六进制,在进行字符串比较(isEqTo)

    相关文章

      网友评论

          本文标题:比较color1 和 color2 是否一致 (判断两个UIco

          本文链接:https://www.haomeiwen.com/subject/xbozkctx.html