UIColor -> Color

How to convert a UIColor to Color for use in SwiftUI?

Accepted Reply

Color has a new

init(_ color: UIColor)

Replies

You'll want to check the documentation, as this is shooting from memory, and has zero error checking or testing...but something like:


func convertUIColor (uiColor: UIColor) -> Color {
    return Color(red: Double(uiColor.cgColor.components![0]), green: Double(uiColor.cgColor.components![1]), blue: Double(uiColor.cgColor.components![2]))
}

One of the initializers for Color is a simple RGB value triplet, so extract the RGB values from the UIColor (I don't know of any other way except using CoreGraphics), and initialize your Color. This won't take into account color space or opacity values though.

Color has a new

init(_ color: UIColor)