Swift_VIP专场汇总iOS开发首页投稿(暂停使用,暂停投稿)

iOS UIColor扩展,十六进制颜色转RGB颜色的三种方法

2017-03-11  本文已影响221人  高扬先生

本文以十六进制颜色转RGB颜色为主,同时做了原生的 UIColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) 的扩展,以方便使用。具体如下:

1、通过重写init方法

public extension UIColor {
    
    // Init color without divide 255.0
    //
    // - Parameters:
    //   - r: (0 ~ 255) red
    //   - g: (0 ~ 255) green
    //   - b: (0 ~ 255) blue
    //   - a: (0 ~ 1) alpha
    convenience init(r: Int, g: Int, b: Int, a: CGFloat) {
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: a)
    }
    
    // Init color with hex code
    //
    // - Parameter hex: hex code (eg. 0x00eeee)
    convenience init(hex: Int) {
        self.init(r: (hex & 0xff0000) >> 16, g: (hex & 0xff00) >> 8, b: (hex & 0xff), a: 1)
    }
}

2、通过新建公用方法

func kColorFromHex(rgbValue: Int) -> (UIColor) {
    
    return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0,
                   green: ((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0,
                   blue: ((CGFloat)(rgbValue & 0xFF)) / 255.0,
                   alpha: 1.0)
}

func kColorFromRGB(r: Int, g:Int, b: Int, alpha: CGFloat) -> (UIColor) {
    
    return UIColor(red: (CGFloat(r)) / 255.0,
                   green: (CGFloat(r)) / 255.0,
                   blue: (CGFloat(r)) / 255.0,
                   alpha: alpha)
}

3、通过扩展UIColor的方法

extension UIColor {
    
    /**
     Make color with divide 255.0
     - Parameters:
        - r: (0 ~ 255) red
        - g: (0 ~ 255) green
        - b: (0 ~ 255) blue
        - a: (0 ~ 1) alpha
     - returns: RGB
     */
    static func colorWithRGB(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> UIColor {
        return UIColor(red: red/255, green: green/255, blue: blue/255, alpha: alpha)
    }
    
    /**
     Make color with hex string
     - parameter hex: 16进制字符串(eg. #0x00eeee or #0X00eeee or 0x00eeee or 0X00eeee or 00eeee)
     - returns: RGB
     */
    static func colorWithHexString (hex: String, alpha: CGFloat) -> UIColor {
        
        var cString: NSString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString
        
        if (cString.hasPrefix("#")) {
            cString = cString.substring(from: 1) as NSString
        }
        if (cString.hasPrefix("0X") || cString.hasPrefix("0x")) {
            cString = cString.substring(from: 2) as NSString
        }
        
        if (cString.length != 6) {
            return UIColor.gray
        }
        
        let rString = cString.substring(with: NSMakeRange(0, 2))
        let gString = cString.substring(with: NSMakeRange(2, 2))
        let bString = cString.substring(with: NSMakeRange(4, 2))
        
        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: alpha)
    }
}

上一篇下一篇

猜你喜欢

热点阅读