PresentationTheme.swift 6.3 KB

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