Get Slightly Lighter and Darker Color from UIColor

前端 未结 19 2614
猫巷女王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条回答
  •  -上瘾入骨i
    2020-12-07 07:43

    Ideally, the functions should be encapsulated inside a UIColor extension called, UIColor+Brightness.swift, and have a configurable brightness - see example below:

    import UIKit
    
    extension UIColor {
    
      func lighterColorWithBrightnessFactor(brightnessFactor:CGFloat) -> UIColor {
        var r:CGFloat = 0, g:CGFloat = 0, b:CGFloat = 0, a:CGFloat = 0
        if self.getRed(&r, green:&g, blue:&b, alpha:&a) {
          return UIColor(red:min(r + brightnessFactor, 1.0),
            green:min(g + brightnessFactor, 1.0),
            blue:min(b + brightnessFactor, 1.0),
            alpha:a)
        }
        return UIColor()
      }
    
    }
    

提交回复
热议问题