Date
Dec. 26th, 2024
 
2024年 11月 26日

Post: Cocoa/Cocoa Touch: Hex Color

Cocoa/Cocoa Touch: Hex Color

Published 12:09 Sep 12, 2014.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

将 Hex 颜色 (例如 #ffffff) 转换为 UIColor/NSColor, 以 UIColor 举例:

我们通过分类方式实现:

@implementation UIColor (MXColours)

Hex 字符串转 Color 实例:

#pragma mark - Color from Hex
+ (instancetype)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    hexString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner scanHexInt:&rgbValue];

    return [[self class] colorWithR:((rgbValue & 0xFF0000) >> 16) G:((rgbValue & 0xFF00) >> 8) B:(rgbValue & 0xFF) A:1.0];
}

Color 实例转 Hex 字符串:

#pragma mark - Hex from Color
- (NSString *)hexString {
    NSArray *colorArray = [self rgbaArray];
    int r = [colorArray[0] floatValue] * 255;
    int g = [colorArray[1] floatValue] * 255;
    int b = [colorArray[2] floatValue] * 255;
    NSString *red = [NSString stringWithFormat:@"%02x", r];
    NSString *green = [NSString stringWithFormat:@"%02x", g];
    NSString *blue = [NSString stringWithFormat:@"%02x", b];

    return [NSString stringWithFormat:@"#%@%@%@", red, green, blue];
}

上面代码用到的自定义方法:

#pragma mark - Private
#pragma mark - RGBA Helper method
+ (instancetype)colorWithR:(CGFloat)red G:(CGFloat)green B:(CGFloat)blue A:(CGFloat)alpha {
    return [[self class] colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha];
}

#pragma mark - RGBA from Color
- (NSArray <NSNumber *>*)rgbaArray {
    CGFloat r=0,g=0,b=0,a=0;

    if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
        [self getRed:&r green:&g blue:&b alpha:&a];
    }
    else {
        const CGFloat *components = CGColorGetComponents(self.CGColor);
        r = components[0];
        g = components[1];
        b = components[2];
        a = components[3];
    }

    return @[@(r),
             @(g),
             @(b),
             @(a)];
}
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询