Objective-C颜色处理

一、设置颜色的透明度

@implementation UIColor (Alpha)
/*
 * 调整当前颜色的透明度,获取新的颜色对象
 */
- (UIColor *)colorWithAlpha:(CGFloat)alpha { 
    // 当前颜色 
    CGFloat a = 1; 
    CGFloat r = 1; 
    CGFloat g = 1; 
    CGFloat b = 1; 
    [self getRed: &r green: &g blue: &b alpha: &a]; 
    return [UIColor colorWithRed: r green: g blue: b alpha: alpha];
}
@end

二、获取当前颜色和另一个颜色对象的过渡色

@implementation UIColor (ProgressColor)
/*
 * 获取当前颜色,到另一颜色之间的过渡色
 */
- (UIColor *)progressColorWithToColor:(UIColor *)toColor progress:(CGFloat)progress { 
    // 开始颜色 
    CGFloat f_a = 1; 
    CGFloat f_r = 1; 
    CGFloat f_g = 1; 
    CGFloat f_b = 1; 
    [self getRed: &f_r green: &f_g blue: &f_b alpha: &f_a]; 
    // 将要显示的颜色 
    CGFloat t_a = 1; 
    CGFloat t_r = 1; 
    CGFloat t_g = 1; 
    CGFloat t_b = 1; 
    [toColor getRed: &t_r green: &t_g blue: &t_b alpha: &t_a];
    // 要显示的颜色 
    CGFloat a = f_a + (t_a - f_a) * progress; 
    CGFloat r = f_r + (t_r - f_r) * progress; 
    CGFloat g = f_g + (t_g - f_g) * progress; 
    CGFloat b = f_b + (t_b - f_b) * progress; 
    return [UIColor colorWithRed: r green: g blue: b alpha: alpha];
}
@end

三、把16进制颜色对象转换为UIColor对象

@implementation UIColor (StringToColor)
/*
 * 调整当前颜色的透明度,获取新的颜色对象
 * colorString : 字符串支持rgb 和 argb 格式 前缀可以使用‘#’和'0X'
 */
+ (UIColor *)getColorWithColorString:(NSString *)colorString { 
    // 获取当前颜色字符串对应的argb数据 
    NSDictionary *argb = [self getColorARGBWithColorString:colorString]; 
    int a = [argb[@"a"] intValue]; 
    int r = [argb[@"r"] intValue]; 
    int g = [argb[@"g"] intValue]; 
    int b = [argb[@"b"] intValue]; 
    return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:a / 255.0f];
}
// 颜色解析
+ (NSDictionary *)getColorARGBWithColorString:(NSString *)colorString {
    if ((colorString.length == 10 && [colorString.uppercaseString hasPrefix:@"0X"]) || (colorString.length == 9 && [colorString hasPrefix:@"#"])) { 
        // 1.当前是argb格式 OXFFFFFFFF 或者 #FFFFFFFF 
        if ([colorString hasPrefix:@"#"]) { 
            colorString = [colorString substringFromIndex:1]; 
        } else { 
            colorString = [colorString substringFromIndex:2]; 
        }
        NSString *alphaString = [colorString substringWithRange:NSMakeRange(0, 2)];
        //透明度 
        NSString *redString = [colorString substringWithRange:NSMakeRange(2, 2)]; 
        NSString *greenString = [colorString substringWithRange:NSMakeRange(4, 2)]; 
        NSString *blueString = [colorString substringWithRange:NSMakeRange(6, 2)]; 
        unsigned int a, r, g, b; 
        [[NSScanner scannerWithString:alphaString] scanHexInt:&a]; 
        [[NSScanner scannerWithString:redString] scanHexInt:&r]; 
        [[NSScanner scannerWithString:greenString] scanHexInt:&g]; 
        [[NSScanner scannerWithString:blueString] scanHexInt:&b]; 
        return @{@"a": @(a),@"r": @(r), @"g": @(g), @"b": @(b)}; 
    } else if ((colorString.length == 8 && [colorString.uppercaseString hasPrefix:@"0X"]) || (colorString.length == 7 && [colorString hasPrefix:@"#"])) { 
        // 1.当前是rgb格式 OXFFFFFF 或者 #FFFFFF 
        if ([colorString hasPrefix:@"#"]) { 
            colorString = [colorString substringFromIndex:1]; 
        } else { 
            colorString = [colorString substringFromIndex:2]; 
        } 
        NSString *redString = [colorString substringWithRange:NSMakeRange(0, 2)]; 
        NSString *greenString = [colorString substringWithRange:NSMakeRange(2, 2)]; 
        NSString *blueString = [colorString substringWithRange:NSMakeRange(4, 2)];
        unsigned int r, g, b; 
        [[NSScanner scannerWithString:redString] scanHexInt:&r]; 
        [[NSScanner scannerWithString:greenString] scanHexInt:&g]; 
        [[NSScanner scannerWithString:blueString] scanHexInt:&b];
        return @{@"a": @(255),@"r": @(r), @"g": @(g), @"b": @(b)}; 
    } else { 
        return @{@"a": @(255),@"r": @(255), @"g": @(255), @"b": @(255)}; 
    }
}
@end

更多分享:

Github:https://github.com/zhusiming/
GitBook:https://zhusiming.gitbooks.io/smbook/

    原文作者:朱思明
    原文地址: https://www.jianshu.com/p/b7e960cbd8c5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞