//
//  UIImage+KBColor.m
//  keyBoard
//
//  颜色/渐变 生成图片工具
//

#import "UIImage+KBColor.h"

@implementation UIImage (KBColor)

+ (UIImage *)kb_imageWithColor:(UIColor *)color size:(CGSize)size {
    return [self kb_imageWithColor:color size:size cornerRadius:0];
}

+ (UIImage *)kb_imageWithColor:(UIColor *)color size:(CGSize)size cornerRadius:(CGFloat)cornerRadius {
    if (size.width <= 0 || size.height <= 0 || !color) { return nil; }
    CGRect rect = (CGRect){CGPointZero, size};
    UIGraphicsBeginImageContextWithOptions(size, NO, 0); // 0 => 使用设备 scale
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    if (cornerRadius > 0) {
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
        [path addClip];
    }
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    CGContextFillRect(ctx, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

+ (UIImage *)kb_gradientImageWithColors:(NSArray<UIColor *> *)colors
                                   size:(CGSize)size
                               direction:(KBGradientDirection)direction {
    return [self kb_gradientImageWithColors:colors locations:nil size:size direction:direction cornerRadius:0];
}

+ (UIImage *)kb_gradientImageWithColors:(NSArray<UIColor *> *)colors
                               locations:(NSArray<NSNumber *> * _Nullable)locations
                                     size:(CGSize)size
                                direction:(KBGradientDirection)direction
                              cornerRadius:(CGFloat)cornerRadius {
    if (colors.count == 0 || size.width <= 0 || size.height <= 0) { return nil; }
    // 构造渐变层
    CAGradientLayer *layer = [CAGradientLayer layer];
    layer.frame = (CGRect){CGPointZero, size};
    NSMutableArray *cgColors = [NSMutableArray arrayWithCapacity:colors.count];
    for (UIColor *c in colors) { [cgColors addObject:(id)c.CGColor]; }
    layer.colors = cgColors;
    if (locations.count == colors.count) { layer.locations = locations; }

    // 方向 -> 起止点
    switch (direction) {
        case KBGradientDirectionLeftToRight:
            layer.startPoint = CGPointMake(0, 0.5);
            layer.endPoint   = CGPointMake(1, 0.5);
            break;
        case KBGradientDirectionTopToBottom:
            layer.startPoint = CGPointMake(0.5, 0);
            layer.endPoint   = CGPointMake(0.5, 1);
            break;
        case KBGradientDirectionLeftTopToRightBottom:
            layer.startPoint = CGPointMake(0, 0);
            layer.endPoint   = CGPointMake(1, 1);
            break;
        case KBGradientDirectionLeftBottomToRightTop:
            layer.startPoint = CGPointMake(0, 1);
            layer.endPoint   = CGPointMake(1, 0);
            break;
    }

    if (cornerRadius > 0) {
        layer.cornerRadius = cornerRadius;
        layer.masksToBounds = YES;
    }

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [layer renderInContext:ctx];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

@end

