本文主要是介绍#FFFFFF 和RGB的相互转化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#00b3e9 --->RGB(0,179,233)的转化
float R = (float)((0x00b3e9 & 0xFF0000)>>16); --> 0
float G = (float)((0x00b3e9 & 0xFF00)>>8); --> 179
float B = (float)((0x00b3e9 & 0xFF)); --> 233
计算方法:按位与 后按照相应位数右移
+ (UIColor*) colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
return [UIColorcolorWithRed:((float)((hexValue &0xFF0000) >> 16))/255.0
green:((float)((hexValue &0xFF00) >>8))/255.0
blue:((float)(hexValue &0xFF))/255.0alpha:alphaValue];
}
//RGB--->16进制色值
+ (NSString *) hexFromUIColor: (UIColor*) color {
if (CGColorGetNumberOfComponents(color.CGColor) <4) {
constCGFloat *components =CGColorGetComponents(color.CGColor);
color = [UIColorcolorWithRed:components[0]
green:components[0]
blue:components[0]
alpha:components[1]];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
return [NSStringstringWithFormat:@"#FFFFFF"];
}
return [NSStringstringWithFormat:@"#%x%x%x", (int)((CGColorGetComponents(color.CGColor))[0]*255.0),
(int)((CGColorGetComponents(color.CGColor))[1]*255.0),
(int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}
这篇关于#FFFFFF 和RGB的相互转化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!