Last Updated: February 25, 2016
·
1.994K
· saturngod

RGB to UIColor

Declare in your app .pch

#define rgb(R,G,B) [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:1.0]

#define rgba(R,G,B,A) [UIColor colorWithRed:R/255.0f green:G/255.0f blue:B/255.0f alpha:A]

After that you can use like

mylabel.textColor = rgb(231, 76, 60);
myview.backgroundColor = rgba(231, 76, 60,0.2); 
// Alpaha value max is 1 and min is 0 , you must use float point

4 Responses
Add your response

If you work with designers they'll mostly send you hex color values. I use this:

#define UIColorFromHEX(hex, a)     [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:a]

Just use it like

view.bacgkroundColor = UIColorFromHex(#585858, 1.0);
over 1 year ago ·

@mrnickbarker , thank you.

over 1 year ago ·

Both great solutions - thanks guys!

over 1 year ago ·

Use HSB not RGB because it is the way we think. That is hue: the color, saturation: how strong the color is, brightness, how dark/light the color is. If you use RGB and are asked to make the color less saturated that is going to be a problem, in HSB it is intuitively easy. Consider that Apple used 215° hue extensively but with different saturations and brightness.

over 1 year ago ·