Blame view

Twear/Tools/UIColor+Extension.swift 3.56 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  //
  //  UIColor+Extension.swift
  //  Floral
  //
  //  Created by ALin on 16/4/26.
  //  Copyright © 2016 ALin. All rights reserved.
  //
  
  import UIKit
  
  extension CALayer {
      @IBInspectable
      var borderUIColor: UIColor {
          get {
              return UIColor(cgColor: self.borderColor!)
          }
          set {
              self.borderColor = newValue.cgColor
          }
      }
  }
  
  
  extension UIColor
  {
      /**
       根据RGB生成颜色
       
       - parameter r: red
       - parameter g: green
       - parameter b: blue
       
       - returns: 颜色
       */
  //    convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
  //        self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
  //    }
      
      /**
       根据RGB生成颜色
       
       - parameter r: red
       - parameter g: green
       - parameter b: blue
       - parameter alpha: 透明度
       
       - returns: 颜色
       */
  //    convenience init(r: CGFloat, g: CGFloat, b: CGFloat, alpha: CGFloat) {
  //        self.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: alpha)
  //    }
      
      /**
       生成灰色
       
       - parameter gray: 灰色
       
       - returns: 颜色
       */
      convenience init(gray: CGFloat) {
          self.init(red: gray/255.0, green: gray/255.0, blue: gray/255.0, alpha: 1)
      }
      
      class func rgbaColorFromHex(_ rgb:Int, alpha: CGFloat) ->UIColor {
          
          return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
                         green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
                         blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
                         alpha: alpha)
      }
      
      class func rgbColorFromHex(_ rgb:Int) ->UIColor {
          
          return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
                         green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
                         blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
                         alpha: 1.0)
      }
  
      func trans2Image() -> UIImage {
          let rect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
          UIGraphicsBeginImageContext(rect.size)
          let context = UIGraphicsGetCurrentContext()
          context?.setFillColor(self.cgColor)
          context?.fill(rect)
          let theImage = UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()
          return theImage ?? UIImage()
      }
      
      
  //    @objc func hexStringToColor(hexString: String) -> UIColor{
  //        var cString: String = hexString.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
  //        if cString.characters.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.characters.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.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: CGFloat(1))
  //        
  //    }
      
      
  }