UIColor and components

UIColor是iOS小伙伴们经常运用的一个类,也是Apple提供的一个很好用的颜色设置的类。今天要说到的是通过UIColor如何正确取得color components对应的R、G、B,及alpha的值(没听过components概念的童鞋自行度娘or谷哥下~~)。

通常在OC中设置颜色,主要是通过以下这几种方式设置(当然也不排除部分童鞋运用其他一些方法哈~~):

UIColor *color = [UIColor whiteColor];
UIColor *color = [UIColor colorWithWhite: alpha:];
UIColor *color = [UIColor colorWithRed: green: blue: alpha:];

然后,获取color components的方法也很简单:

// 1.先将UIColor转换为对应的CGColor
 CGColorRef cgColor = [color CGColor];

 // 2.得到components
 const CGFloat *components = CGColorGetComponents(color);

接下来就是如何得到对应的R、G、B,及alpha的值了。
看到这里,有些童鞋可能会说,那还不简单吗:

r = components[0];
g = components[1];
b = components[2];
a = components[3];

这样不就可以了吗。。。。

能这么说的童鞋,说明是用过这个方法的,同时也说明这些童鞋对UIColor的研究不只是停留在UIColor *color = [UIColor whiteColor]这个层面。值得点赞~~

好了,接下来就要说问题所在了。
通过[UIColor whiteColor] 和 [UIColor colorWithRed: green: blue: alpha:]这两种方式生成的color,用components取得的R、G、B,及alpha的值是有差别的(具体的差别,请各位童鞋自行实践)。那么为什么会有差别呢?

其实这跟colorSpace这个概念有关。
colorSpace,即色值空间,UIColor的取值都是通过colorSpace来调配。

  • 通过[UIColor whiteColor]这种方式生成的color其实是通过[UIColor colorWithWhite: alpha:]这个方法对应的colorSpace来调配色值的;
  • 通过[UIColor colorWithRed: green: blue: alpha:]这种方式生成的color是通过RGB色值对应的colorSpace来调配色值的。

两种colorSpace不一样,从而也造就了CGColorGetComponents(color)得到的components值不一样。具体差异如下:

  • 通过[UIColor whiteColor]这种方式生成的color,最终得到的components中其实只有两个值,即components[0]和components[1]。其中components[0]代表R、G、B三色的值(三者值相同),而components[1]则代表alpha的值。
  • 通过[UIColor colorWithRed: green: blue: alpha:]这种方式生成的color,最终得到的components中才是真正的四个值,即R、G、B,及alpha对应的值。其中
    • components[0] == red;
    • components[1] == green;
    • components[2] == blue;
    • components[3] == alpha;

这时,可能有些童鞋又要问了,你怎么知道前一种方式生成的color得到的components只有2个值,而后一种方式生成的color的到的components就有4个值呢?
其实,这是可以通过一个方法取得的:

int numOfComponents = CGColorGetNumberOfComponents(color);

这样就可以得到准确的R、G、B,及alpha对应的值了。

其实,通过[UIColor colorWithWhite: alpha:]这个方法对应的colorSpace来调配的颜色并不多,就是下面这些了。有兴趣的童鞋可以点进UIColor的头文件看一下,Apple都是给大家标记出来。

@property(class, nonatomic, readonly) UIColor *blackColor;      // 0.0 white
@property(class, nonatomic, readonly) UIColor *darkGrayColor;   // 0.333 white
@property(class, nonatomic, readonly) UIColor *lightGrayColor;  // 0.667 white
@property(class, nonatomic, readonly) UIColor *whiteColor;      // 1.0 white
@property(class, nonatomic, readonly) UIColor *grayColor;       // 0.5 white
@property(class, nonatomic, readonly) UIColor *clearColor;      // 0.0 white, 0.0 alpha

最后,附上一个方法供参考:

- (void)getRGBComponents:(UIColor *)color {

    int numComponents = CGColorGetNumberOfComponents(color.CGColor);
    const CGFloat *components = CGColorGetComponents(color.CGColor);

    CGFloat r = 0.0;
    CGFloat g = 0.0;
    CGFloat b = 0.0;
    CGFloat a = 0.0;

    if (numComponents == 4) {
        r = components[0];
        g = components[1];
        b = components[2];
        a = components[3];

        NSLog(@"r == %f",r);
        NSLog(@"g == %f",g);
        NSLog(@"b == %f",b);
        NSLog(@"a == %f",a);
    } else if (numComponents == 2) {
        r = components[0];
        g = components[0];
        b = components[0];
        a = components[1];
 
        NSLog(@"r == %f",r);
        NSLog(@"g == %f",g);
        NSLog(@"b == %f",b);
        NSLog(@"a == %f",a);
    } else {
        NSLog(@"what is this???");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • iOS中的颜色有UIColor、CGColor、CIColor三种,下面对三种颜色分别进行说明: 一、常用的UIC...
    lfp901020阅读 6,731评论 0 7
  • 接下来我们看Base文件夹下的UIKIt文件夹的内容。 1.UIColor+YYAdd 这里看了这个类,里面有许多...
    充满活力的早晨阅读 2,322评论 0 1
  • UIColor,CGColor,CIColor三者的区别和联系 最近看了看CoreGraphics的东西,看到关于...
    VincentHK阅读 1,659评论 0 3
  • 这个夏天你作主 梅子时节,雨总会阴郁了心情, 花残绿肥,本也不必太过心伤, 只是烟雨水墨的婉约怎抵得泪雨涟涟, 风...
    不语不问阅读 192评论 0 3
  • 我和爸比妈咪一起玩了跳棋。其实并不是我们三个人在玩跳棋,而是两个人在玩,那就是我和妈咪妈咪,后来爸比才加入了。怎么...
    Amy627阅读 267评论 0 1