美文网首页iOS开发知识小集
将UIColor 转换为对应的RGB值(最安全的方法)

将UIColor 转换为对应的RGB值(最安全的方法)

作者: 流年划过颜夕 | 来源:发表于2019-04-17 18:38 被阅读15次

最近开发研究了颜色转换:将获得的UIColor 转换为对应的RGB值

看似简单,其实也有坑。
如果不假思索的从网上copy 对应的转换代码,可能就会潜伏很多问题,因为很多都不完善,过于老旧,大多都是一套判断逻辑,如下所示:

//将UIColor转换为RGB值
- (NSMutableArray *) changeUIColorToRGB:(UIColor *)color
{
    NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];
    NSString *RGBStr = nil;
    //获得RGB值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];
    //将RGB值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
    //获取红色值
    int r = [[RGBArr objectAtIndex:1] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",r];
    [RGBStrValueArr addObject:RGBStr];
    //获取绿色值
    int g = [[RGBArr objectAtIndex:2] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",g];
    [RGBStrValueArr addObject:RGBStr];
    //获取蓝色值
    int b = [[RGBArr objectAtIndex:3] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",b];
    [RGBStrValueArr addObject:RGBStr];
    //返回保存RGB值的数组
    return [RGBStrValueArr autorelease];
}

其实深入研究可以发现其实UIColor这个类也隐藏颜色解析的差异

官方中UIColor有以下

@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 *redColor;        // 1.0, 0.0, 0.0 RGB
@property(class, nonatomic, readonly) UIColor *greenColor;      // 0.0, 1.0, 0.0 RGB
@property(class, nonatomic, readonly) UIColor *blueColor;       // 0.0, 0.0, 1.0 RGB
@property(class, nonatomic, readonly) UIColor *cyanColor;       // 0.0, 1.0, 1.0 RGB
@property(class, nonatomic, readonly) UIColor *yellowColor;     // 1.0, 1.0, 0.0 RGB
@property(class, nonatomic, readonly) UIColor *magentaColor;    // 1.0, 0.0, 1.0 RGB
@property(class, nonatomic, readonly) UIColor *orangeColor;     // 1.0, 0.5, 0.0 RGB
@property(class, nonatomic, readonly) UIColor *purpleColor;     // 0.5, 0.0, 0.5 RGB
@property(class, nonatomic, readonly) UIColor *brownColor;      // 0.6, 0.4, 0.2 RGB
@property(class, nonatomic, readonly) UIColor *clearColor;      // 0.0 white, 0.0 alpha

可以看出blackColor,darkGrayColor,lightGrayColor,whiteColor ,grayColor ,clearColor等并不属于RGB范畴

以blackColor为例
打印对应的[UIColor blackColor] 相关信息,可得到它是基于UICachedDeviceWhiteColor类

    //获得色值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",[UIColor blackColor]];
    NSLog(@"%@ RGBValue %@",RGBValue,[[UIColor blackColor] class]);
结果如下:
UIExtendedGrayColorSpace 0 1 RGBValue UICachedDeviceWhiteColor

以redColor代表RGB
打印对应的[UIColor redColor] 相关信息,就可以明显看出它属于UICachedDeviceRGBColor类

    //获得色值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",[UIColor redColor]];
    NSLog(@"%@ RGBValue %@",RGBValue,[[UIColor redColor] class]);
结果如下:
UIExtendedSRGBColorSpace 1 0 0 1 RGBValue UICachedDeviceRGBColor

所以即使转换颜色也不能一概而论,正确的做法应该如下

+ (NSMutableArray *) changeUIColorToRGB:(UIColor *)color
{
    
    NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];
    NSString *RGBStr = @"";
    
    //获得色值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];
    //将色值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
    if (RGBArr > 0) {
        NSString *colorStatus = RGBArr.firstObject;
        if ([colorStatus isEqualToString:@"UIExtendedSRGBColorSpace"]&&RGBArr.count >= 5) {
            //获取红色值
            double r = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",r];
            [RGBStrValueArr addObject:RGBStr];
            //获取绿色值
            double g = [[RGBArr objectAtIndex:2] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",g];
            [RGBStrValueArr addObject:RGBStr];
            //获取蓝色值
            double b = [[RGBArr objectAtIndex:3] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",b];
            [RGBStrValueArr addObject:RGBStr];
            
            //获取透明度
            double a = [[RGBArr objectAtIndex:4] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",a];
            [RGBStrValueArr addObject:RGBStr];
            //返回保存RGB值的数组
        }
        else if([colorStatus  isEqualToString:@"UIExtendedGrayColorSpace"] &&RGBArr.count >= 3)
        {
            //获取红色值
            double r = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",r];
            [RGBStrValueArr addObject:RGBStr];
            //获取绿色值
            double g = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",g];
            [RGBStrValueArr addObject:RGBStr];
            //获取蓝色值
            double b = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",b];
            [RGBStrValueArr addObject:RGBStr];
            
            //获取透明度
            double a = [[RGBArr objectAtIndex:2] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",a];
            [RGBStrValueArr addObject:RGBStr];
        }
    }

    

    return RGBStrValueArr;
}

相关文章

网友评论

    本文标题:将UIColor 转换为对应的RGB值(最安全的方法)

    本文链接:https://www.haomeiwen.com/subject/qufowqtx.html