美文网首页
iOS 常用RGB十六进制颜色转换方法

iOS 常用RGB十六进制颜色转换方法

作者: Levi段玉磊 | 来源:发表于2018-12-26 16:14 被阅读0次

iOS 常用RGB十六进制颜色转换方法

需求

正常美术出图都是给你十六进制编码,然后让我们在Xcode中,对颜色进行赋值,由于iOS的UIColor并未提供方法直接使用十六进制编码对设置颜色的场景进行赋值,所以我们需要造个小轮子。

实现方式

通过分类的方法实现:

UIColor+Addition.h

@interface UIColor (Addition)

//设置RGB颜色
+ (UIColor *)red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha;
//将颜色转换成RGB
+ (NSArray *)convertColorToRGB:(UIColor *)color;
//设置十六进制颜色
+ (UIColor *)colorWithHex:(NSInteger)hex;
+ (UIColor *)colorWithHexString:(NSString *)hexString;
+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha

@end

UIColor+Addition.m

#import "UIColor+Addition.h"

@implementation UIColor (Addition)

+ (UIColor *)red:(NSInteger)red green:(NSInteger)green blue:(NSInteger)blue alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
}

+ (NSArray *)convertColorToRGB:(UIColor *)color {
    NSInteger numComponents = CGColorGetNumberOfComponents(color.CGColor);
    NSArray *array = nil;
    if (numComponents == 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        array = @[@((int)(components[0] * 255)),
                  @((int)(components[1] * 255)),
                  @((int)(components[2] * 255))];
    }
    return array;
}

+ (UIColor *)colorWithHex:(NSInteger)hex {
    return [UIColor colorWithRed:((float)((hex & 0xff0000) >> 16))/255.0 green:((float)((hex & 0x00ff00) >> 8))/255.0 blue:((float)(hex & 0x0000ff))/255.0 alpha:1.0];
}

+ (UIColor *)colorWithHexString:(NSString *)hexString
{
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
            case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
            case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
            case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
            case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            blue=0;
            green=0;
            red=0;
            alpha=0;
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger) length
{
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}

+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha
{
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:
                          [NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([cString length] < 6) {
        return [UIColor whiteColor];
    }
    
    if ([cString hasPrefix:@"#"]) {
        cString = [cString substringFromIndex:1];
    }
    
    if ([cString length] != 6) {
        return [UIColor whiteColor];
    }
    
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r/255.0f)
                           green:((float) g/255.0f)
                            blue:((float) b/255.0f)
                           alpha:alpha];
}

思考与总结:

需要记住的是:

纯黑是 #000000
纯白是 #FFFFFF

相关文章

  • 取色值相关宏

    iOS中,常用的获取RGB颜色值和十六进制颜色值转换方法的宏定义。#define RGB(r,g,b) ...

  • iOS 常用RGB十六进制颜色转换方法

    iOS 常用RGB十六进制颜色转换方法 需求 正常美术出图都是给你十六进制编码,然后让我们在Xcode中,对颜色进...

  • 颜色RGB与十六进制的转换

    RGB值转换为十六进制颜色值 方法一: 方法二: 十六进制颜色值转换为RGB值 函数substr(I,length...

  • UIColor - 取出 RGBA 四个值

    iOS中十六进制的颜色转换UIColor(RGB ) 1、https://www.jianshu.com/p/96...

  • iOS-简单方便的十六进制颜色转换

    iOS原生的RGB颜色方法真是令人头痛,哎呀、妈呀,脑瓜疼! 今天寡人分享一个非常非常方便的十六进制颜色转换的封装...

  • JavaScript中颜色操作方法集合

    随机生成十六进制颜色 RGB 转十六进制 将 3 位颜色扩展为 6 位颜色 将 rgb()颜色字符串转换为具有每种...

  • JS 颜色RGB与16进制转换核心

    JavaScript颜色转换的核心就是进制间的转换。RGB格式其实就是十进制表示法,所以,十六进制颜色与RGB颜色...

  • CSS颜色

    颜色 颜色值的常用表示方式 十六进制色 RGB 颜色 RGBA 颜色 RGBA 颜色值是 RGB 颜色值的扩展,带...

  • 十六进制颜色转RGB颜色

    由于在iOS开发中颜色常用RGB形式,怎么把十六进制的颜色转成RGB形式就成了一个重要的问题,因此写了一个分类以便...

  • CSS(层叠样式表)之外观属性

    color文本颜色 预定颜色值十六进制,常用****RGB代码 color:rgb(0,0,0);透明效果:col...

网友评论

      本文标题:iOS 常用RGB十六进制颜色转换方法

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