开发者

Iphone development: UIColor / CGColor components comparison

I've got some internal codes for colors in my application, so I created a function that, given a color, returns the code. I'm having problems as it just doesn't work and return always "01" for every color else than the red, which returns the proper code. Has someone a clue about it?

(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开发者_JAVA技巧 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"];
}

}


The problem is with the type of logic used in the code like (0.949019 <= red <= 0.949021).

You need to replace with ((0.949019 <= red) && (red <= 0.949021)) for the expression to be evaluated in the expected manner.

So, all the condition checking of the type( a <= variable <= b) need to be changed to ((a<= variable)&&(variable <=b)).

The following is the detailed explanation of the why it doesn't work.

Operator precedence of <= is left to right. Hence, the expression is evaluated as follows:

  1. First 'a' compared with 'variable' and result is evaluated.

  2. The RESULT(not the 'variable') is then compared with 'b' and final result is found.

This is not as you wish to evaluate the expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜