HexColorTransform.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // HexColorTransform.swift
  3. // ObjectMapper
  4. //
  5. // Created by Vitaliy Kuzmenko on 10/10/16.
  6. //
  7. // Copyright (c) 2014-2018 Tristan Himmelman
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(iOS) || os(tvOS) || os(watchOS)
  27. import UIKit
  28. #elseif os(macOS)
  29. import Cocoa
  30. #endif
  31. #if os(iOS) || os(tvOS) || os(watchOS) || os(macOS)
  32. open class HexColorTransform: TransformType {
  33. #if os(iOS) || os(tvOS) || os(watchOS)
  34. public typealias Object = UIColor
  35. #else
  36. public typealias Object = NSColor
  37. #endif
  38. public typealias JSON = String
  39. var prefix: Bool = false
  40. var alpha: Bool = false
  41. public init(prefixToJSON: Bool = false, alphaToJSON: Bool = false) {
  42. alpha = alphaToJSON
  43. prefix = prefixToJSON
  44. }
  45. open func transformFromJSON(_ value: Any?) -> Object? {
  46. if let rgba = value as? String {
  47. if rgba.hasPrefix("#") {
  48. let index = rgba.index(rgba.startIndex, offsetBy: 1)
  49. let hex = String(rgba[index...])
  50. return getColor(hex: hex)
  51. } else {
  52. return getColor(hex: rgba)
  53. }
  54. }
  55. return nil
  56. }
  57. open func transformToJSON(_ value: Object?) -> JSON? {
  58. if let value = value {
  59. return hexString(color: value)
  60. }
  61. return nil
  62. }
  63. fileprivate func hexString(color: Object) -> String {
  64. let comps = color.cgColor.components!
  65. let compsCount = color.cgColor.numberOfComponents
  66. let r: Int
  67. let g: Int
  68. var b: Int
  69. let a = Int(comps[compsCount - 1] * 255)
  70. if compsCount == 4 { // RGBA
  71. r = Int(comps[0] * 255)
  72. g = Int(comps[1] * 255)
  73. b = Int(comps[2] * 255)
  74. } else { // Grayscale
  75. r = Int(comps[0] * 255)
  76. g = Int(comps[0] * 255)
  77. b = Int(comps[0] * 255)
  78. }
  79. var hexString: String = ""
  80. if prefix {
  81. hexString = "#"
  82. }
  83. hexString += String(format: "%02X%02X%02X", r, g, b)
  84. if alpha {
  85. hexString += String(format: "%02X", a)
  86. }
  87. return hexString
  88. }
  89. fileprivate func getColor(hex: String) -> Object? {
  90. var red: CGFloat = 0.0
  91. var green: CGFloat = 0.0
  92. var blue: CGFloat = 0.0
  93. var alpha: CGFloat = 1.0
  94. let scanner = Scanner(string: hex)
  95. var hexValue: CUnsignedLongLong = 0
  96. if scanner.scanHexInt64(&hexValue) {
  97. switch (hex.count) {
  98. case 3:
  99. red = CGFloat((hexValue & 0xF00) >> 8) / 15.0
  100. green = CGFloat((hexValue & 0x0F0) >> 4) / 15.0
  101. blue = CGFloat(hexValue & 0x00F) / 15.0
  102. case 4:
  103. red = CGFloat((hexValue & 0xF000) >> 12) / 15.0
  104. green = CGFloat((hexValue & 0x0F00) >> 8) / 15.0
  105. blue = CGFloat((hexValue & 0x00F0) >> 4) / 15.0
  106. alpha = CGFloat(hexValue & 0x000F) / 15.0
  107. case 6:
  108. red = CGFloat((hexValue & 0xFF0000) >> 16) / 255.0
  109. green = CGFloat((hexValue & 0x00FF00) >> 8) / 255.0
  110. blue = CGFloat(hexValue & 0x0000FF) / 255.0
  111. case 8:
  112. red = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
  113. green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
  114. blue = CGFloat((hexValue & 0x0000FF00) >> 8) / 255.0
  115. alpha = CGFloat(hexValue & 0x000000FF) / 255.0
  116. default:
  117. // Invalid RGB string, number of characters after '#' should be either 3, 4, 6 or 8
  118. return nil
  119. }
  120. } else {
  121. // "Scan hex error
  122. return nil
  123. }
  124. #if os(iOS) || os(tvOS) || os(watchOS)
  125. return UIColor(red: red, green: green, blue: blue, alpha: alpha)
  126. #else
  127. return NSColor(red: red, green: green, blue: blue, alpha: alpha)
  128. #endif
  129. }
  130. }
  131. #endif