Quick iPhone Dev Tip: Creating an UIColor with just one RGB value

Are you also tired of splitting that long RGB value into red, green and blue and converting them to a format that UIColor understands? I was, so I wrote a macro that converts the value on compile time and returns an UIColor autorelease object with the correct values:

#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]

In your code you can use it like this:

UIColor myGreen = UIColorFromRGB(0x00FF00);

Put this in your global header referenced from <project_name>_Prefix.pch and you can use it everywhere in your project without importing it excplicitly.


20 thoughts on “Quick iPhone Dev Tip: Creating an UIColor with just one RGB value

  1. This macro is definetly against Objectiv-C programming style.

    1. Why do you use Hex-RGB values in your code? ‘Cause you know it? This is no reason to do so.

    2. FromRGB? RGB-what? Do you think, that a 24-bit rgb value is a standard, that everybody has to use? Why? Because modern computers uses this as an *internal* representation? the [0.0-1.0] range is without any context to anything. You should prefer this range.

    3. However: If you need such a translation, add a category instead of a macro. Macros are made by the devil sometime in the last century. When Cox added categoriieys to Objective-C he was *not* joking.

  2. No need to be so rude! If you don’t want to use it: don’t use it.

    1. I get the hex codes from the designers and I am too lazy to convert them by hand into these strange floating point values.

    2. Not everyone, only the people who find it handy. I am not defining a framework here.

    3. I also first created a category, but then I made the macro because it is an unnecessary computation made each time the call is made versus the macro which is called only during compile time. If you use this in your UITableViewCell you have to think about these things.

    Feel free to troll at someone else’s blog!

  3. I don’t use it and I feel free to comment it. And I think, that it is a good idea to comment it. Otherwise readers of this article will think, that this code is good design.

    1. This floating point values are not strange. Using the rgb color system you mix up components. It is a very usual way to specify a compnents by a range between 0 and 1. And as I said: It is without a context, especially a context as “typically color encoding on a devie developed in the early 2000’s”

    In contrast to this a 24-bit expression (any integer range) of the components are related to a specific context (in this case: usual encoding of colors on a computer). if a device supports a better color resolutions than a random integer range, you will not get it.

    2. The correct representation of a value does not depend on the fact whether you are developing a framework or not. You represent the value correct or not.

    3. We have three floating point computations for that and one method dispatch. This is no problem. However this computation does not apologize poor design. If it is too slow, you have to caluclate it your own.

    Simply try this:
    int colorIndex =0;
    while( colorTable[colorIndex] ) {
    UIColor shade = UIColorFromRGB( color[colorIndex++] );
    // …

    Result: Probably a crashing app, definetly not the intended result

    • The only problem I see here is the computation vs the dispatch. Arguing that it’s the “right thing” ™ to use a 0..1 range is useless as this is just a convenience macro. I don’t see what is so wrong to define the color components in the 0..255 range instead. After all it’s just a factor. And for what it’s worth – most designer (I have worked with) do provide colors in that very format. Those hex digits are everywhere. Maybe you need to do a reality check there, Amin? 😉

  4. Very handy and very ridiculous to categorize as “bad design”
    New to Mac and iPhone.
    This is what I would have expected the foundation to have

  5. actually I did not look in detail about the comments. Anyway, this is very useful for me, thx a lot!

  6. I can not seem to figure out how to put an nsstring into this function. I’ve been searching everywhere. Please help! Thanks for the code.

    • What kind of string do you want to put in it? BTW: it is no function, it is a macro, so it will be converted by the C preprocessor into plain C code.

  7. @ this Amin guy: I’ve come across your comments here and there every once in a while. And your attitude slowly starts to annoy me. We all try to get our work done in one way or another and we try to meet the projects requirements as good as we can. So I believe it does not help anyone to criticise his/her approach in this way. Concerning this specific matter: May be your projects are not as visually oriented as others’. In my world designers talk Hex colors. Photoshop just does not show colors with values ranging from 0 to 1. If you prefer to translate the color by hand that’s cool for you BUT I have to skin my application with a whole lot of colors I get in Hex. This little macro does exactly what I need so please relax and let us do things our way. – Talking about bad/good code: Hex colors make code easier for me since I can find my colors and alter them. For me three values ranging from 0 to 1 each seem strange and I can’t interpretate them. They don’t tell me anything. So please stay in your pseudo scientific world, write your next book and let us others get our work done!

    • Its so obvious that Im pretty sure that kind of method will be avaliable soon in the next SDK. Its a MUST! Thanks for your work, Mr FriendOfDesigner!

  8. My 5 cents :

    put the macro and an #if and make it barf if building for production 🙂
    everybody win – trough the design process you can use web hex colors, and before releasing you just turn all hex-es into rgbs

    How about that ? Otherwise, as a person fluent in hex colors, I find the macro just great !

  9. This Amin Negm-Awad guy gotta be the rudest man alive. Some people just have so much hatred inside them that they need to spew their guts out on inventive people like you Torsten. Or they have severe communication problems rendering them in a constant frustrated state making them insensible and totally asocial.

    I was finding myself doing colorWithRed:223.0/255.0 green:236.0/255.0 blue:242.0/255.0 alpha:0.5 etc all them time and your macro is beautiful. Thanks a million

  10. Thanks for the tip .. Wow! to think that this concept is “wrong?” I certainly don’t get that.

    I’m a PHP/HTML programmer normally and I’ve been using standard RGB (0..255) for years. This convenient macro makes a whole lot more sense to me than (0..1). I certainly don’t think I’m out of touch.

  11. Pingback: RGB값으로 UIColor생성하기 - d'mind project

  12. Pingback: Tint UIButton and UIBarButtonItem : MrBrown blob

  13. Pingback: MrBrown blob » Tint UIButton and UIBarButtonItem

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s