KMAdvertisementModelTransition.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // KMAdvertisementModelTansition.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/12/2.
  6. //
  7. import AppKit
  8. @objcMembers open class KMAdvertisementModelTransition: NSObject {
  9. //MARK: 模型转换
  10. //颜色模型转换
  11. // public static func transitionColor(color: KMAdvertisementModelItem.Color?, highlight:Bool) -> NSColor {
  12. // var result = NSColor.clear
  13. // guard let tempColor = color else { return result }
  14. //
  15. // if highlight {
  16. // let darkHex = tempColor.height?.dark?.hex
  17. // let darkAlpha = tempColor.height?.dark?.alpha
  18. // let universalHex = tempColor.height?.universal?.hex
  19. // let universalAlpha = tempColor.height?.universal?.alpha
  20. // let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
  21. // let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
  22. //
  23. // result = KMAdvertisementConfig.adapterDark(light: light , dark: dark) as! NSColor
  24. // } else {
  25. // let darkHex = tempColor.normal?.dark?.hex
  26. // let darkAlpha = tempColor.normal?.dark?.alpha
  27. // let universalHex = tempColor.normal?.universal?.hex
  28. // let universalAlpha = tempColor.normal?.universal?.alpha
  29. //
  30. // let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
  31. // let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
  32. // result = KMAdvertisementConfig.adapterDark(light: light, dark: dark) as! NSColor
  33. // }
  34. //
  35. // return result
  36. // }
  37. //图片转换
  38. public static func transitionImagePath(image: KMAdvertisementItemInfo.Image?, highlight:Bool) -> String {
  39. var result = ""
  40. guard let tempImage = image else { return result }
  41. if highlight {
  42. result = (KMAdvertisementConfig.isDarkModel() ? tempImage.hover_Dark : tempImage.hover_Light) ?? ""
  43. } else {
  44. result = (KMAdvertisementConfig.isDarkModel() ? tempImage.normal_Dark : tempImage.normal_Light) ?? ""
  45. }
  46. if result.count == 0 {
  47. let language = Language()
  48. language.en = image?.zh_TW
  49. language.zh_TW = image?.zh_TW
  50. language.zh_CN = image?.zh_CN
  51. result = KMAdvertisementModelTransition.transitionLanguage(langeuage: language)
  52. }
  53. return result
  54. }
  55. //多语转换
  56. @objc public static func transitionLanguage(langeuage: Language?) -> String {
  57. var result = ""
  58. // let languageConfig = KMAdvertisementManager.manager.configuration.language
  59. // if languageConfig != .unknow {
  60. // if languageConfig == .en {
  61. // result = langeuage?.en ?? ""
  62. // } else if languageConfig == .zhCN {
  63. // result = langeuage?.zh_CN ?? ""
  64. // } else if languageConfig == .zhTW {
  65. // result = langeuage?.zh_TW ?? ""
  66. // }
  67. // } else {
  68. let string = KMAdvertisementModelTransition.getLanguageType()
  69. if string == "zh-Hans-CN" {
  70. result = langeuage?.zh_CN ?? ""
  71. } else if string == "zh-Hans-TW" {
  72. result = langeuage?.zh_TW ?? ""
  73. } else {
  74. result = langeuage?.en ?? ""
  75. }
  76. // }
  77. return result
  78. }
  79. public static func getLanguageType() -> String {
  80. #if os(iOS)
  81. let def = UserDefaults.standard
  82. let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
  83. let chooseLanguage = allLanguages.first
  84. return chooseLanguage ?? "en"
  85. #elseif os(OSX)
  86. // let locale = NSLocale.current.languageCode
  87. // let chooseLanguage = Locale.preferredLanguages[0]
  88. let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
  89. let chooseLanguage: String = allLanguages.first ?? "en"
  90. return chooseLanguage
  91. #endif
  92. }
  93. }
  94. extension NSColor {
  95. convenience init(hex: String) {
  96. var hexString = hex
  97. if hex.starts(with: "#") {
  98. hexString = String(hex.dropFirst())
  99. }
  100. if let ui64 = UInt64(hexString, radix: 16) {
  101. self.init(hex: Int(ui64))
  102. } else {
  103. self.init(hex: 0) // <--- black
  104. }
  105. // or using ternaries as requested
  106. // let hexString = hex.starts(with: "#") ? String(hex.dropFirst()) : hex
  107. // let ui64 = UInt64(hexString, radix: 16)
  108. // self.init(hex: ui64 != nil ? Int(ui64!) : 0)
  109. }
  110. convenience init(hex: Int) {
  111. let components = (
  112. R: CGFloat((hex >> 16) & 0xff) / 255,
  113. G: CGFloat((hex >> 08) & 0xff) / 255,
  114. B: CGFloat((hex >> 00) & 0xff) / 255
  115. )
  116. self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  117. }
  118. // func toHex(alpha: Bool = false) -> String? {
  119. // guard let components = cgColor.components, components.count >= 3 else {
  120. // return nil
  121. // }
  122. //
  123. // let r = Float(components[0])
  124. // let g = Float(components[1])
  125. // let b = Float(components[2])
  126. // var a = Float(1.0)
  127. //
  128. // if components.count >= 4 {
  129. // a = Float(components[3])
  130. // }
  131. //
  132. // if alpha {
  133. // return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  134. // } else {
  135. // return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  136. // }
  137. // }
  138. }