文章仅供参考,如有更好的编写方法,欢迎指正!!!
XFJColor.h文件
@interface XFJColor : NSObject
/**
* RGB转UIColor
* R : 红
* G : 绿
* B : 蓝
* Return : UIColor
*/
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b;
/**
* RGB转UIColor
* R : 红
* G : 绿
* B : 蓝
* A : 透明度
* Return : UIColor
*/
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b A:(float)a;
/**
* 十六进制字符串转换UIColor
* Color : 十六进制字符串
* Return : UIColor
*/
+ (UIColor *)colorWithHexString:(NSString *)color;
/**
* 十六进制字符串转换UIColor
* Color : 十六进制字符串
* A : 透明度
* Return : UIColor
*/
+ (UIColor *)colorWithHexString:(NSString *)color A:(float)a;
/**
* UIColor转十六进制字符串
* Color : UIColor
* Perfix : 返回十六进制字符串格式("#","0x")
* Return : 十六进制字符串
*/
+ (NSString *)hexadecimalFromUIColor:(UIColor*)color perfix:(NSString *)perfix;
@end
XFJColor.m文件
@implementation XFJColor
/**
* RGB转UIColor
* R : 红
* G : 绿
* B : 蓝
* Return : UIColor
*/
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b
{
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}
/**
* RGB转UIColor
* R : 红
* G : 绿
* B : 蓝
* A : 透明度
* Return : UIColor
*/
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b A:(float)a
{
return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:a];
}
/**
* 十六进制字符串转换UIColor
* Color : 十六进制字符串
* Return : UIColor
*/
+ (UIColor *)colorWithHexString:(NSString *)color
{
// 去空格换行
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 判断前缀
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// 从六位数值中找到RGB对应的位数并转换
NSRange range;
range.location = 0;
range.length = 2;
//R、G、B
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [self colorWithR:(float) r G:(float) g B:(float) b];
}
/**
* 十六进制字符串转换UIColor
* Color : 十六进制字符串
* A : 透明度
* Return : UIColor
*/
+ (UIColor *)colorWithHexString:(NSString *)color A:(float)a
{
// 去空格换行
NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) {
return [UIColor clearColor];
}
// 判断前缀
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor clearColor];
// 从六位数值中找到RGB对应的位数并转换
NSRange range;
range.location = 0;
range.length = 2;
//R、G、B
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [self colorWithR:(float) r G:(float) g B:(float) b A:a];
}
/**
* UIColor转十六进制字符串
* Color : UIColor
* Perfix : 返回十六进制字符串格式("#","0x")
* Return : 十六进制字符串
*/
+ (NSString *)hexadecimalFromUIColor:(UIColor*)color perfix:(NSString *)perfix
{
if(CGColorGetNumberOfComponents(color.CGColor) < 4)
{
const CGFloat *components =CGColorGetComponents(color.CGColor);
color = [UIColor colorWithRed:components[0]green:components[0]blue:components[0]alpha:components[1]];
}
if(CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) !=kCGColorSpaceModelRGB)
{
return [NSString stringWithFormat:@"#FFFFFF"];
}
NSString *r,*g,*b;
(int)((CGColorGetComponents(color.CGColor))[0]*255.0) == 0?(r =[NSString stringWithFormat:@"0%x",(int)((CGColorGetComponents(color.CGColor))[0]*255.0)]):(r= [NSString stringWithFormat:@"%x",(int)((CGColorGetComponents(color.CGColor))[0]*255.0)]);
(int)((CGColorGetComponents(color.CGColor))[1]*255.0)== 0?(g = [NSString stringWithFormat:@"0%x",(int)((CGColorGetComponents(color.CGColor))[1]*255.0)]):(g= [NSString stringWithFormat:@"%x",(int)((CGColorGetComponents(color.CGColor))[1]*255.0)]);
(int)((CGColorGetComponents(color.CGColor))[2]*255.0)== 0?(b = [NSString stringWithFormat:@"0%x",(int)((CGColorGetComponents(color.CGColor))[2]*255.0)]):(b= [NSString stringWithFormat:@"%x",(int)((CGColorGetComponents(color.CGColor))[2]*255.0)]);
return [NSString stringWithFormat:@"%@%@%@%@", perfix,r,g,b];
}
@end