Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials

Reply
 
LinkBack Thread Tools Display Modes
Old 07-25-2011, 10:33 AM   #1 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default 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
iSDK is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0