Objective-C顏色處理

一、設(shè)置顏色的透明度

@implementation UIColor (Alpha)
/*
 * 調(diào)整當(dāng)前顏色的透明度,獲取新的顏色對象
 */
- (UIColor *)colorWithAlpha:(CGFloat)alpha { 
    // 當(dāng)前顏色 
    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

二、獲取當(dāng)前顏色和另一個(gè)顏色對象的過渡色

@implementation UIColor (ProgressColor)
/*
 * 獲取當(dāng)前顏色,到另一顏色之間的過渡色
 */
- (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進(jìn)制顏色對象轉(zhuǎn)換為UIColor對象

@implementation UIColor (StringToColor)
/*
 * 調(diào)整當(dāng)前顏色的透明度,獲取新的顏色對象
 * colorString : 字符串支持rgb 和 argb 格式 前綴可以使用‘#’和'0X'
 */
+ (UIColor *)getColorWithColorString:(NSString *)colorString { 
    // 獲取當(dāng)前顏色字符串對應(yīng)的argb數(shù)據(jù) 
    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.當(dāng)前是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.當(dāng)前是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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容