PresentationTheme.swift 6.4 KB

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