18 Jun 2009

#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 \
                                    green:((c>>16)&0xFF)/255.0 \
                                     blue:((c>>8)&0xFF)/255.0 \
                                    alpha:((c)&0xFF)/255.0]

Usage: UIColor *c = HEXCOLOR(0xff0000);.

If you want a macro for RGB (above is for RGBA), replace alpha: with alpha:1.0.

Courtesy of this thread.

Add Your Comment

Spam Protection by WP-SpamFree

Hex/HTML Color (RGBA, RGB) to UIColor

  1. And here I was so hoping for a HTML or Flash widget that would convert for me…

    Richard
  2. Shouldn’t it be UIColor *c = HEXCOLOR(0xff0000ff); ?

    Any Way very nice workaround, thanks Aral :)

    Idden