Search code examples
swiftmacoshexnscolor

Swift 5.4 hex to NSColor


I am developing a program for macOS.

I need to convert a hex color to NSColor.

I looked at the proposed solutions here:

Convert Hex Color Code to NSColor

How to convert hex to NSColor?

But none of it works correctly with Xcode 12.5.1.

At the moment I did this, it works correctly:

extension NSObject {
    func RGB(r:CGFloat, g:CGFloat, b:CGFloat, alpha:CGFloat? = 1) -> NSColor {
        return NSColor(red: r/255, green: g/255, blue: b/255, alpha: alpha!)
    }
}

let fillColor = RGB(r: 33, g: 150, b: 243)

Possibly not having to use Cocoa.

I would like a function like this: hexToNSColor("#2196f3")

Can you give me a hand?


Solution

  • you could try something like this:

    EDIT: included toHex(alpha:), from code I probably got from the net somewhere many years ago.

    EDIT3,4: included the case for #RRGGBBAA

    EDIT 5: stripping blank spaces in the hex string, to make NSColor (hex:" # 2196f380 ") work as well.

    extension NSColor {
        
     convenience init(hex: String) {
        let trimHex = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        let dropHash = String(trimHex.dropFirst()).trimmingCharacters(in: .whitespacesAndNewlines)
        let hexString = trimHex.starts(with: "#") ? dropHash : trimHex
        let ui64 = UInt64(hexString, radix: 16)
        let value = ui64 != nil ? Int(ui64!) : 0
        // #RRGGBB
        var components = (
            R: CGFloat((value >> 16) & 0xff) / 255,
            G: CGFloat((value >> 08) & 0xff) / 255,
            B: CGFloat((value >> 00) & 0xff) / 255,
            a: CGFloat(1)
        )
        if String(hexString).count == 8 {
            // #RRGGBBAA
            components = (
                R: CGFloat((value >> 24) & 0xff) / 255,
                G: CGFloat((value >> 16) & 0xff) / 255,
                B: CGFloat((value >> 08) & 0xff) / 255,
                a: CGFloat((value >> 00) & 0xff) / 255
            )
        }
        self.init(red: components.R, green: components.G, blue: components.B, alpha: components.a)
    }
    
    func toHex(alpha: Bool = false) -> String? {
        guard let components = cgColor.components, components.count >= 3 else {
            return nil
        }
        
        let r = Float(components[0])
        let g = Float(components[1])
        let b = Float(components[2])
        var a = Float(1.0)
        
        if components.count >= 4 {
            a = Float(components[3])
        }
        
        if alpha {
            return String(format: "%02lX%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255), lroundf(a * 255))
        } else {
            return String(format: "%02lX%02lX%02lX", lroundf(r * 255), lroundf(g * 255), lroundf(b * 255))
        }
    }
    }
     
        let nscol = NSColor(hex: "#2196f3")  // <-- with or without #
    

    EDIT2:

    you can do the same for UIColor, and for Color (with UIColor or NSColor):

    extension Color {
        public init(hex: String) {
            self.init(UIColor(hex: hex))
        }
    
        public func toHex(alpha: Bool = false) -> String? {
            UIColor(self).toHex(alpha: alpha)
        }
    }