2010-10-05 5 views
1

J'ai des codes internes pour les couleurs dans mon application, j'ai donc créé une fonction qui, en couleur, retourne le code. J'ai des problèmes car cela ne fonctionne pas et retourne toujours "01" pour chaque couleur autre que le rouge, ce qui renvoie le bon code. Quelqu'un a-t-il une idée à ce sujet?Développement Iphone: Comparaison des composants UIColor/CGColor

(NSString *) internalColorCode: (UIColor *) couleur {

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

}

Répondre

5

Le problème est avec le type de logique utilisée dans le code comme (0,949019 < = rouge < = 0,949021) .

Vous devez remplacer l'expression avec ((0,949019 < = rouge) & & (rouge = 0,949021 <)) à évaluer de la manière attendue.

Ainsi, toutes les vérifications de l'état du type (une < = Variable < = b) doivent être modifiés pour ((a < = variable) & & (Variable < = b)).

Ce qui suit est l'explication détaillée de la raison pour laquelle cela ne fonctionne pas.

La priorité de l'opérateur de < = est de gauche à droite. Par conséquent, l'expression est évaluée comme suit:

  1. D'abord 'a' par rapport à 'variable' et le résultat est évalué. Le RÉSULTAT (pas la 'variable') est ensuite comparé à 'b' et le résultat final est trouvé.

Ce n'est pas comme vous souhaitez évaluer l'expression.

+0

Maintenant ça marche, merci beaucoup. – w4nderlust