title: How do I easily support light and dark mode with a custom color used in my app?
body: <p>Let's say I have a custom color in my app:</p>
  <pre class="lang-swift prettyprint-override"><code>extension UIColor {
      static var myControlBackground: UIColor {
          return UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 1)
      }
  }
  </code></pre>
  <p>I use this in a custom control (and other places) as the control's background:</p>
  <pre class="lang-swift prettyprint-override"><code>class MyControl: UIControl {
      override init(frame: CGRect) {
          super.init(frame: frame)
  
          setup()
      }
  
      required init?(coder: NSCoder) {
          super.init(coder: coder)
  
          setup()
      }
  
      private func setup() {
          backgroundColor = .myControlBackground
      }
  
      // Lots of code irrelevant to the question
  }
  </code></pre>
  <p>With iOS 13, I wish to have my custom control support both light and dark mode.</p>
  <p>One solution is to override <code>traitCollectionDidChange</code> and see if the color has changed and then update my background as needed. I also need to provide both a light and dark color.</p>
  <p>So I update my custom colors:</p>
  <pre class="lang-swift prettyprint-override"><code>extension UIColor {
      static var myControlBackgroundLight: UIColor {
          return UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 1)
      }
      static var myControlBackgroundDark: UIColor {
          return UIColor(red: 0.4, green: 0.3, blue: 0.2, alpha: 1)
      }
  }
  </code></pre>
  <p>And I update my control code:</p>
  <pre class="lang-swift prettyprint-override"><code>extension MyControl {
      override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
          super.traitCollectionDidChange(previousTraitCollection)
  
          if #available(iOS 13.0, *) {
              if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
                  backgroundColor = traitCollection.userInterfaceStyle == .dark ?
                     .myControlBackgroundDark : .myControlBackgroundLight
              }
          }
      }
  }
  </code></pre>
  <p>This seems to work but it's clunky and anywhere else I happen to use <code>myControlBackground</code> needs to have the same code added.</p>
  <p>Is there a better solution to having my custom color and control support both light and dark mode?</p>