Whilst the code below worked previously, it has stopped working in Xcode 8 Beta 4, presumably because the components
return was a very un-Swift-y C-array of floats,
@Hamish's answer works, but my original intent was not to use UIColor
or NSColor
so that my code works in both iOS & MacOS. @LeoDabus suggested using SKColor
, but that's just a type alias to either NSColor
or UIColor
, and doesn't have a direct init from CGColor
anyway, however, Leo's suggestion prompted me to refine my kludge using CIColor
instead:
import CoreImage
extension CGColor {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
let ciColor = CIColor(cgColor: self)
return (ciColor.red, ciColor.green, ciColor.blue, ciColor.alpha)
}
}