美文网首页
字符串转颜色

字符串转颜色

作者: BeSt2wazi | 来源:发表于2017-07-13 17:21 被阅读0次
    @interface UIColor (BGHexColor)
    
    + (UIColor *)colorWithHex:(uint)hex alpha:(CGFloat)alpha;
    
    /**
     *  十六进制字符串转颜色
     */
    + (UIColor *)colorWithHexString:(NSString *)HexString;
    
    + (UIColor *)colorWithHexString:(NSString *)HexString alpha:(CGFloat)alpha;
    
    + (UIColor*) colorRGBonvertToHSB:(UIColor*)color withBrighnessDelta:(CGFloat)delta;
    
    + (UIColor*) colorRGBonvertToHSB:(UIColor*)color withAlphaDelta:(CGFloat)delta;
    
    + (UIColor*) colorWithHex:(NSInteger)hexValue;
    
    @end
    
    @implementation UIColor (BGHexColor)
    
    + (UIColor *)colorWithHex:(uint)hex alpha:(CGFloat)alpha
    {
        int red, green, blue;
        blue = hex & 0x0000FF;
        green = ((hex & 0x00FF00) >> 8);
        red = ((hex & 0xFF0000) >> 16);
        return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha];
    }
    
    //字符串转颜色
    + (UIColor *)colorWithHexString:(NSString *)HexString
    {
        NSString *cString = [[HexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        
        if ([cString length] < 6)
            return DEFAULT_VOID_COLOR;
        if ([cString hasPrefix:@"#"])
            cString = [cString substringFromIndex:1];
        if ([cString length] != 6)
            return DEFAULT_VOID_COLOR;
        
        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:1.0f];
    }
    
    + (UIColor *)colorWithHexString:(NSString *)HexString alpha:(CGFloat)alpha{
        NSString *cString = [[HexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
        
        if ([cString length] < 6)
            return DEFAULT_VOID_COLOR;
        if ([cString hasPrefix:@"#"])
            cString = [cString substringFromIndex:1];
        if ([cString length] != 6)
            return DEFAULT_VOID_COLOR;
        
        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];
    }
    
    + (UIColor*) colorRGBonvertToHSB:(UIColor*)color withBrighnessDelta:(CGFloat)delta {
        CGFloat hue = 0.0f;
        CGFloat saturation = 0.0f;
        CGFloat brightness = 0.0f;
        CGFloat alpha = 0.0f;
        
        [color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
        
        brightness += delta;
        
        return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
    }
    
    + (UIColor*) colorRGBonvertToHSB:(UIColor*)color withAlphaDelta:(CGFloat)delta {
        CGFloat hue = 0.0f;
        CGFloat saturation = 0.0f;
        CGFloat brightness = 0.0f;
        CGFloat alpha = 0.0f;
        
        [color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
        
        alpha += delta;
        
        return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
    }
    + (UIColor*) colorWithHex:(NSInteger)hexValue
    {
        return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                               green:((float)((hexValue & 0xFF00) >> 8))/255.0
                                blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:字符串转颜色

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