//
//  UIColor+SSAdd.swift
//  htcm_swift
//
//  Created by soldoros on 2018/12/20.
//  Copyright © 2018年 soldoros. All rights reserved.
//

import Foundation
import UIKit

extension UIColor {
    
    class func hexStringToColor(hexString: String) -> UIColor{
        return UIColor.hexStringToColor(hexString: hexString, alpha: 1)
    }
    
    
    class func hexStringToColor(hexString: String, alpha:CGFloat) -> UIColor{
        
        var cString: String = hexString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
        
        if cString.count < 6 {
            return UIColor.black
        }
        if cString.hasPrefix("0X") {
            cString = cString.substring(from: cString.index(cString.startIndex, offsetBy: 2))
        }
        if cString.hasPrefix("#") {
            cString = cString.substring(from: cString.index(cString.startIndex, offsetBy: 1))
        }
        if cString.count != 6 {
            return UIColor.black
        }
        
        var range: NSRange = NSMakeRange(0, 2)
        let rString = (cString as NSString).substring(with: range)
        range.location = 2
        let gString = (cString as NSString).substring(with: range)
        range.location = 4
        let bString = (cString as NSString).substring(with: range)
        
        var r: UInt32 = 0x0
        var g: UInt32 = 0x0
        var b: UInt32 = 0x0
        Scanner.init(string: rString).scanHexInt32(&r)
        Scanner.init(string: gString).scanHexInt32(&g)
        Scanner.init(string: bString).scanHexInt32(&b)
        
        return UIColor(red: CGFloat(r)/255, green: CGFloat(g)/255, blue: CGFloat(b)/255, alpha: alpha)
    }
    
    
    
    class func colorWithRGB(_r:CGFloat , _g:CGFloat , _b:CGFloat) -> UIColor{
        return UIColor.colorWithRGB(_r: _r, _g: _g, _b: _b, alpha: 1)
    }
    
    class func colorWithRGB(_r:CGFloat , _g:CGFloat , _b:CGFloat, alpha:CGFloat) -> UIColor{
        return UIColor.init(red: _r/255, green: _g/255, blue: _b/255, alpha: alpha)
    }
    
    class func colorWithRGBINT(_r:Int , _g:Int , _b:Int) -> UIColor{
        return UIColor.init(red: CGFloat(_r)/255, green: CGFloat(_g)/255, blue: CGFloat(_b)/255, alpha: 1.0)
    }
    
}


