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.
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.
Comment by Amin Negm-Awad — 19.11.2008 @ 10:54 |
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!
Comment by pegolon — 19.11.2008 @ 11:01 |
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
Comment by Amin Negm-Awad — 19.11.2008 @ 11:54 |
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?
Comment by Torsten Curdt — 31.08.2009 @ 11:30 |
Thank you, Torsten!
Comment by pegolon — 31.08.2009 @ 11:36
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
Comment by Craig Cunic — 13.12.2008 @ 13:20 |
actually I did not look in detail about the comments. Anyway, this is very useful for me, thx a lot!
Comment by walty — 18.12.2008 @ 11:22 |
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.
Comment by Chris the wild man — 08.06.2009 @ 14:35 |
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.
Cheers,
Markus
Comment by pegolon — 12.06.2009 @ 4:58 |
@ 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!
Comment by mr Aloha — 02.10.2009 @ 9:57 |
Thank you very much, Mr Aloha, for being so supportive.
Comment by pegolon — 03.10.2009 @ 5:44 |