UIColor+Extension.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // UIColor+Extension.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2017/8/18.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. extension UIColor {
  11. /// EZSE: init method with RGB values from 0 to 255, instead of 0 to 1. With alpha(default:1)
  12. public convenience init(r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat = 1) {
  13. self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
  14. }
  15. convenience init(red: Int, green: Int, blue: Int) {
  16. assert(red >= 0 && red <= 255, "Invalid red component")
  17. assert(green >= 0 && green <= 255, "Invalid green component")
  18. assert(blue >= 0 && blue <= 255, "Invalid blue component")
  19. self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
  20. }
  21. static var random: UIColor {
  22. let maxValue: UInt32 = 24
  23. return UIColor(red: CGFloat(arc4random() % maxValue) / CGFloat(maxValue),
  24. green: CGFloat(arc4random() % maxValue) / CGFloat(maxValue) ,
  25. blue: CGFloat(arc4random() % maxValue) / CGFloat(maxValue) ,
  26. alpha: 1)
  27. }
  28. convenience init(netHex:Int) {
  29. self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff)
  30. }
  31. convenience init(hex string: String) {
  32. var hex = string.hasPrefix("#")
  33. ? String(string.dropFirst())
  34. : string
  35. guard hex.count == 3 || hex.count == 6
  36. else {
  37. self.init(white: 1.0, alpha: 0.0)
  38. return
  39. }
  40. if hex.count == 3 {
  41. for (index, char) in hex.enumerated() {
  42. hex.insert(char, at: hex.index(hex.startIndex, offsetBy: index * 2))
  43. }
  44. }
  45. guard let intCode = Int(hex, radix: 16) else {
  46. self.init(white: 1.0, alpha: 0.0)
  47. return
  48. }
  49. self.init(
  50. red: CGFloat((intCode >> 16) & 0xFF) / 255.0,
  51. green: CGFloat((intCode >> 8) & 0xFF) / 255.0,
  52. blue: CGFloat((intCode) & 0xFF) / 255.0, alpha: 1.0)
  53. }
  54. public func alpha(_ value: CGFloat) -> UIColor {
  55. return withAlphaComponent(value)
  56. }
  57. class func rgb(_ r: Int, _ g: Int, _ b: Int, _ alpha: CGFloat) -> UIColor {
  58. return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha / 1.0)
  59. }
  60. class func hexRGB(_ rgbValue: Int, _ alpha: CGFloat) -> UIColor {
  61. return UIColor.init(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16))/255.0, green: ((CGFloat)((rgbValue & 0xFF00) >> 8))/255.0, blue: ((CGFloat)(rgbValue & 0xFF))/255.0, alpha: alpha / 1.0)
  62. }
  63. /**
  64. * 16进制 转 RGBA
  65. */
  66. class func UIColorFromRGBA(rgb:Int, alpha:CGFloat) ->UIColor {
  67. return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
  68. green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
  69. blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
  70. alpha: alpha)
  71. }
  72. /**
  73. * 16进制 转 RGB
  74. */
  75. class func UIColorFromRGB(rgb:Int) ->UIColor {
  76. return UIColor(red: ((CGFloat)((rgb & 0xFF0000) >> 16)) / 255.0,
  77. green: ((CGFloat)((rgb & 0xFF00) >> 8)) / 255.0,
  78. blue: ((CGFloat)(rgb & 0xFF)) / 255.0,
  79. alpha: 1.0)
  80. }
  81. // MARK: - RGBA
  82. class func RGBA(r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor {
  83. return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
  84. }
  85. // MARK: - HexInt Covert UIColor
  86. class func hexInt(_ hexValue:Int) -> UIColor {
  87. return hexInt(hexValue, 1.0)
  88. }
  89. // MARK: - HexInt alpha Covert UIColor
  90. class func hextIntWithAlpha(_ hexValue:Int,_ alpha:CGFloat) -> UIColor {
  91. return hexInt(hexValue,alpha)
  92. }
  93. // MARK: - 私有方法实现
  94. fileprivate class func hexInt(_ hexValue:Int,_ alpha:CGFloat) -> UIColor {
  95. return UIColor(red: ((CGFloat)((hexValue & 0xFF0000) >> 16)) / 255.0,
  96. green: ((CGFloat)((hexValue & 0xFF00) >> 8)) / 255.0,
  97. blue: ((CGFloat)(hexValue & 0xFF)) / 255.0,
  98. alpha: alpha)
  99. }
  100. }