ObjectC&&Swift 渐变色算法实现

-(NSArray *)getGradientColorWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor step:(NSInteger)step inverse:(BOOL)inverse {
    //1 get start color rgb
    CGFloat startR = 0.0, startG = 0.0, startB = 0.0;
    CGColorRef startColorRGB = [startColor CGColor];
    NSInteger startNumComponents = CGColorGetNumberOfComponents(startColorRGB);
    if (startNumComponents == 4)
    {
        const CGFloat *components = CGColorGetComponents(startColorRGB);
        startR = components[0];
        startG = components[1];
        startB = components[2];
    }
    //2 get end color rgb
    CGFloat endR = 0.0, endG = 0.0, endB = 0.0;
    CGColorRef endColorRGB = [endColor CGColor];
    NSInteger endNumComponents = CGColorGetNumberOfComponents(endColorRGB);
    if (endNumComponents == 4)
    {
        const CGFloat *components = CGColorGetComponents(endColorRGB);
        endR = components[0];
        endG = components[1];
        endB = components[2];
    }
    //3 calculate total threshold by step
    CGFloat stepR = 0.0, stepG = 0.0, stepB = 0.0;
    stepR = step == 1 ? 0 : (endR-startR) / (step - 1);
    stepG = step == 1 ? 0 : (endG-startG) / (step - 1);
    stepB = step == 1 ? 0 : (endB-startB) / (step - 1);
    //calculate uicolor by step
    CGFloat green = startG;
    CGFloat red   = startR;
    CGFloat blue  = startB;
    NSMutableArray *stepColorArray = [[NSMutableArray alloc] initWithCapacity:step];
    for (NSInteger i = 0; i < step - 1; i++) {
        red   = red + stepR;
        green = green + stepG;
        blue  = green + stepB;
        UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:0];
        [stepColorArray addObject:color];
    }
    return stepColorArray;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值