Skip to content

Instantly share code, notes, and snippets.

@chris-redbeed
Last active March 6, 2024 22:05
Show Gist options
  • Save chris-redbeed/8cab0afa2bcf2e4cb6287e60a1700ab0 to your computer and use it in GitHub Desktop.
Save chris-redbeed/8cab0afa2bcf2e4cb6287e60a1700ab0 to your computer and use it in GitHub Desktop.
SwiftUI - UIColor to Color
// Convert UIColor to Color
import SwiftUI
extension UIColor {
var color: Color {
get {
let rgbColours = self.cgColor.components
return Color(
red: Double(rgbColours![0]),
green: Double(rgbColours![1]),
blue: Double(rgbColours![2])
)
}
}
}
@RyuuzakiJulio
Copy link

Great, just what I was looking for, thank you!

@winkelsdorf
Copy link

Are you aware of Color(color: UIColor)? Just use the initializer :)

If you prefer having an extension the following should do the trick:

extension UIColor {
    var color: Color { Color(self) }
}

@sourov2008
Copy link

Thanks both :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment