博客> 标准16进制数转RGB, alpha
标准16进制数转RGB, alpha
2017-10-18 16:24 评论:0 阅读:505 lvhahaha
UIColor 16进制

-(UIColor *)stringToColor {

// 十六进制字符串转成整形。
long colorLong = strtoul([[self stringByReplacingOccurrencesOfString:[self substringWithRange:NSMakeRange(0, 3)]withString:@"0x"] cStringUsingEncoding:NSUTF8StringEncoding], 0, 16);
// 通过位与方法获取三色值
int R = (colorLong & 0xFF0000 )>>16;
int G = (colorLong & 0x00FF00 )>>8;
int B =  colorLong & 0x0000FF;

//string转color
return [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1];

}

-(float)stringToColorAlpha { NSString *alphaStr = [self substringWithRange:NSMakeRange(1, 2)];

return [self alphaWithAlphaStr:alphaStr];

}

/** 00%=FF(不透明) 5%=F2 10%=E5 15%=D8 20%=CC 25%=BF 30%=B2 35%=A5 40%=99 45%=8c 50%=7F 55%=72 60%=66 65%=59 70%=4c 75%=3F 80%=33 85%=21 90%=19 95%=0c 100%=00

*/

  • (float)alphaWithAlphaStr:(NSString *)alphaStr { if ([alphaStr isEqualToString:@"FF"] || [alphaStr isEqualToString:@"ff"]) { return 1.00; } else if ([alphaStr isEqualToString:@"F2"] || [alphaStr isEqualToString:@"f2"]) { return 0.10; } else if ([alphaStr isEqualToString:@"7F"] || [alphaStr isEqualToString:@"7f"]) { return 0.50; }else if ([alphaStr isEqualToString:@"72"]) { return 0.55; }else if ([alphaStr isEqualToString:@"66"]) { return 0.60; }else if ([alphaStr isEqualToString:@"59"]) { return 0.65; }else if ([alphaStr isEqualToString:@"4c"] || [alphaStr isEqualToString:@"4C"]) { return 0.70; }else if ([alphaStr isEqualToString:@"3F"] || [alphaStr isEqualToString:@"3f"]) { return 0.75; }else if ([alphaStr isEqualToString:@"33"]) { return 0.80; }else if ([alphaStr isEqualToString:@"21"]) { return 0.85; } else if ([alphaStr isEqualToString:@"19"]) { return 0.90; } else if ([alphaStr isEqualToString:@"0c"]) { return 0.95; }else { return 0.05; }

}

收藏
0
sina weixin mail 回到顶部