十六進(jìn)制字符串&RGB&UIColor轉(zhuǎn)換~~OC

文章僅供參考,如有更好的編寫方法,歡迎指正?。?!
XFJColor.h文件

@interface XFJColor : NSObject
/**
 * RGB轉(zhuǎn)UIColor
 * R : 紅
 * G : 綠
 * B : 藍(lán)
 * Return : UIColor
 */
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b;

/**
 * RGB轉(zhuǎn)UIColor
 * R : 紅
 * G : 綠
 * B : 藍(lán)
 * A : 透明度
 * Return : UIColor
 */
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b A:(float)a;

/**
 * 十六進(jìn)制字符串轉(zhuǎn)換UIColor
 * Color : 十六進(jìn)制字符串
 * Return : UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)color;

/**
 * 十六進(jìn)制字符串轉(zhuǎn)換UIColor
 * Color : 十六進(jìn)制字符串
 * A : 透明度
 * Return : UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)color A:(float)a;

/**
 * UIColor轉(zhuǎn)十六進(jìn)制字符串
 * Color : UIColor
 * Perfix : 返回十六進(jìn)制字符串格式("#","0x")
 * Return : 十六進(jìn)制字符串
 */
+ (NSString *)hexadecimalFromUIColor:(UIColor*)color perfix:(NSString *)perfix;

@end

XFJColor.m文件

@implementation XFJColor

/**
 * RGB轉(zhuǎn)UIColor
 * R : 紅
 * G : 綠
 * B : 藍(lán)
 * Return : UIColor
 */
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b
{
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}

/**
 * RGB轉(zhuǎn)UIColor
 * R : 紅
 * G : 綠
 * B : 藍(lán)
 * A : 透明度
 * Return : UIColor
 */
+ (UIColor *)colorWithR:(float)r G:(float)g B:(float)b A:(float)a
{
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:a];
}

/**
 * 十六進(jìn)制字符串轉(zhuǎn)換UIColor
 * Color : 十六進(jìn)制字符串
 * Return : UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)color
{
    // 去空格換行
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // 判斷前綴
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // 從六位數(shù)值中找到RGB對應(yīng)的位數(shù)并轉(zhuǎn)換
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [self colorWithR:(float) r G:(float) g B:(float) b];
}

/**
 * 十六進(jìn)制字符串轉(zhuǎn)換UIColor
 * Color : 十六進(jìn)制字符串
 * A : 透明度
 * Return : UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)color A:(float)a
{
    // 去空格換行
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // 判斷前綴
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // 從六位數(shù)值中找到RGB對應(yīng)的位數(shù)并轉(zhuǎn)換
    NSRange range;
    range.location = 0;
    range.length = 2;
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [self colorWithR:(float) r G:(float) g B:(float) b A:a];
}

/**
 * UIColor轉(zhuǎn)十六進(jìn)制字符串
 * Color : UIColor
 * Perfix : 返回十六進(jìn)制字符串格式("#","0x")
 * Return : 十六進(jìn)制字符串
 */
+ (NSString *)hexadecimalFromUIColor:(UIColor*)color perfix:(NSString *)perfix
{
    if(CGColorGetNumberOfComponents(color.CGColor) < 4)
    {
        const CGFloat *components =CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0]green:components[0]blue:components[0]alpha:components[1]];
    }
    
    if(CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) !=kCGColorSpaceModelRGB)
    {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    
    NSString *r,*g,*b;
    (int)((CGColorGetComponents(color.CGColor))[0]*255.0) == 0?(r =[NSString stringWithFormat:@"0%x",(int)((CGColorGetComponents(color.CGColor))[0]*255.0)]):(r= [NSString stringWithFormat:@"%x",(int)((CGColorGetComponents(color.CGColor))[0]*255.0)]);
    (int)((CGColorGetComponents(color.CGColor))[1]*255.0)== 0?(g = [NSString stringWithFormat:@"0%x",(int)((CGColorGetComponents(color.CGColor))[1]*255.0)]):(g= [NSString stringWithFormat:@"%x",(int)((CGColorGetComponents(color.CGColor))[1]*255.0)]);
    (int)((CGColorGetComponents(color.CGColor))[2]*255.0)== 0?(b = [NSString stringWithFormat:@"0%x",(int)((CGColorGetComponents(color.CGColor))[2]*255.0)]):(b= [NSString stringWithFormat:@"%x",(int)((CGColorGetComponents(color.CGColor))[2]*255.0)]);
    return [NSString stringWithFormat:@"%@%@%@%@", perfix,r,g,b];
}

@end
最后編輯于
?著作權(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ù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

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

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