PresentationTheme.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 class ColorPalette: NSObject {
  17. let isDark: Bool
  18. let name: String
  19. let statusBarStyle: UIStatusBarStyle
  20. let navigationbarColor: UIColor
  21. let navigationbarTextColor: UIColor
  22. let background: UIColor
  23. let cellBackgroundA: UIColor
  24. let cellBackgroundB: UIColor
  25. let cellDetailTextColor: UIColor
  26. let cellTextColor: UIColor
  27. let lightTextColor: UIColor
  28. let sectionHeaderTextColor: UIColor
  29. let sectionHeaderTintColor: UIColor
  30. let settingsBackground: UIColor
  31. let settingsCellBackground: UIColor
  32. let separatorColor: UIColor
  33. let mediaCategorySeparatorColor: UIColor
  34. let tabBarColor: UIColor
  35. let orangeUI: UIColor
  36. 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. separatorColor: 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.separatorColor = separatorColor
  71. self.mediaCategorySeparatorColor = mediaCategorySeparatorColor
  72. self.tabBarColor = tabBarColor
  73. self.orangeUI = orangeUI
  74. }
  75. }
  76. @objcMembers class PresentationTheme: NSObject {
  77. static let brightTheme = PresentationTheme(colors: brightPalette)
  78. 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. init(colors: ColorPalette) {
  89. self.colors = colors
  90. super.init()
  91. }
  92. let colors: ColorPalette
  93. }
  94. @objc extension UIColor {
  95. convenience init(_ rgbValue: UInt32, _ alpha: CGFloat = 1.0) {
  96. let r = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
  97. let g = CGFloat((rgbValue & 0xFF00) >> 8) / 255.0
  98. let b = CGFloat(rgbValue & 0xFF) / 255.0
  99. self.init(red: r, green: g, blue: b, alpha: 1.0)
  100. }
  101. private func toHex(alpha: Bool = false) -> String? {
  102. guard let components = cgColor.components, components.count >= 3 else {
  103. assertionFailure()
  104. return nil
  105. }
  106. let r = Float(components[0])
  107. let g = Float(components[1])
  108. let b = Float(components[2])
  109. var a = Float(1.0)
  110. if components.count == 4 {
  111. a = Float(components[3])
  112. }
  113. if alpha {
  114. return String(format: "#%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  115. } else {
  116. return String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  117. }
  118. }
  119. var toHex: String? {
  120. return toHex()
  121. }
  122. }
  123. let brightPalette = ColorPalette(isDark: false,
  124. name: "Default",
  125. statusBarStyle: .default,
  126. navigationbarColor: UIColor(0xFFFFFF),
  127. navigationbarTextColor: UIColor(0x000000),
  128. background: UIColor(0xF9F9F7),
  129. cellBackgroundA: UIColor(0xF9F9F7),
  130. cellBackgroundB: UIColor(0xE5E5E3),
  131. cellDetailTextColor: UIColor(0xA9A9A9),
  132. cellTextColor: UIColor(0x000000),
  133. lightTextColor: UIColor(0x888888),
  134. sectionHeaderTextColor: UIColor(0xF9F9F7),
  135. sectionHeaderTintColor: UIColor(0xE5EFE3),
  136. settingsBackground: UIColor(0xDCDCDC),
  137. settingsCellBackground: UIColor(0xF9F9F7),
  138. separatorColor: UIColor(0xD3D3D3),
  139. mediaCategorySeparatorColor: UIColor(0xECF2F6),
  140. tabBarColor: UIColor(0xFFFFFF),
  141. orangeUI: UIColor(0xFF8800))
  142. let darkPalette = ColorPalette(isDark: true,
  143. name: "Dark",
  144. statusBarStyle: .lightContent,
  145. navigationbarColor: UIColor(0x292B36),
  146. navigationbarTextColor: UIColor(0xD3D3D3),
  147. background: UIColor(0x1B1E21),
  148. cellBackgroundA: UIColor(0x292B36),
  149. cellBackgroundB: UIColor(0x000000),
  150. cellDetailTextColor: UIColor(0xD3D3D3),
  151. cellTextColor: UIColor(0xFFFFFF),
  152. lightTextColor: UIColor(0xB8B8B8),
  153. sectionHeaderTextColor: UIColor(0x828282),
  154. sectionHeaderTintColor: UIColor(0x3C3C3C),
  155. settingsBackground: UIColor(0x292B36),
  156. settingsCellBackground: UIColor(0x3D3F40),
  157. separatorColor: UIColor(0x25292C),
  158. mediaCategorySeparatorColor: UIColor(0x25292C),
  159. tabBarColor: UIColor(0x292B36),
  160. orangeUI: UIColor(0xFF8800))