UIColor -> Color

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

Answered by yvsong in 379204022

Color has a new

init(_ color: UIColor)

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.

Accepted Answer

Color has a new

init(_ color: UIColor)
UIColor -> Color
 
 
Q