通过位与方法获取三色值,十六进制色值转换RGB


//通过位与方法获取三色值

UIView *tView =[[UIView alloc]initWithFrame:CGRectMake(10, 10, 300, 40)];

tView.backgroundColor = [self colorWithHexString:@"345643"];

[self.view addSubview:tView];



- (UIColor *)colorWithHexString:(NSString *)colorString

{

    unsigned long colorLong = strtoul(colorString.UTF8String, 0, 16);

    //通过位与方法获取三色值

    int R = (colorLong & 0xFF0000) >> 16;

    int G = (colorLong & 0x00FF00) >> 8;

    int B = colorLong & 0x0000FF;

    

    return [UIColor colorWithRed:R / 255.0 green:G / 255.0 blue:B / 255.0 alpha:1.f];

}



#define UIColorFromRGB(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]


使用方法:例如上图表示  label.titleColor = UIColorFromRGB(0xbdbdbd); 就好了


#define RGBCOLORV(rgbValue) [UIColor \

colorWithRed:((float)((rgbValue & 0xFF0000) >>16))/255.0 \

green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \

blue:((float)(rgbValue & 0x0000FF))/255.0 \

alpha:1.0]


/* 颜色相关 */

#define UIColorFromRGB(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 RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

#define RGBCOLORV(rgbValue) [UIColor \

colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \

green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \

blue:((float)(rgbValue & 0x0000FF))/255.0 \

alpha:1.0]

#define RGBCOLORVA(rgbValue, alphaValue) [UIColor \

colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \

green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \

blue:((float)(rgbValue & 0x0000FF))/255.0 \

alpha:alphaValue]



你可能感兴趣的:(UIColor,iOS,十六进制色值转换RGB,UIColor,字符串取颜色色值)