iOS中为UIColor创建类别 其中添加了几个常用的方法
// UIColor+UIColor_HexConvert.h#import
@inte易做图ce UIColor (HexConvert)
+(UIColor *)colorFromHexString:(NSString*)colorString;
+(NSString*)stringFromColor:(UIColor*)color;
-(void)getRGBComponents:(CGFloat [3])components;
@end
// UIColor+UIColor_HexConvert.m
#import "UIColor+HexConvert.h"
@implementation UIColor (HexConvert)
+(UIColor *)colorFromHexString:(NSString *)colorString
{
NSString *cString = [[colorString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6)
return [UIColor whiteColor];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor whiteColor];
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
+(NSString *)stringFromColor:(UIColor *)color
{
const CGFloat *cs=CGColorGetComponents(color.CGColor);
NSString *r = [NSString stringWithFormat:@"%@",[self ToHex:cs[0]*255]];
NSString *g = [NSString stringWithFormat:@"%@",[self ToHex:cs[1]*255]];
NSString *b = [NSString stringWithFormat:@"%@",[self ToHex:cs[2]*255]];
return [NSString stringWithFormat:@"#%@%@%@",r,g,b];
}
+(NSString *)ToHex:(int)tmpid
{
NSString *endtmp=@"";
NSString *nLetterValue;
NSString *nStrat;
int ttmpig=tmpid%16;
int tmp=tmpid/16;
switch (ttmpig)
{
case 10:
nLetterValue =@"A";break;
case 11:
nLetterValue =@"B";break;
case 12:
nLetterValue =@"C";break;
case 13:
nLetterValue =@"D";break;
case 14:
nLetterValue =@"E";break;
case 15:
nLetterValue =@"F";break;
default:nLetterValue=[[NSString alloc]initWithFormat:@"%i",ttmpig];
}
switch (tmp)
{
case 10:
nStrat =@"A";break;
case 11:
nStrat =@"B";break;
case 12:
nStrat =@"C";break;
case 13:
nStrat =@"D";break;
case 14:
nStrat =@"E";break;
case 15:
nStrat =@"F";break;
default:nStrat=[[NSString alloc]initWithFormat:@"%i",tmp];
}
endtmp=[[NSString alloc]initWithFormat:@"%@%@",nStrat,nLetterValue];
return endtmp;
}
- (void)getRGBComponents:(CGFloat [3])components
{
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char resultingPixel[4];
CGContextRef context = CGBitmapContextCreate(&resultingPixel,
1,
1,
8,
4,
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGContextSetFillColorWithColor(context, [self CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGContextRelease(context);
CGColorSpaceRelease(rgbColorSpace);
for (int component = 0; component < 3; component++) {
components[component
补充:移动开发 , IOS ,
- 更多wap疑问解答:
- 新人求助QPainter
- 为什么程序都退出了还可以收到推送?如果大多设备都可以推送那运营商怎么办?
- qt 4.7 sqlserver2000 存储过程调用
- 关于ANDROID4.0.1编译问题!
- Android FrameBuffer读屏幕30秒后mmap失败
- 联通粗定位用java程序如何来请求和接受数据
- 为什么QT运行Android平台的程序时,mouseMoveEvent事件响应的间隔时间很长??????????
- android与PC蓝牙通讯
- 指定大小的label 内容可变,如果内容超出label的宽度,将未能显示的部分显示在另一个label上
- Android调试
- android如何通过wifi连接无线打印机
- 运行程序,release目录下产生一个乱码文件夹
- 分享个某机构最新安卓资料,自己验证了
- service启动不起来,掉不了service connection
- 求助:QT5.0 没有QPrinter吗