Last active
March 6, 2024 22:05
-
-
Save chris-redbeed/8cab0afa2bcf2e4cb6287e60a1700ab0 to your computer and use it in GitHub Desktop.
SwiftUI - UIColor to Color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]) | |
) | |
} | |
} | |
} |
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) }
}
Thanks both :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, just what I was looking for, thank you!