Check if UIColor is dark or bright?

前端 未结 14 2018
醉酒成梦
醉酒成梦 2020-11-29 17:20

I need to determine whether a selected UIColor (picked by the user) is dark or bright, so I can change the color of a line of text that sits on top of that color, for better

14条回答
  •  醉梦人生
    2020-11-29 17:47

    Swift3

    extension UIColor {
        var isLight: Bool {
            var white: CGFloat = 0
            getWhite(&white, alpha: nil)
            return white > 0.5
        }
    }
    
    // Usage
    if color.isLight {
        label.textColor = UIColor.black
    } else {
        label.textColor = UIColor.white
    }
    

提交回复
热议问题