// // ADHcolor.swift // ADHTuanCan // // Created by 敖德亨 on 2023/9/27. // import Foundation import UIKit extension UIColor { class func RGBColor(red : CGFloat,green :CGFloat ,blue : CGFloat,alpha : CGFloat) ->UIColor{ return UIColor(red: red, green: green, blue: blue, alpha: alpha) } class func color(hex:String)->UIColor{ var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) { let index = cString.index(cString.startIndex, offsetBy:1) cString = cString.substring(from: index) } if (cString.count != 6) { return UIColor.red } let rIndex = cString.index(cString.startIndex, offsetBy: 2) let rString = cString.substring(to: rIndex) let otherString = cString.substring(from: rIndex) let gIndex = otherString.index(otherString.startIndex, offsetBy: 2) let gString = otherString.substring(to: gIndex) let bIndex = cString.index(cString.endIndex, offsetBy: -2) let bString = cString.substring(from: bIndex) var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0; Scanner(string: rString).scanHexInt32(&r) Scanner(string: gString).scanHexInt32(&g) Scanner(string: bString).scanHexInt32(&b) return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1)) } class func color(hex:String,alpha:CGFloat)->UIColor{ var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) { let index = cString.index(cString.startIndex, offsetBy:1) cString = cString.substring(from: index) } if (cString.count != 6) { return UIColor.red } let rIndex = cString.index(cString.startIndex, offsetBy: 2) let rString = cString.substring(to: rIndex) let otherString = cString.substring(from: rIndex) let gIndex = otherString.index(otherString.startIndex, offsetBy: 2) let gString = otherString.substring(to: gIndex) let bIndex = cString.index(cString.endIndex, offsetBy: -2) let bString = cString.substring(from: bIndex) var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0; Scanner(string: rString).scanHexInt32(&r) Scanner(string: gString).scanHexInt32(&g) Scanner(string: bString).scanHexInt32(&b) return UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(alpha)) } }