PresentationTheme.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 tabBarColor: UIColor
  34. public let orangeUI: UIColor
  35. public init(isDark: Bool,
  36. name: String,
  37. statusBarStyle:UIStatusBarStyle,
  38. navigationbarColor: UIColor,
  39. navigationbarTextColor: UIColor,
  40. background: UIColor,
  41. cellBackgroundA: UIColor,
  42. cellBackgroundB: UIColor,
  43. cellDetailTextColor: UIColor,
  44. cellTextColor: UIColor,
  45. lightTextColor: UIColor,
  46. sectionHeaderTextColor: UIColor,
  47. sectionHeaderTintColor: UIColor,
  48. settingsBackground: UIColor,
  49. settingsCellBackground: UIColor,
  50. settingsSeparatorColor: UIColor,
  51. tabBarColor: UIColor,
  52. orangeUI: UIColor) {
  53. self.isDark = isDark
  54. self.name = name
  55. self.statusBarStyle = statusBarStyle
  56. self.navigationbarColor = navigationbarColor
  57. self.navigationbarTextColor = navigationbarTextColor
  58. self.background = background
  59. self.cellBackgroundA = cellBackgroundA
  60. self.cellBackgroundB = cellBackgroundB
  61. self.cellDetailTextColor = cellDetailTextColor
  62. self.cellTextColor = cellTextColor
  63. self.lightTextColor = lightTextColor
  64. self.sectionHeaderTextColor = sectionHeaderTextColor
  65. self.sectionHeaderTintColor = sectionHeaderTintColor
  66. self.settingsBackground = settingsBackground
  67. self.settingsCellBackground = settingsCellBackground
  68. self.settingsSeparatorColor = settingsSeparatorColor
  69. self.tabBarColor = tabBarColor
  70. self.orangeUI = orangeUI
  71. }
  72. }
  73. @objcMembers public class PresentationTheme: NSObject {
  74. public static let brightTheme = PresentationTheme(colors: brightPalette)
  75. public static let darkTheme = PresentationTheme(colors: darkPalette)
  76. static var current: PresentationTheme = {
  77. let isDarkTheme = UserDefaults.standard.bool(forKey: kVLCSettingAppTheme)
  78. return isDarkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme
  79. }() {
  80. didSet {
  81. AppearanceManager.setupAppearance(theme: self.current)
  82. NotificationCenter.default.post(name: .VLCThemeDidChangeNotification, object: self)
  83. }
  84. }
  85. public init(colors: ColorPalette) {
  86. self.colors = colors
  87. }
  88. public let colors: ColorPalette
  89. }
  90. @objc public extension UIColor {
  91. public convenience init(_ rgbValue: UInt32, _ alpha: CGFloat = 1.0) {
  92. let r = CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0
  93. let g = CGFloat((rgbValue & 0xFF00) >> 8) / 255.0
  94. let b = CGFloat(rgbValue & 0xFF) / 255.0
  95. self.init(red: r, green: g, blue: b, alpha: 1.0)
  96. }
  97. private func toHex(alpha: Bool = false) -> String? {
  98. guard let components = cgColor.components, components.count >= 3 else {
  99. assertionFailure()
  100. return nil
  101. }
  102. let r = Float(components[0])
  103. let g = Float(components[1])
  104. let b = Float(components[2])
  105. var a = Float(1.0)
  106. if components.count == 4 {
  107. a = Float(components[3])
  108. }
  109. if alpha {
  110. return String(format: "#%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
  111. } else {
  112. return String(format: "#%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
  113. }
  114. }
  115. var toHex: String? {
  116. return toHex()
  117. }
  118. }
  119. let brightPalette = ColorPalette(isDark: false,
  120. name: "Default",
  121. statusBarStyle: .default,
  122. navigationbarColor: UIColor(0xFFFFFF),
  123. navigationbarTextColor: UIColor(0x000000),
  124. background: UIColor(0xF9F9F7),
  125. cellBackgroundA: UIColor(0xF9F9F7),
  126. cellBackgroundB: UIColor(0xE5E5E3),
  127. cellDetailTextColor: UIColor(0xA9A9A9),
  128. cellTextColor: UIColor(0x000000),
  129. lightTextColor: UIColor(0x888888),
  130. sectionHeaderTextColor: UIColor(0xF9F9F7),
  131. sectionHeaderTintColor: UIColor(0xE5EFE3),
  132. settingsBackground: UIColor(0xDCDCDC),
  133. settingsCellBackground: UIColor(0xF9F9F7),
  134. settingsSeparatorColor: UIColor(0xD3D3D3),
  135. tabBarColor: UIColor(0xFFFFFF),
  136. orangeUI: UIColor(0xFF8800))
  137. let darkPalette = ColorPalette(isDark: true,
  138. name: "Dark",
  139. statusBarStyle: .lightContent,
  140. navigationbarColor: UIColor(0x292B36),
  141. navigationbarTextColor: UIColor(0xD3D3D3),
  142. background: UIColor(0x292B36),
  143. cellBackgroundA: UIColor(0x292B36),
  144. cellBackgroundB: UIColor(0x000000),
  145. cellDetailTextColor: UIColor(0xD3D3D3),
  146. cellTextColor: UIColor(0xFFFFFF),
  147. lightTextColor: UIColor(0xB8B8B8),
  148. sectionHeaderTextColor: UIColor(0x828282),
  149. sectionHeaderTintColor: UIColor(0x3C3C3C),
  150. settingsBackground: UIColor(0x292B36),
  151. settingsCellBackground: UIColor(0x3D3F40),
  152. settingsSeparatorColor: UIColor(0xA9A9A9),
  153. tabBarColor: UIColor(0x292B36),
  154. orangeUI: UIColor(0xFF8800))