最近在做Objective-C的开发,发现在Objective-C中使用UIColor生成颜色需要的参数不是我们一般使用的0-255和十六进制表示方法,而是0-1的小数表示方式(如:UIColor* color = [UIColor colorWithRed:0.79 green:0.87 blue:0.91 alpha:1];),很不习惯,所以用flash写了个转换的小工具放上来以便有需要的朋友使用。
补发两个在Objective-C中取颜色的宏:
//
RGB color macro
#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 ]
// RGB color macro with alpha
#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor \
colorWithRed:(( float )((rgbValue & 0xFF0000 ) >> 16 )) / 255.0 \
green:(( float )((rgbValue & 0xFF00 ) >> 8 )) / 255.0 \
blue:(( float )(rgbValue & 0xFF )) / 255.0 alpha:a]
#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 ]
// RGB color macro with alpha
#define UIColorFromRGBWithAlpha(rgbValue,a) [UIColor \
colorWithRed:(( float )((rgbValue & 0xFF0000 ) >> 16 )) / 255.0 \
green:(( float )((rgbValue & 0xFF00 ) >> 8 )) / 255.0 \
blue:(( float )(rgbValue & 0xFF )) / 255.0 alpha:a]