xcode Macro 是否比 UIColor 更好地设置 RGB 颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1243201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Macro Better Than UIColor for Setting RGB Color?
提问by Rahul Vyas
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:
我在我的 .m 文件中使用它作为这样的东西:
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?
哪一种是更好的方法?
回答by woens
or create a separate category, so you only need to import one .h file:
或者创建一个单独的类别,所以你只需要导入一个 .h 文件:
@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
回答by Marius
How about 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.
使用起来似乎很简单,使用颜色的十六进制值并且不需要额外的计算开销。
回答by Tim
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:
中间立场可能是您的最佳选择。您可以定义常规 C 或目标 C 函数来执行您的宏现在正在执行的操作:
// 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. If I decide to call your macro with:
但是,如果您决定坚持使用该宏,则应该rgbValue
在它出现的任何地方加上括号。如果我决定使用以下命令调用您的宏:
UIColorFromRGB(0xFF0000 + 0x00CC00 + 0x000099);
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.
总而言之,我建议将您的宏重构为一个函数并保留它。
回答by Rob Napier
I typically recommend functions rather than complex #defines. If inlining has a real benefit, the compiler will generally do it for you. #defines make debugging difficult, particularly when they're complex (and this one is).
我通常推荐函数而不是复杂的#defines。如果内联有真正的好处,编译器通常会为您完成。#defines 使调试变得困难,特别是当它们很复杂时(这就是一个)。
But there's nothing wrong with using a function here. The only nitpick I'd say is that you should be using CGFloat rather than float, but there's nothing wrong with the hex notation if it's more comfortable for you. If you have a lot of these, I can see where using Web color notation may be convenient. But avoid macros.
但是在这里使用函数并没有错。我要说的唯一挑剔是你应该使用 CGFloat 而不是 float,但如果十六进制表示法对你来说更舒服,那么它没有任何问题。如果你有很多这些,我可以看到使用 Web 颜色表示法可能方便的地方。但避免使用宏。
回答by Eric Skroch
Keep in mind that 33 != 0x33. The first is decimal notation and the second is hexadecimal. They're both valid, but they are different. Your second option should read
请记住,33 != 0x33。第一个是十进制表示法,第二个是十六进制表示法。它们都是有效的,但它们是不同的。你的第二个选项应该是
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];
回答by Adriaan
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.
恕我直言,UIcolor 方法更具可读性。我认为宏如果能解决问题就很棒;即提供更高的性能和/或可读的代码。
It is not clear to me what the advantage of using a macro is in this case, so I'd prefer the second option.
我不清楚在这种情况下使用宏的好处是什么,所以我更喜欢第二种选择。
回答by Natchaphon
Nice Marius, but to compile I had to get rid of the parenthesis, as follows (otherwise, Objective C takes it literally and you get a syntax compilation error:
不错的 Marius,但是为了编译我必须去掉括号,如下所示(否则,Objective C 从字面上理解它,你会得到一个语法编译错误:
#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
...