给定一种颜色,获取颜色的A R G B值

#pragma mark - 获取颜色的 argb
- ( void )getRGBComponents:( CGFloat [ 4 ])components forColor:( UIColor *)color {
   
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB ();
   
unsigned char resultingPixel[ 4 ];
   
CGContextRef context = CGBitmapContextCreate (&resultingPixel,
                                                
1 ,
                                                
1 ,
                                                
8 ,
                                                
4 ,
                                                 rgbColorSpace,
                                                 (
CGBitmapInfo ) kCGImageAlphaPremultipliedLast );
   
CGContextSetFillColorWithColor (context, [color CGColor ]);
   
CGContextFillRect (context, CGRectMake ( 0 , 0 , 1 , 1 ));
   
CGContextRelease (context);
   
CGColorSpaceRelease (rgbColorSpace);
   
   
for ( int component = 0 ; component < 4 ; component++) {
        components[component] = resultingPixel[component] /
255.0f ;
    }
}

使用:
// 获取线的颜色的 argb
        CGFloat components[ 4 ];
        [
self getRGBComponents :components forColor : self . drawView . strokeColor ];

component数组中保存的就是r,g,b,a的值了。

你可能感兴趣的:(ios,颜色,RGB,ARGB,获取颜色argb)