Get Slightly Lighter and Darker Color from UIColor

前端 未结 19 2620
猫巷女王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:39

    I'm not sure if you're looking for some sort of Objective-C answer, but based on how colors specified by RGBA work, I think you can simply scale the RGB values according to an arbitrary factor to get a "lighter" or "darker" shade. For example, you might have a blue:

    [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
    

    Want a darker blue? Multiply the RGB values by 0.9:

    [UIColor colorWithRed:0.0 green:0.0 blue:0.9 alpha:1.0];
    

    Voila. Or maybe you have an orange:

    [UIColor colorWithRed:1.0 green:0.4 blue:0.0 alpha:1.0];
    

    Choose another scale factor, say, 0.8:

    [UIColor colorWithRed:0.8 green:0.32 blue:0.0 alpha:1.0];
    

    Is that the sort of effect you're looking for?

提交回复
热议问题