Is Macro Better Than UIColor for Setting RGB Color?
صفحه اصلي

Is Macro Better Than UIColor for Setting RGB Color?



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?


Date Picker for iPhone Web Application

1:

Best analytics offering for iPhone
A middle ground might be your best option. C makefile to compile OpenGL project directly on iphoneYou could define either a regular C or objective-C function to did what your macro is doing now:. Checking online status from an iPhone web app
// As a C function: UIColor* UIColorFromRGB(NSInteger rgbValue) {     return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0                            green:((float)((rgbValue & 0xFF00) >> 8))/255.0                             blue:((float)(rgbValue & 0xFF))/255.0                            alpha:1.0]; }  // As an Objective-C function: - (UIColor *)UIColorFromRGB:(NSInteger)rgbValue { return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0                        green:((float)((rgbValue & 0xFF00) >> 8))/255.0                         blue:((float)(rgbValue & 0xFF))/255.0                        alpha:1.0]; }
If you decide to stick with the macro, though, you should put parentheses around rgbValue wherever it appears. How to create a GUID/UUID using the iPhone SDKIf I decide to call your macro with:. Accessing image data from a bitmap given a pointer
UIColorFromRGB(0xFF0000 + 0x00CC00 + 0x000099);
you may run into trouble.. What is better: Global string or singleton? The last bit of code is certainly the most readable, although probably the least portable - you can't call it simply from anywhere in your program.. How do I launch my settings bundle from my application? All in all, I'd suggest refactoring your macro into a function and leaving it at that..

2:

or create a separate category, so you only need to import one .h file:.
@interface UIColor (util) + (UIColor *) colorWithHexString:(NSString *)hex; + (UIColor *) colorWithHexValue: (NSInteger) hex; @end 
and.
#import "UIColor-util.h"  @implementation UIColor (util)  // Create a color using a string with a webcolor // ex. [UIColor colorWithHexString:@"#03047F"] + (UIColor *) colorWithHexString:(NSString *)hexstr {     NSScanner *scanner;     unsigned int rgbval;      scanner = [NSScanner scannerWithString: hexstr];     [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];     [scanner scanHexInt: &rgbval];      return [UIColor colorWithHexValue: rgbval]; }  // Create a color using a hex RGB value // ex. [UIColor colorWithHexValue: 0x03047F] + (UIColor *) colorWithHexValue: (NSInteger) rgbValue {     return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0                            green:((float)((rgbValue & 0xFF00) >> 8))/255.0                             blue:((float)(rgbValue & 0xFF))/255.0                            alpha:1.0];  }   @end 

3:

How around creating your own:.
#define RGB(r, g, b) \     [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] #define RGBA(r, g, b, a) \     [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)] 
Then use it:.
cell.textColor = RGB(0x66, 0x33, 0x33); 
Seems simple enough to use, uses hex values for colors and without needing additional calculation overhead..

4:

I typically recommend functions rather than complex #defines. If inlining has a real benefit, the compiler will generally did it for you. #defines make debugging difficult, particularly when they're complex (and this one is).. But there's nothing wrong with using a function here. The only nitpick I'd say is this you should be using CGFloat rather than float, although there's nothing wrong with the hex notation if it's more comfortable for you. If you have a lot of these, I must see where using Web color notation may be convenient. But avoid macros..

5:

I.m.h.o the UIcolor method is more readable. I think macro's are great if they solve a problem; i.e. provide more performance and/or readable code. . It is not clear to me what the advantage of using a macro is in this case, so I'd prefer the second option..

6:

Keep in mind this 33 != 0x33. The first is decimal notation and the second is hexadecimal. They're both valid, although they are different. Your second option should read.
cell.textColor = [UIColor colorWithRed:0x66/255.0                              green:0x33/255.0                               blue:0x33/255.0                              alpha:1.0]; 
or.
cell.textColor = [UIColor colorWithRed:102/255.0                              green:51/255.0                               blue:51/255.0                              alpha:1.0]; 

7:

Nice Marius, although to compile I had to receive rid of the parenthesis, as follows (otherwise, Objective C takes it literally and you receive a syntax compilation error:.
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0] ...  NSArray *palette; ...  palette = [NSArray arrayWithObjects:              RGB(0,0,0),              RGB(255,0,0), // red ... 


52 out of 100 based on 22 user ratings 382 reviews

*