 |
 |
|
 |
07-25-2011, 10:33 AM
|
#1 (permalink)
|
|
Indie Developer
Join Date: Jul 2010
Posts: 1,346
|
Hex to RGB / UIColor + RGB
Hey,
I just coded a Hex to RGB/UIColor category for iOS, let me know your thoughts:
Code:
@interface UIColor (UIColor_Hex)
+ (UIColor *) colorWithHex:(NSString *) hexString;
@end
Code:
@implementation UIColor (UIColor_Hex)
+ (UIColor *) colorWithHex:(NSString *)hexString {
if (!hexString)
return nil;
hexString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
hexString = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (hexString.length > 6)
return nil;
float firstPairInt, secondPairInt, thirdPairInt;
NSString *firstPairString, *secondPairString, *thirdPairString;
firstPairString = [hexString substringToIndex:2];
secondPairString = [[hexString substringFromIndex:2] substringToIndex:2];
thirdPairString = [[hexString substringFromIndex:4] substringToIndex:2];
NSString *firstLetter, *secondLetter, *thirdLetter, *fourthLetter, *fifthLetter, *sixthLetter;
firstLetter = [firstPairString substringToIndex:1];
secondLetter = [[firstPairString substringFromIndex:1] substringToIndex:1];
thirdLetter = [secondPairString substringToIndex:1];
fourthLetter = [[secondPairString substringFromIndex:1] substringToIndex:1];
fifthLetter = [thirdPairString substringToIndex:1];
sixthLetter = [[thirdPairString substringFromIndex:1] substringToIndex:1];
NSArray *letters = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"A", @"B", @"C", @"D", @"E", @"F", nil];
NSArray *numbers = [NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", nil];
int firstLetterInt, secondLetterInt, thirdLetterInt, fourthLetterInt, fifthLetterInt, sixthLetterInt;
for (int i = 0; i < [letters count]; i++) {
if ([firstLetter isEqualToString:[letters objectAtIndex:i]]) {
firstLetterInt = [[numbers objectAtIndex:i] intValue];
}
if ([secondLetter isEqualToString:[letters objectAtIndex:i]]) {
secondLetterInt = [[numbers objectAtIndex:i] intValue];
}
if ([thirdLetter isEqualToString:[letters objectAtIndex:i]]) {
thirdLetterInt = [[numbers objectAtIndex:i] intValue];
}
if ([fourthLetter isEqualToString:[letters objectAtIndex:i]]) {
fourthLetterInt = [[numbers objectAtIndex:i] intValue];
}
if ([fifthLetter isEqualToString:[letters objectAtIndex:i]]) {
fifthLetterInt = [[numbers objectAtIndex:i] intValue];
}
if ([sixthLetter isEqualToString:[letters objectAtIndex:i]]) {
sixthLetterInt = [[numbers objectAtIndex:i] intValue];
}
}
firstPairInt = ((firstLetterInt * 16) + secondLetterInt) / 255.0f;
secondPairInt = ((thirdLetterInt * 16) + fourthLetterInt) / 255.0f;
thirdPairInt = ((fifthLetterInt * 16) + fifthLetterInt) / 255.0f;
return [UIColor colorWithRed:firstPairInt green:secondPairInt blue:thirdPairInt alpha:1.0];
}
@end
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is On
|
|
|
|
» Advertisements |
» Online Users: 471 |
| 13 members and 458 guests |
| alexeir, David-T, Dj_kades, foslock, iAppDeveloper, jeroenkeij, LunarMoon, Mijator, pipposanta, QuantumDoja, robsmy, sacha1996, usernametaken |
| Most users ever online was 1,387, 04-10-2012 at 04:21 AM. |
» Stats |
Members: 175,679
Threads: 94,129
Posts: 402,928
Top Poster: BrianSlick (7,990)
|
| Welcome to our newest member, xzoonxoom |
|