UIColor cheatsheet: color list, conversion from and to RGB values

First, here is lies a list of all “default” UIColors and their graphic representation. May become handy in case I forget how actually terrible some of these colors look.

blackColoriphone uicolors

darkGrayColor

lightGrayColor

whiteColor

grayColor

redColor

greenColor

blueColor

cyanColor

yellowColor

magentaColor

orangeColor

purpleColor

brownColor

clearColor

UIColor from RGB

In case default colors is not enough we can always construct our “own” color using RGB (red, green, blue) values like this:

[[UIColor alloc] initWithRed:20.0 / 255 green:59.0 / 255 blue:102.0 / 255 alpha:1.0]

And in case default colors is not enough and we hate to looking for RGB values every time we need to use a color macros or categories comes in handy. Personally I like this solution, because everything you need – add code snippet below to your applications global header and then use CSS-ish HEX values to construct desired UIColor object.

#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]
//usage
UIColor color = UIColorFromRGB(0xF7F7F7);

Extracting RGB values from UIColor

UIColor color = [[UIColor redColor] retain];
CGColorRef colorref = [color CGColor];

int numComponents = CGColorGetNumberOfComponents(colorref);

if (numComponents == 4) {
  const CGFloat *components = CGColorGetComponents(colorref);
  CGFloat red     = components[0];
  CGFloat green = components[1];
  CGFloat blue   = components[2];
  CGFloat alpha = components[3];
}
[color release];

Tags: , ,

11 responses

  1. Karib
    Posted August 6, 2010 at 14:17 | Permalink

    Thanks for this code snippet.

  2. Simone
    Posted October 29, 2010 at 16:30 | Permalink

    Nice, I’m using this now!

  3. Posted November 15, 2010 at 10:18 | Permalink

    Hi

    I have used the same code.

    UIColor *uicolor = [[UIColor blackColor] retain];
    CGColorRef color = [uicolor CGColor];

    int numComponents = CGColorGetNumberOfComponents(color);
    const CGFloat *components = CGColorGetComponents(color);
    if (numComponents == 4)
    {

    CGFloat red = components[0];
    CGFloat green = components[1];
    CGFloat blue = components[2];
    CGFloat alpha = components[3];
    NSLog(@”red:%f\ngreen:%f\nblue:%f\nalpha:%f\n\n”, red, green, blue, alpha);
    }

    But

    It is not printing the components when i pass black color. But in m,y iphone application,
    There is a hard need of red blue green components of a black color. how can i fix this.?

    Can you tell me the answer ASAP?

  4. Tamás
    Posted November 20, 2010 at 12:31 | Permalink

    @Ganesh: black, gray and white colors use only two components instead of four; so, if numComponents == 2, use components[0] for red, green and blue values and components[1] for alpha.

  5. Posted March 27, 2011 at 11:10 | Permalink

    perfect ive just had my birthday and i wan an iphone four but i have heard that the iphone 5 is coming out this yr, so when is it coming out? and, is it that small iphone i heard about or will it be standard size?

  6. Posted May 27, 2012 at 16:22 | Permalink

    great work thank you to share this sheet it’s very helpful

  7. Rache
    Posted September 13, 2012 at 11:40 | Permalink

    you can use http://uicolor.org ,a uicolor auto generator,it’s much more easy to use

  8. Posted May 21, 2013 at 09:28 | Permalink

    Heya i’m for the first time here. I found this board and I find It truly helpful & it helped me out much. I am hoping to present one thing back and aid others such as you helped me.

Leave a Reply