Search code examples
swiftinitcgcolor

Extension for CGColor with Error 'init(red:green:blue:alpha) is unavailable'


I am a little lost here. I am writing an extension to CGColor which returns a CGColor from a hex value (String) and an optional alpha value (CGFloat). On returning the new CGColor instance, the following error occurs:

'init(red:gee:blue:alpha)' is unavailable

The following info ist given:

  1. 'init(red:green:blue:alpha:)' has been explicitly marked unavailable here (CoreGraphics.CGColor)

Here is the complete extension code:

import Foundation
import CoreGraphics

extension CGColor {

    /// Construct a CGColor from a hex value and an optional alpha value.
    ///
    /// - Parameter hex: The hex value to use for the rgb value, must be in the form of "#ffffff"
    /// - Parameter alpha: Optional alpha value, ranges from 0 to 1.0
    ///
    /// - Returns: A CGColor or nil if the provided hex value is invalid.
    static func from(hex: String, alpha: CGFloat = 1.0) -> CGColor? {

        // not a hex value: missing prefix or invalid character count
        guard hex.hasPrefix("#") || hex.count == 7 else {
            return nil
        }

        let start = hex.index(hex.startIndex, offsetBy: 1)
        let hexColor = String(hex[start...])

        let scanner = Scanner(string: hexColor)
        var hexNumber: UInt64 = 0

        scanner.scanHexInt64(&hexNumber)

        let r = CGFloat((hexNumber & 0xff000000) >> 16) / 255
        let g = CGFloat((hexNumber & 0x00ff0000) >> 8) / 255
        let b = CGFloat(hexNumber & 0x0000ff00) / 255

        // here comes the error: 'init(red:green:blue:alpha)' is unavailable
        return CGColor(
            red: r,
            green: g,
            blue: b,
            alpha: alpha
        )
    }
}

[Edit]

For Context: This code is part of an universal framework targeting iOS and macOS. This framework parses a (huge) bunch of text files describing geometry and color objects. Color are provided as hex values.

This CGColor extension is used to return a CGColor from the parsed hex value.

Some example from the code:

// the geometry object, which has a color
public struct Geometry {
    public let color: Color
}

// the color definition for the geometry object
public struct Color {
    public let id: ColorID
    public let value: String
    public let alpha: Int

    // some code left out

    public var cgColor: CGColor {
        return CGColor?.from(hex: value, alpha: alphaValue)
    }
}

If the app, which uses the framework, needs a color:

let geometry = fromParsingFramework.geometry()
if let color = geometry.color.cgColor

Solution

  • Since that initializer is only for macOS, you can replace that with the following:

    let comps = [r,g,b,alpha]
    return CGColor(colorSpace: CGColorSpace(name: CGColorSpace.sRGB)!, components: comps)
    

    If your framework only needs to support iOS 13+/macOS 10.15+ then you can use the new initializer:

    return CGColor(srgbRed: r, green: g, blue: b, alpha: alpha)