How to initialize UIColor from RGB values properly?

后端 未结 6 2329
春和景丽
春和景丽 2020-12-23 18:47

I am working on an iPhone application which uses various colors. When user selects the particular color button I set drawing color accordingly. I am getting the color for so

6条回答
  •  我在风中等你
    2020-12-23 19:44

    Pragmatic approach with Swift.

    extension UIColor {
    
       convenience init(rgbColorCodeRed red: Int, green: Int, blue: Int, alpha: CGFloat) {
    
         let redPart: CGFloat = CGFloat(red) / 255
         let greenPart: CGFloat = CGFloat(green) / 255
         let bluePart: CGFloat = CGFloat(blue) / 255
    
         self.init(red: redPart, green: greenPart, blue: bluePart, alpha: alpha)
    
       }
    }
    

提交回复
热议问题