iOS 14 shipped with native color pickers for UIKit (UIColorWell), AppKit (NSColorWell) and SwiftUI (ColorPicker).
While the color wells have a property selectedColor that exposes the selected UIColor/NSColor values, the SwiftUI ColorPicker takes a Binding<Color>. However, I can't find any information how to get anything useful from the Color value.
Creating a Color from a UIColor is easy using Color.init(_: UIColor) but there does not seem to be a way to get a UIColor from an existing Color or to extract the color components (e.g. the RGB values similar to the UIColor.getRed(_:green:blue:alpha:) method).
How do I get the underlying color values from a SwiftUI ColorPicker?
In majority of cases you use Color in SwiftUI directly, ie it is not needed to extract anything from it.
Anyway if it is needed to have UIColor SwiftUI 2.0 provides new API for that
extension UIColor { @available(iOS 14.0, tvOS 14.0, watchOS 7.0, *) @available(OSX, unavailable) public convenience init(_ color: Color) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With