iphone开发:UIColor/CGColor组件对比

Posted

技术标签:

【中文标题】iphone开发:UIColor/CGColor组件对比【英文标题】:Iphone development: UIColor / CGColor components comparison 【发布时间】:2010-10-05 20:10:24 【问题描述】:

我的应用程序中有一些颜色的内部代码,所以我创建了一个函数,给定颜色,返回代码。我遇到了问题,因为它不起作用,并且对于除红色以外的每种颜色都返回“01”,这会返回正确的代码。有人知道吗?

(NSString *)internalColorCode:(UIColor *)color

const CGFloat *components = CGColorGetComponents(color.CGColor);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];

NSString *s = [NSString stringWithFormat:@"-- Red: %f - Green: %f - Blue: %f", red, green, blue];
NSLog(s);

if ((0.949019 <= red <= 0.949021) && (0.949019 <= green <= 0.949021) && (0.952940 <= blue <= 0.952942)) 
    return [NSString stringWithFormat:@"01"];
 else if ((0.760783 <= red <= 0.760785) && (0.768626 <= green <= 0.768628) && (0.776470 <= blue <= 0.776472)) 
    return [NSString stringWithFormat:@"02"];
 else if ((0.427450 <= red <= 0.427452) && (0.431372 <= green <= 0.431374) && (0.443136 <= blue <= 0.443138)) 
    return [NSString stringWithFormat:@"03"];
 else if ((0.254901 <= red <= 0.254903) && (0.250979 <= green <= 0.250981) && (0.258823 <= blue <= 0.258825)) 
    return [NSString stringWithFormat:@"04"];
 else if ((0.156862 <= red <= 0.156864) && (0.156862 <= green <= 0.156864) && (0.152940 <= blue <= 0.152942)) 
    return [NSString stringWithFormat:@"05"];
 else if ((0.784313 <= red <= 0.784315) && (0.568626 <= green <= 0.568628) && (0.388234 <= blue <= 0.388236)) 
    return [NSString stringWithFormat:@"06"];
 else if ((0.576470 <= red <= 0.576472) && (0.415685 <= green <= 0.415687) && (0.278430 <= blue <= 0.278432)) 
    return [NSString stringWithFormat:@"07"];
 else if ((0.431372 <= red <= 0.431374) && (0.301960 <= green <= 0.301962) && (0.192156 <= blue <= 0.192158)) 
    return [NSString stringWithFormat:@"08"];
 else if ((0.356862 <= red <= 0.356864) && (0.243136 <= green <= 0.243138) && (0.145097 <= blue <= 0.145099)) 
    return [NSString stringWithFormat:@"09"];
 else if ((0.274509 <= red <= 0.274511) && (0.180391 <= green <= 0.180393) && (0.090195 <= blue <= 0.090197)) 
    return [NSString stringWithFormat:@"10"];
 else if ((0.627450 <= red <= 0.627452) && (0.000000 <= green <= 0.000001) && (0.090195 <= blue <= 0.090197)) 
    return [NSString stringWithFormat:@"11"];
 else if ((0.968626 <= red <= 0.968628) && (0.121568 <= green <= 0.121570) && (0.105881 <= blue <= 0.105883)) 
    return [NSString stringWithFormat:@"12"];
 else if ((0.999999 <= red <= 1.000000) && (0.466666 <= green <= 0.466668) && (0.000000 <= blue <= 0.000001)) 
    return [NSString stringWithFormat:@"13"];
 else if ((0.964705 <= red <= 0.964707) && (0.560783 <= green <= 0.560785) && (0.450979 <= blue <= 0.450981)) 
    return [NSString stringWithFormat:@"14"];
 else if ((0.999999 <= red <= 1.000000) && (0.709803 <= green <= 0.709805) && (0.498038 <= blue <= 0.498040)) 
    return [NSString stringWithFormat:@"15"];
 else if ((0.980391 <= red <= 0.980393) && (0.811764 <= green <= 0.811766) && (0.584313 <= blue <= 0.584315)) 
    return [NSString stringWithFormat:@"16"];
 else if ((0.999999 <= red <= 1.000000) && (0.870587 <= green <= 0.870589) && (0.525489 <= blue <= 0.525491)) 
    return [NSString stringWithFormat:@"17"];
 else if ((0.988234 <= red <= 0.988236) && (0.886274 <= green <= 0.886276) && (0.662744 <= blue <= 0.662746)) 
    return [NSString stringWithFormat:@"18"];
 else if ((0.968626 <= red <= 0.968628) && (0.894117 <= green <= 0.894119) && (0.352940 <= blue <= 0.352942)) 
    return [NSString stringWithFormat:@"19"];
 else if ((0.815685 <= red <= 0.815687) && (0.752940 <= green <= 0.752942) && (0.239215 <= blue <= 0.239217)) 
    return [NSString stringWithFormat:@"20"];
 else if ((0.862744 <= red <= 0.862746) && (0.870587 <= green <= 0.870589) && (0.152940 <= blue <= 0.152942)) 
    return [NSString stringWithFormat:@"21"];
 else if ((0.576470 <= red <= 0.576472) && (0.772548 <= green <= 0.772550) && (0.254901 <= blue <= 0.254903)) 
    return [NSString stringWithFormat:@"22"];
 else if ((0.133332 <= red <= 0.133334) && (0.682352 <= green <= 0.682354) && (0.305881 <= blue <= 0.305883)) 
    return [NSString stringWithFormat:@"23"];
 else if ((0.000000 <= red <= 0.000001) && (0.482352 <= green <= 0.482354) && (0.231372 <= blue <= 0.231374)) 
    return [NSString stringWithFormat:@"24"];
 else if ((0.137254 <= red <= 0.137256) && (0.247058 <= green <= 0.247060) && (0.000000 <= blue <= 0.000001)) 
    return [NSString stringWithFormat:@"25"];
 else if ((0.470587 <= red <= 0.470589) && (0.823528 <= green <= 0.823530) && (0.968626 <= blue <= 0.968628)) 
    return [NSString stringWithFormat:@"26"];
 else if ((0.000000 <= red <= 0.000001) && (0.658823 <= green <= 0.658825) && (0.847058 <= blue <= 0.847060)) 
    return [NSString stringWithFormat:@"27"];
 else if ((0.000000 <= red <= 0.000001) && (0.435293 <= green <= 0.435295) && (0.690195 <= blue <= 0.690197)) 
    return [NSString stringWithFormat:@"28"];
 else if ((0.000000 <= red <= 0.000001) && (0.294117 <= green <= 0.294119) && (0.478430 <= blue <= 0.478432)) 
    return [NSString stringWithFormat:@"29"];
 else if ((0.000000 <= red <= 0.000001) && (0.129411 <= green <= 0.129413) && (0.490195 <= blue <= 0.490197)) 
    return [NSString stringWithFormat:@"30"];
 else if ((0.384313 <= red <= 0.384315) && (0.109803 <= green <= 0.109805) && (0.360783 <= blue <= 0.360785)) 
    return [NSString stringWithFormat:@"31"];
 else if ((0.725489 <= red <= 0.725491) && (0.109803 <= green <= 0.109805) && (0.552940 <= blue <= 0.552942)) 
    return [NSString stringWithFormat:@"32"];
 else if ((0.658823 <= red <= 0.658825) && (0.352940 <= green <= 0.352942) && (0.639215 <= blue <= 0.639217)) 
    return [NSString stringWithFormat:@"33"];
 else if ((0.937254 <= red <= 0.937256) && (0.384313 <= green <= 0.384315) && (0.643136 <= blue <= 0.643138)) 
    return [NSString stringWithFormat:@"34"];
 else if ((0.999999 <= red <= 1.000000) && (0.623528 <= green <= 0.623530) && (0.776470 <= blue <= 0.776472)) 
    return [NSString stringWithFormat:@"35"];
else 
    return [NSString stringWithFormat:@"01"];

【问题讨论】:

【参考方案1】:

问题在于代码中使用的逻辑类型,例如 (0.949019

您需要替换为 ((0.949019

所以,所有类型(a

下面是它为什么不起作用的详细解释。

    首先将“a”与“变量”进行比较并评估结果。

    然后将 RESULT(不是“变量”)与“b”进行比较并找到最终结果。

这不是您希望评估的表达式。

【讨论】:

以上是关于iphone开发:UIColor/CGColor组件对比的主要内容,如果未能解决你的问题,请参考以下文章

删除 UIColor CGColor - KVC 警告

错误sintax UIColor(CGColor:selectedButton?.layer.backgroundColor)Swift

iPhone 游戏开发:成就框架?

如何获取标签栏控制器中包含的所有视图控制器? iphone开发

iPhone 通讯簿 - 总是从默认组中提取

使用 NSData 在 iPhone 上将音频文件正确组合为一个