I have this UIColor
:
The left color is shown on light appearance
The right color is shown on dark appearance
I want to be able to get the right color when the device is on light appearance,
and be able to get the left color when the device is on dark appearance.
I know I can just duplicate it and create another color, and use that one, but is there a way to get all the variations from a UIColor
variable from code? thanks
You can use UIColor method func resolvedColor(with traitCollection: UITraitCollection) -> UIColor
and select the desired trait collection:
extension UIColor {
var dark: UIColor { resolvedColor(with: .init(userInterfaceStyle: .dark)) }
var light: UIColor { resolvedColor(with: .init(userInterfaceStyle: .light)) }
}
let systemPurple = UIColor.systemPurple // r 0,686 g 0,322 b 0,871 a 1,0
systemPurple.dark // r 0,749 g 0,353 b 0,949 a 1,0
systemPurple.light // r 0,686 g 0,322 b 0,871 a 1,0