Get lighter and darker color variations for a given UIColor

前端 未结 11 2174
一生所求
一生所求 2021-01-30 02:32

How to get different lighter and darker variations of a given UIColor in Swift?

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 02:48

    The code example below demonstrate how you can get a lighter and darker shade of a given color, useful in applications having dynamic themes

    For Darker Color

    + (UIColor *)darkerColorForColor:(UIColor *)c
    {
    CGFloat r, g, b, a;
        if ([c getRed:&r green:&g blue:&b alpha:&a])
    return [UIColor colorWithRed:MAX(r - 0.2, 0.0)
                                   green:MAX(g - 0.2, 0.0)
                                    blue:MAX(b - 0.2, 0.0)
    return nil; 
    }
    
    

    For Lighter Color

    + (UIColor *)lighterColorForColor:(UIColor *)c
    {
    CGFloat r, g, b, a;
        if ([c getRed:&r green:&g blue:&b alpha:&a])
    return [UIColor colorWithRed:MIN(r + 0.2, 1.0)
                           green:MIN(g + 0.2, 1.0)
                            blue:MIN(b + 0.2, 1.0)
    alpha:a];
    return nil;
    }
    
    

提交回复
热议问题