UIColor.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // UIColor.swift
  3. // LoopKitUI
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import UIKit
  8. private class LocalBundle {
  9. /// Returns the resource bundle associated with the current Swift module.
  10. static var main: Bundle = {
  11. if let mainResourceURL = Bundle.main.resourceURL,
  12. let bundle = Bundle(url: mainResourceURL.appendingPathComponent("LoopKitUI_LoopKitUI.bundle"))
  13. {
  14. return bundle
  15. }
  16. return Bundle(for: LocalBundle.self)
  17. }()
  18. }
  19. private func BundleColor(_ name: String, compatibleWith traitCollection: UITraitCollection? = nil) -> UIColor? {
  20. return UIColor(named: name, in: LocalBundle.main, compatibleWith: traitCollection)
  21. }
  22. extension UIColor {
  23. @nonobjc static let lightenedInsulin = BundleColor("Lightened Insulin") ?? systemOrange
  24. @nonobjc static let darkenedInsulin = BundleColor("Darkened Insulin") ?? systemOrange
  25. static func interpolatingBetween(_ first: UIColor, _ second: UIColor, biasTowardSecondColor bias: CGFloat = 0.5) -> UIColor {
  26. let (r1, g1, b1, a1) = first.components
  27. let (r2, g2, b2, a2) = second.components
  28. return UIColor(
  29. red: (r2 - r1) * bias + r1,
  30. green: (g2 - g1) * bias + g1,
  31. blue: (b2 - b1) * bias + b1,
  32. alpha: (a2 - a1) * bias + a1
  33. )
  34. }
  35. var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
  36. var r, g, b, a: CGFloat
  37. (r, g, b, a) = (0, 0, 0, 0)
  38. getRed(&r, green: &g, blue: &b, alpha: &a)
  39. return (red: r, green: g, blue: b, alpha: a)
  40. }
  41. }