加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ObjectC&&Swift 渐变色算法实现

发布时间:2020-12-14 01:43:49 所属栏目:百科 来源:网络整理
导读:-(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 = [
-(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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读