iPhone开发:UIColor / CGColor组件比较

发布于 2024-09-26 09:17:24 字数 7267 浏览 11 评论 0原文

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

(NSString *)internalColorCode:(UIColor *)颜色 {

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"];
}

}

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 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"];
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2024-10-03 09:17:24

问题在于代码中使用的逻辑类型,例如 (0.949019 <= red <= 0.949021)。

您需要替换为 ((0.949019 <= red) && (red <= 0.949021)) 才能以预期方式计算表达式。

因此,所有类型的条件检查(a <=变量<= b)都需要更改为((a<=变量)&&(变量<=b))。

以下是它不起作用的原因的详细解释。

<= 的运算符优先级是从左到右。因此,表达式的计算如下:

  1. 第一个 'a' 与 'variable' 进行比较,并对结果进行计算。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文