PresentationTheme.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*****************************************************************************
  2. * PresentationTheme.swift
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. extension Notification.Name {
  14. static let VLCThemeDidChangeNotification = Notification.Name("themeDidChangeNotfication")
  15. }
  16. @objcMembers public class ColorPalette: NSObject {
  17. public let isDark: Bool
  18. public let name: String
  19. public let statusBarStyle: UIStatusBarStyle
  20. public let navigationbarColor: UIColor
  21. public let navigationbarTextColor: UIColor
  22. public let background: UIColor
  23. public let cellBackgroundA: UIColor
  24. public let cellBackgroundB: UIColor
  25. public let cellDetailTextColor: UIColor
  26. public let cellTextColor: UIColor
  27. public let lightTextColor: UIColor
  28. public let sectionHeaderTextColor: UIColor
  29. public let sectionHeaderTintColor: UIColor
  30. public let settingsBackground: UIColor
  31. public let settingsCellBackground: UIColor
  32. public let settingsSeparatorColor: UIColor
  33. public let mediaCategorySeparatorColor: UIColor
  34. public let tabBarColor: UIColor
  35. public let orangeUI: UIColor
  36. public init(isDark: Bool,
  37. name: String,
  38. statusBarStyle: UIStatusBarStyle,
  39. navigationbarColor: UIColor,
  40. navigationbarTextColor: UIColor,
  41. background: UIColor,
  42. cellBackgroundA: UIColor,
  43. cellBackgroundB: UIColor,
  44. cellDetailTextColor: UIColor,
  45. cellTextColor: UIColor,
  46. lightTextColor: UIColor,
  47. sectionHeaderTextColor: UIColor,
  48. sectionHeaderTintColor: UIColor,
  49. settingsBackground: UIColor,
  50. settingsCellBackground: UIColor,
  51. settingsSeparatorColor: UIColor,
  52. mediaCategorySeparatorColor: UIColor,
  53. tabBarColor: UIColor,
  54. orangeUI: UIColor) {
  55. self.isDark = isDark
  56. self.name = name
  57. self.statusBarStyle = statusBarStyle
  58. self.navigationbarColor = navigationbarColor
  59. self.navigationbarTextColor = navigationbarTextColor
  60. self.background = background
  61. self.cellBackgroundA = cellBackgroundA
  62. self.cellBackgroundB = cellBackgroundB
  63. self.cellDetailTextColor = cellDetailTextColor
  64. self.cellTextColor = cellTextColor
  65. self.lightTextColor = lightTextColor
  66. self.sectionHeaderTextColor = sectionHeaderTextColor
  67. self.sectionHeaderTintColor = sectionHeaderTintColor
  68. self.settingsBackground = settingsBackground
  69. self.settingsCellBackground = settingsCellBackground
  70. self.settingsSeparatorColor = settingsSeparatorColor
  71. self.mediaCategorySeparatorColor = mediaCategorySeparatorColor
  72. self.tabBarColor = tabBarColor
  73. self.orangeUI = orangeUI
  74. }
  75. }
  76. @objcMembers public class PresentationTheme: NSObject {
  77. public static let brightTheme = PresentationTheme(colors: brightPalette)
  78. public static let darkTheme = PresentationTheme(colors: darkPalette)
  79. static var current: PresentationTheme = {
  80. let isDarkTheme = UserDefaults.standard.bool(forKey: kVLCSettingAppTheme)
  81. return isDarkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme
  82. }() {
  83. didSet {
  84. AppearanceManager.setupAppearance(theme: self.current)
  85. NotificationCenter.default.post(name: .VLCThemeDidChangeNotification, object: self)
  86. }
  87. }
  88. public init(colors: ColorPalette) {
  89. self.colors = colors
  90. }
  91. public let colors: ColorPalette
  92. }
  93. @objc public extension UIColor {
  94. public convenience init(_ rgbValue: UInt32, _ alpha: CGFloat = 1.0) {
  95. let r = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
  96. let g = CGFloat((rgbValue & 0xFF00) >> 8) / 255.0
  97. let b = CGFloat(rgbValue & 0xFF) / 255.0
  98. self.init(red: r, green: g, blue: b, alpha: 1.0)
  99. }
  100. private func toHex(alpha: Bool = false) -> String? {
  101. guard let components = cgColor.components, components.count >= 3 else {
  102. assertionFailure()
  103. return nil
  104. }
  105. let r = Float(components[0])
  106. let g = Float(components[1])
  107. let b = Float(components[2])
  108. var a = Float(1.0)
  109. if components.count == 4 {
  110. a = Float(components[3])
  111. }
  112. if alpha {
  113. return String(format: "#%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  114. } else {
  115. return String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  116. }
  117. }
  118. var toHex: String? {
  119. return toHex()
  120. }
  121. }
  122. let brightPalette = ColorPalette(isDark: false,
  123. name: "Default",
  124. statusBarStyle: .default,
  125. navigationbarColor: UIColor(0xFFFFFF),
  126. navigationbarTextColor: UIColor(0x000000),
  127. background: UIColor(0xF9F9F7),
  128. cellBackgroundA: UIColor(0xF9F9F7),
  129. cellBackgroundB: UIColor(0xE5E5E3),
  130. cellDetailTextColor: UIColor(0xA9A9A9),
  131. cellTextColor: UIColor(0x000000),
  132. lightTextColor: UIColor(0x888888),
  133. sectionHeaderTextColor: UIColor(0xF9F9F7),
  134. sectionHeaderTintColor: UIColor(0xE5EFE3),
  135. settingsBackground: UIColor(0xDCDCDC),
  136. settingsCellBackground: UIColor(0xF9F9F7),
  137. settingsSeparatorColor: UIColor(0xD3D3D3),
  138. mediaCategorySeparatorColor: UIColor(0xECF2F6),
  139. tabBarColor: UIColor(0xFFFFFF),
  140. orangeUI: UIColor(0xFF8800))
  141. let darkPalette = ColorPalette(isDark: true,
  142. name: "Dark",
  143. statusBarStyle: .lightContent,
  144. navigationbarColor: UIColor(0x292B36),
  145. navigationbarTextColor: UIColor(0xD3D3D3),
  146. background: UIColor(0x1B1E21),
  147. cellBackgroundA: UIColor(0x292B36),
  148. cellBackgroundB: UIColor(0x000000),
  149. cellDetailTextColor: UIColor(0xD3D3D3),
  150. cellTextColor: UIColor(0xFFFFFF),
  151. lightTextColor: UIColor(0xB8B8B8),
  152. sectionHeaderTextColor: UIColor(0x828282),
  153. sectionHeaderTintColor: UIColor(0x3C3C3C),
  154. settingsBackground: UIColor(0x292B36),
  155. settingsCellBackground: UIColor(0x3D3F40),
  156. settingsSeparatorColor: UIColor(0xA9A9A9),
  157. mediaCategorySeparatorColor: UIColor(0x25292C),
  158. tabBarColor: UIColor(0x292B36),
  159. orangeUI: UIColor(0xFF8800))