Created
January 19, 2024 17:52
-
-
Save lucaswkuipers/278af5d2afb4c46ed3eeb1f2b9d2cc22 to your computer and use it in GitHub Desktop.
RGBA components from SwiftUI.Color
This file contains hidden or 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
import SwiftUI | |
#if canImport(UIKit) | |
typealias PlatformColor = UIColor | |
#elseif canImport(AppKit) | |
typealias PlatformColor = NSColor | |
#endif | |
extension Color { | |
var rgba: (red: Double, green: Double, blue: Double, alpha: Double) { | |
var red = CGFloat() | |
var green = CGFloat() | |
var blue = CGFloat() | |
var alpha = CGFloat() | |
PlatformColor(self) | |
#if canImport(AppKit) | |
.usingColorSpace(.sRGB)! | |
#endif | |
.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | |
return (red: red, green: green, blue: blue, alpha: alpha) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment