iOS根据hex值获取颜色UIColor
2020-04-19 本文已影响0人
MiniCoder
在开发过程中,很多时候设计给我都是hex格式的颜色值,那么我们这个转成我们需要RGB颜色。
2710245-35d6e212c015af5b.png 2710245-fb982ecbb363a16c.jpg- 在转换过程中,其实hex值如果是16进制的字符串的话,那么会有固定的长度,如果抛开可能会出现的#,0x的话,那么可能出现的字符长度就是3,6,8.长度是3的话,其实应该是6我们要按位重复添加比如345,那么应该转换成334455,然后获取颜色值。
8位长度的其实包括alpha的值,在前两位。
一般我们都是将16进制的颜色值,根据固定的位置取出颜色值,依次是alpha,red,green,blue. - 还有一种是10进制的颜色值。
设置颜色相关宏定义
#define ssRGB(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define ssRGBAlpha(r, g, b, a) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]
十六进制颜色 Hex
#define ssRGBHex(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]
#define ssRGBHexAlpha(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)]
十进制的颜色转换
///十进制的颜色转换
+ (UIColor *)colorWithHex:(NSInteger)hex {
return [UIColor colorWithRed:((float)((hex & 0xff0000) >> 16))/255.0 green:((float)((hex & 0x00ff00) >> 8))/255.0 blue:((float)(hex & 0x0000ff))/255.0 alpha:1.0];
}
16进制值转换成颜色
///将hex值转换成颜色
+(UIColor*)colorFromHex:(NSString*)hex{
NSString * newHex = hex;
if ([newHex hasPrefix:@"#"]) {
newHex = [newHex substringFromIndex:1];
}
if ([newHex hasPrefix:@"0x"]) {
newHex = [newHex substringFromIndex:2];
}
if (newHex.length != 3 && newHex.length != 6 && newHex.length != 8 && newHex.length < 3) {
return UIColor.clearColor;
}
NSRange range = NSMakeRange(0, 2);
unsigned int alpha = 255,red = 0,green = 0,blue = 0;
//Check for short hex strings
if(newHex.length == 3) {
//Convert to full length hex string
newHex = [NSString stringWithFormat:@"%@%@%@%@%@%@",
[newHex substringWithRange:NSMakeRange(0, 1)],[newHex substringWithRange:NSMakeRange(0, 1)],
[newHex substringWithRange:NSMakeRange(1, 1)],[newHex substringWithRange:NSMakeRange(1, 1)],
[newHex substringWithRange:NSMakeRange(2, 1)],[newHex substringWithRange:NSMakeRange(2, 1)]];
}
if (newHex.length == 8) {
NSString * alphaStr = [newHex substringWithRange:range];
range.location = 2;
NSString * redStr = [newHex substringWithRange:range];
range.location = 4;
NSString * greenStr = [newHex substringWithRange:range];
range.location = 6;
NSString * blueStr = [newHex substringWithRange:range];
[[NSScanner scannerWithString:alphaStr] scanHexInt:&alpha];
[[NSScanner scannerWithString:redStr] scanHexInt:&red];
[[NSScanner scannerWithString:greenStr] scanHexInt:&green];
[[NSScanner scannerWithString:blueStr] scanHexInt:&blue];
return [UIColor colorWithRed:[self transferColorValue:red] green:[self transferColorValue:green] blue:[self transferColorValue:blue] alpha:[self transferColorValue:alpha]];
}
if (newHex.length == 6) {
NSString * redStr = [newHex substringWithRange:range];
range.location = 2;
NSString * greenStr = [newHex substringWithRange:range];
range.location = 4;
NSString * blueStr = [newHex substringWithRange:range];
[[NSScanner scannerWithString:redStr] scanHexInt:&red];
[[NSScanner scannerWithString:greenStr] scanHexInt:&green];
[[NSScanner scannerWithString:blueStr] scanHexInt:&blue];
return [UIColor colorWithRed:[self transferColorValue:red] green:[self transferColorValue:green] blue:[self transferColorValue:blue] alpha:[self transferColorValue:alpha]];
}
return UIColor.clearColor;
}
+(CGFloat)transferColorValue:(unsigned int)value{
return value/255.0;
}
下面是另一种写法
+(UIColor *) colorWithHexString: (NSString *) hexString
{
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case 3: // #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 1];
green = [self colorComponentFrom: colorString start: 1 length: 1];
blue = [self colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
red = [self colorComponentFrom: colorString start: 1 length: 1];
green = [self colorComponentFrom: colorString start: 2 length: 1];
blue = [self colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
red = [self colorComponentFrom: colorString start: 2 length: 2];
green = [self colorComponentFrom: colorString start: 4 length: 2];
blue = [self colorComponentFrom: colorString start: 6 length: 2];
break;
default:
blue=0;
green=0;
red=0;
alpha=0;
break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
+(CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length
{
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
@end
- 将颜色转成hex值
///将颜色转成hex值
- (NSString *)hexString {
const CGFloat * value = CGColorGetComponents(self.CGColor);
CGFloat alpha = CGColorGetAlpha(self.CGColor);
CGFloat red, green, blue;
red = roundf(value[0] * 255.f);
green = roundf(value[1] * 255.f);
blue = roundf(value[2] * 255.f);
alpha = roundf(alpha * 255.f);
uint hex = ((uint)alpha << 24) | ((uint)red << 16) | ((uint)green << 8) | ((uint)blue);
NSLog(@"%u",hex);
return [NSString stringWithFormat:@"#%08x", hex];
}
获取颜色的RGB值
+ (NSArray *)convertColorToRGB:(UIColor *)color {
NSInteger numComponents = CGColorGetNumberOfComponents(color.CGColor);
NSArray *array = nil;
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
array = @[@((int)(components[0] * 255)),
@((int)(components[1] * 255)),
@((int)(components[2] * 255))];
}
return array;
}