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.
blackColor
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: iphone-dev, RGB, UIColor
Thanks for this code snippet.
Nice, I’m using this now!
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?
@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.
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?
great work thank you to share this sheet it’s very helpful
you can use http://uicolor.org ,a uicolor auto generator,it’s much more easy to use
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.