I have this macro in my header file:
#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]
And I am using this as something like this in my .m file:
cell.textColor = UIColorFromRGB(0x663333);
So I want to ask everyone is this better or should I use this approach:
cell.textColor = [UIColor colorWithRed:66/255.0
green:33/255.0
blue:33/255.0
alpha:1.0];
Which one is the better approach?
A middle ground might be your best option. You could define either a regular C or objective-C function to do what your macro is doing now:
If you decide to stick with the macro, though, you should put parentheses around
rgbValuewherever it appears. If I decide to call your macro with:you may run into trouble.
The last bit of code is certainly the most readable, but probably the least portable – you can’t call it simply from anywhere in your program.
All in all, I’d suggest refactoring your macro into a function and leaving it at that.