UIColor+Extensions.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2019 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import Foundation
  21. import UIKit
  22. // swiftlint:disable explicit_acl
  23. internal extension UIColor {
  24. static let incomingGray = UIColor(red: 230/255, green: 230/255, blue: 235/255, alpha: 1.0)
  25. static let outgoingGreen = UIColor(red: 69/255, green: 214/255, blue: 93/255, alpha: 1.0)
  26. static let inputBarGray = UIColor(red: 247/255, green: 247/255, blue: 247/255, alpha: 1.0)
  27. static let playButtonLightGray = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1.0)
  28. static let sendButtonBlue = UIColor(red: 15/255, green: 135/255, blue: 255/255, alpha: 1.0)
  29. convenience init(alpha: Int, red: Int, green: Int, blue: Int) {
  30. assert(red >= 0 && red <= 255, "Invalid red component")
  31. assert(green >= 0 && green <= 255, "Invalid green component")
  32. assert(blue >= 0 && blue <= 255, "Invalid blue component")
  33. self.init(red: CGFloat(red) / 255, green: CGFloat(green) / 255, blue: CGFloat(blue) / 255, alpha: CGFloat(alpha) / 255)
  34. }
  35. convenience init(netHex: Int) {
  36. var alpha = (netHex >> 24) & 0xFF
  37. if alpha == 0 {
  38. alpha = 255
  39. }
  40. self.init(alpha: alpha, red: (netHex >> 16) & 0xFF, green: (netHex >> 8) & 0xFF, blue: netHex & 0xFF)
  41. }
  42. // see: https://stackoverflow.com/a/33397427
  43. convenience init(hexString: String) {
  44. let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
  45. var int = UInt32()
  46. Scanner(string: hex).scanHexInt32(&int)
  47. let a, r, g, b: UInt32
  48. switch hex.count {
  49. case 3: // RGB (12-bit)
  50. (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
  51. case 6: // RGB (24-bit)
  52. (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
  53. case 8: // ARGB (32-bit)
  54. (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
  55. default:
  56. (a, r, g, b) = (255, 0, 0, 0)
  57. }
  58. self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
  59. }
  60. }