UIColor.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // UIColor.swift
  3. // LoopKitUI
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import UIKit
  8. private class FrameworkBundle {
  9. static let main = Bundle(for: FrameworkBundle.self)
  10. }
  11. private func BundleColor(_ name: String, compatibleWith traitCollection: UITraitCollection? = nil) -> UIColor? {
  12. return UIColor(named: name, in: FrameworkBundle.main, compatibleWith: traitCollection)
  13. }
  14. extension UIColor {
  15. static let delete = BundleColor("Delete")
  16. static let invalid = BundleColor("Invalid")
  17. static let warning = BundleColor("Warning")
  18. static let lightenedInsulin = BundleColor("Lightened Insulin")
  19. static let darkenedInsulin = BundleColor("Darkened Insulin")
  20. }
  21. extension UIColor {
  22. static func interpolatingBetween(_ first: UIColor, _ second: UIColor, biasTowardSecondColor bias: CGFloat = 0.5) -> UIColor {
  23. let (r1, g1, b1, a1) = first.components
  24. let (r2, g2, b2, a2) = second.components
  25. return UIColor(
  26. red: (r2 - r1) * bias + r1,
  27. green: (g2 - g1) * bias + g1,
  28. blue: (b2 - b1) * bias + b1,
  29. alpha: (a2 - a1) * bias + a1
  30. )
  31. }
  32. var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
  33. var r, g, b, a: CGFloat
  34. (r, g, b, a) = (0, 0, 0, 0)
  35. getRed(&r, green: &g, blue: &b, alpha: &a)
  36. return (red: r, green: g, blue: b, alpha: a)
  37. }
  38. }