Get Slightly Lighter and Darker Color from UIColor

前端 未结 19 2703
猫巷女王i
猫巷女王i 2020-12-07 07:08

I was looking to be able to turn any UIColor into a gradient. The way I am intending to do this is by using Core Graphics to draw a gradient. What I am trying to do is to ge

19条回答
  •  时光说笑
    2020-12-07 07:37

    Here is a UIColor category that also allows control over the amount of color change.

    - (UIColor *)lighterColorWithDelta:(CGFloat)delta
    {
        CGFloat r, g, b, a;
        if ([self getRed:&r green:&g blue:&b alpha:&a])
            return [UIColor colorWithRed:MIN(r + delta, 1.0)
                                   green:MIN(g + delta, 1.0)
                                    blue:MIN(b + delta, 1.0)
                                   alpha:a];
        return nil;
    }
    
    - (UIColor *)darkerColorWithDelta:(CGFloat)delta
    {
        CGFloat r, g, b, a;
        if ([self getRed:&r green:&g blue:&b alpha:&a])
            return [UIColor colorWithRed:MAX(r - delta, 0.0)
                                   green:MAX(g - delta, 0.0)
                                    blue:MAX(b - delta, 0.0)
                                   alpha:a];
        return nil;
    }
    

提交回复
热议问题