6.UIColor

作者: 二斤寂寞 | 来源:发表于2020-06-28 18:00 被阅读0次

一个UIColor对象代表的颜色,有时不透明(alpha值)。
UIColor几种常见的用法:

宏定义:

// rgb颜色转换(16进制->10进制)
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

// 带有RGBA的颜色设置
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

// 获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)

分类:

#import <UIKit/UIKit.h>

@interface UIColor (Hex)

+ (UIColor *)colorWithHex:(long)hexColor;
+ (UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity;
+ (UIColor *)randomColor;
 //十六进制的颜色值转换为UIColor
+ (UIColor *) colorWithHexString: (NSString *)color;

/**
 *  渐变颜色
 *
 *  @param fromColor    开始颜色
 *  @param toColor      结束颜色
 *  @param height       渐变高度
 *
 *  @return 渐变颜色
 */
+ (UIColor*)gradientColorFromColor:(UIColor*)fromColor toColor:(UIColor*)toColor withHeight:(int)height;

//将UIColor 转换为对应的RGB值(最安全的方法)
+ (NSMutableArray *)changeUIColorToRGB:(UIColor *)color;
@end


#import "UIColor+Hex.h"

@implementation UIColor (Hex)

+ (UIColor*)colorWithHex:(long)hexColor;
{
    return [UIColor colorWithHex:hexColor alpha:1.];    
}

+ (UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity
{
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];  
}   

+ (UIColor *)randomColor {
    CGFloat r = arc4random() % 256 / 255.0;
    CGFloat g = arc4random() % 256 / 255.0;
    CGFloat b = arc4random() % 256 / 255.0;
    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}

#pragma mark - 颜色转换 iOS中十六进制的颜色转换为UIColor

+ (UIColor *)colorWithHexString: (NSString *)hexString
{
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    //hexString应该6到8个字符
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    //如果hexString 有@"0X"前缀
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    
    //如果hexString 有@"#""前缀
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    //RGB转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //R
    NSString *rString = [cString substringWithRange:range];
    
    //G
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //B
    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];
}

#pragma mark - 渐变色
+ (UIColor*)gradientColorFromColor:(UIColor*)fromColor toColor:(UIColor*)toColor withHeight:(int)height
{
    CGSize size = CGSizeMake(1, height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    
    NSArray* colors = [NSArray arrayWithObjects:(id)fromColor.CGColor, (id)toColor.CGColor, nil];
    CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();
    
    return [UIColor colorWithPatternImage:image];
}

#pragma mark -将UIColor 转换为对应的RGB值
+ (NSMutableArray *)changeUIColorToRGB:(UIColor *)color {
    
    NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];
    NSString *RGBStr = @"";
    
    //获得色值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];
    //将色值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
    if (RGBArr > 0) {
        NSString *colorStatus = RGBArr.firstObject;
        if ([colorStatus isEqualToString:@"UIExtendedSRGBColorSpace"]&&RGBArr.count >= 5) {
            //获取红色值
            double r = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",r];
            [RGBStrValueArr addObject:RGBStr];
            //获取绿色值
            double g = [[RGBArr objectAtIndex:2] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",g];
            [RGBStrValueArr addObject:RGBStr];
            //获取蓝色值
            double b = [[RGBArr objectAtIndex:3] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",b];
            [RGBStrValueArr addObject:RGBStr];
            
            //获取透明度
            double a = [[RGBArr objectAtIndex:4] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",a];
            [RGBStrValueArr addObject:RGBStr];
            //返回保存RGB值的数组
        }
        else if([colorStatus  isEqualToString:@"UIExtendedGrayColorSpace"] &&RGBArr.count >= 3)
        {
            //获取红色值
            double r = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",r];
            [RGBStrValueArr addObject:RGBStr];
            //获取绿色值
            double g = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",g];
            [RGBStrValueArr addObject:RGBStr];
            //获取蓝色值
            double b = [[RGBArr objectAtIndex:1] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",b];
            [RGBStrValueArr addObject:RGBStr];
            
            //获取透明度
            double a = [[RGBArr objectAtIndex:2] doubleValue] * 255;
            RGBStr = [NSString stringWithFormat:@"%ff",a];
            [RGBStrValueArr addObject:RGBStr];
        }
    }

    

    return RGBStrValueArr;
}
@end

系统提供color直接转RGB的方法。二了二了。。。。

 CGFloat red, green, blue, alpha;
 [color getRed:&red green:&green blue:&blue alpha:&alpha];

背景色转图片

Objective-c

// 将UIColor转换成图片
- (UIImage*)createImageWithColor:(UIColor*)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

Swift

extension UIColor {
    
    func asImage(_ size: CGSize) -> UIImage? {
        
        var resultImage: UIImage? = nil
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
        
        guard let context = UIGraphicsGetCurrentContext() else {
            
            return resultImage
        }
        
        context.setFillColor(self.cgColor)
        context.fill(rect)
        resultImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return resultImage
    }
}

相关文章

  • 6.UIColor

    一个UIColor对象代表的颜色,有时不透明(alpha值)。UIColor几种常见的用法: 宏定义: 分类: 系...

网友评论

      本文标题:6.UIColor

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