完整代码
import Foundation
import UIKit
extension UIColor {
convenience init(r : CGFloat, g : CGFloat, b : CGFloat){
self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0);
}
//简化RGB颜色写法
class func RGBA(_ r : UInt, g : UInt, b : UInt, a : CGFloat) -> UIColor {
let redFloat = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
return UIColor(red: redFloat, green: green, blue: blue, alpha: a)
}
//随机色
class func randomColor() -> UIColor {
return UIColor(r: CGFloat(arc4random_uniform(256)), g: CGFloat(arc4random_uniform(256)), b: CGFloat(arc4random_uniform(256)));
}
//16进制颜色
class func colorWithHexString(_ hex: String, alpha : CGFloat = 1.0) -> UIColor {
var hex = hex
if hex.hasPrefix("#") {
hex =