Creating UIColor objects from hex values

It’s pretty inconvenient to create UIColor objects while developing apps for the iPhone, as you need to specific separate values for the RGB parts: red, green, blue.

I found a trick online a few weeks ago on how to automatically generate that code, by simply using a macro:

#define UIColorFromRGB(rgbValue) [UIColor \
    colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
    green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
    blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

The usage looks something like this:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];
    }
    cell.textColor = UIColorFromRGB(0x333333);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.text = @"Testing 1 2 3";
}

Hope that’s useful to someone.

20 Comments »

  1. Sahil said,

    December 7, 2008 @ 9:18 pm

    This is really great! thanks for putting it up, I’ve been trying to find something like this for a long time now. Combined with this (http://wafflesoftware.net/hexpicker/), it’s the easiest way to get concise colors.

  2. Ivan said,

    March 1, 2009 @ 8:54 am

    Thanks, this is very helpful.

    Cheers,
    Ivan

  3. Rodney Aiglstorfer said,

    March 1, 2009 @ 2:21 pm

    This is very helpful. Not being a C guy it would never have occured to me to create a macro to do this.

  4. Zachariah Cox said,

    August 10, 2009 @ 11:16 am

    thanks, I’m a java developer and macros are still kind of magical to me.

  5. Waqas Qureshi said,

    November 11, 2009 @ 10:12 pm

    It’s really marvelous concise code. It deals with the issue that I was solving :)

  6. theleo said,

    November 15, 2009 @ 4:03 pm

    Thanks it was helpful……….

  7. Edward said,

    January 7, 2010 @ 5:55 pm

    Awesome, that’s much simpler, thank you.

  8. ammad hussain said,

    February 16, 2010 @ 10:52 am

    i searched for 2 hours but of no use. then i found this and it saved my day.

  9. Ashar said,

    April 7, 2010 @ 12:45 pm

    Helpful post, thanks!

  10. Dustin Bachrach said,

    May 25, 2010 @ 10:33 am

    Here’s a couple extra macros I’ve built off of yours for some added functionality:

    #define UIColorFromRGBA(rgbValue, rgbAlpha) [UIColor \
    colorWithRed:((float)(((rgbValue) & 0xFF0000) >> 16))/255.0 \
    green:((float)(((rgbValue) & 0xFF00) >> 8))/255.0 \
    blue:((float)((rgbValue) & 0xFF))/255.0 alpha:(rgbAlpha)]

    #define UIColorFromRGB(rgbValue) UIColorFromRGBA(rgbValue, 1.0f)

    #define CGColorFromRGB(rgbValue) UIColorFromRGB((rgbValue)).CGColor

    #define CGColorFromRGBA(rgbValue, rgbAlpha) UIColorFromRGBA((rgbValue), (rgbAlpha)).CGColor

  11. links for 2010-06-17 at adam hoyle presents suckmypixel said,

    June 17, 2010 @ 6:30 am

    […] João Prado Maia’s Weblog » Creating UIColor objects from hex values "It’s pretty inconvenient to create UIColor objects while developing apps for the iPhone, as you need to specific separate values for the RGB parts: red, green, blue." (tags: iphone colour ui uicolor objective-c hex rgb) […]

  12. Jonas Schnelli said,

    July 1, 2010 @ 2:11 pm

    better use this category / extension:
    http://github.com/jonasschnelli/UIColor-i7HexColor

  13. Davidmoreen said,

    July 4, 2010 @ 4:20 am

    Thank you, this helped my out a lot!

  14. alok said,

    July 29, 2010 @ 7:38 am

    nice workaround !!! thanx a lot :).

  15. Andrés Mejía said,

    September 27, 2010 @ 6:39 am

    I was about to write something similar myself.

  16. James said,

    November 1, 2010 @ 6:03 am

    Very handy, cheers!

  17. Mahmud Ahsan said,

    December 28, 2010 @ 8:05 am

    It helps me. Thank You.

  18. Darran said,

    January 8, 2011 @ 8:12 pm

    Excellent. Exactly what I was looking for. Thanks.

  19. Tristan said,

    January 17, 2011 @ 1:19 am

    Terrific work!

  20. Joseph DeCarlo said,

    August 23, 2011 @ 2:15 pm

    Rather than use the category that Jonas referred to, I just converted the macro to a category and it works great!

    @implementation UIColor (HexValue)

    + (UIColor*) colorWithHexValue:(NSInteger)hexColor {

    return [UIColor colorWithRed:((float)((hexColor & 0xFF0000) >> 16))/255.0 green:((float)((hexColor & 0xFF00) >> 8))/255.0 blue:((float)(hexColor & 0xFF))/255.0 alpha:1.0];
    }

    @end

RSS feed for comments on this post · TrackBack URI

Leave a Comment