Paul
Paul

Reputation: 4430

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?

Upvotes: 3

Views: 3228

Answers (2)

Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

/*
 // With hash
 let color: NSColor = NSColor(hexString: "#ff8942")

 // Without hash, with alpha
 let secondColor: NSColor = NSColor(hexString: "ff8942", alpha: 0.5)

 // Short handling
 let shortColorWithHex: NSColor = NSColor(hexString: "fff")

 // From a real hex value (an `Int`)
 // With hash
 let color: NSColor = NSColor(hex: 0xff8942)

 // Without hash, with alpha
 let secondColor: NSColor = NSColor(hex: 0xff8942, alpha: 0.5)

*/

#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit
typealias SWColor = UIColor
#else
import Cocoa
typealias SWColor = NSColor
#endif

private extension Int64 {
func duplicate4bits() -> Int64 {
    return (self << 4) + self
}
}

/// An extension of UIColor (on iOS) or NSColor (on OSX) providing HEX color handling.
public extension SWColor {
private convenience init?(hex3: Int64, alpha: Float) {
    self.init(red:   CGFloat( ((hex3 & 0xF00) >> 8).duplicate4bits() ) / 255.0,
              green: CGFloat( ((hex3 & 0x0F0) >> 4).duplicate4bits() ) / 255.0,
              blue:  CGFloat( ((hex3 & 0x00F) >> 0).duplicate4bits() ) / 255.0,
              alpha: CGFloat(alpha))
}

private convenience init?(hex4: Int64, alpha: Float?) {
    self.init(red:   CGFloat( ((hex4 & 0xF000) >> 12).duplicate4bits() ) / 255.0,
              green: CGFloat( ((hex4 & 0x0F00) >> 8).duplicate4bits() ) / 255.0,
              blue:  CGFloat( ((hex4 & 0x00F0) >> 4).duplicate4bits() ) / 255.0,
              alpha: alpha.map(CGFloat.init(_:)) ?? CGFloat( ((hex4 & 0x000F) >> 0).duplicate4bits() ) / 255.0)
}

private convenience init?(hex6: Int64, alpha: Float) {
    self.init(red:   CGFloat( (hex6 & 0xFF0000) >> 16 ) / 255.0,
              green: CGFloat( (hex6 & 0x00FF00) >> 8 ) / 255.0,
              blue:  CGFloat( (hex6 & 0x0000FF) >> 0 ) / 255.0, alpha: CGFloat(alpha))
}

private convenience init?(hex8: Int64, alpha: Float?) {
    self.init(red:   CGFloat( (hex8 & 0xFF000000) >> 24 ) / 255.0,
              green: CGFloat( (hex8 & 0x00FF0000) >> 16 ) / 255.0,
              blue:  CGFloat( (hex8 & 0x0000FF00) >> 8 ) / 255.0,
              alpha: alpha.map(CGFloat.init(_:)) ?? CGFloat( (hex8 & 0x000000FF) >> 0 ) / 255.0)
}

/**
 Create non-autoreleased color with in the given hex string and alpha.

 - parameter hexString: The hex string, with or without the hash character.
 - parameter alpha: The alpha value, a floating value between 0 and 1.
 - returns: A color with the given hex string and alpha.
 */
convenience init?(hexString: String, alpha: Float? = nil) {
    var hex = hexString

    // Check for hash and remove the hash
    if hex.hasPrefix("#") {
        hex = String(hex[hex.index(after: hex.startIndex)...])
    }

    guard let hexVal = Int64(hex, radix: 16) else {
        self.init()
        return nil
    }

    switch hex.count {
    case 3:
        self.init(hex3: hexVal, alpha: alpha ?? 1.0)
    case 4:
        self.init(hex4: hexVal, alpha: alpha)
    case 6:
        self.init(hex6: hexVal, alpha: alpha ?? 1.0)
    case 8:
        self.init(hex8: hexVal, alpha: alpha)
    default:
        // Note:
        // The swift 1.1 compiler is currently unable to destroy partially initialized classes in all cases,
        // so it disallows formation of a situation where it would have to.  We consider this a bug to be fixed
        // in future releases, not a feature. -- Apple Forum
        self.init()
        return nil
    }
}

/**
 Create non-autoreleased color with in the given hex value and alpha

 - parameter hex: The hex value. For example: 0xff8942 (no quotation).
 - parameter alpha: The alpha value, a floating value between 0 and 1.
 - returns: color with the given hex value and alpha
 */
convenience init?(hex: Int, alpha: Float = 1.0) {
    if (0x000000 ... 0xFFFFFF) ~= hex {
        self.init(hex6: Int64(hex), alpha: alpha)
    } else {
        self.init()
        return nil
    }
}

convenience init?(argbHex: Int) {
    if (0x00000000 ... 0xFFFFFFFF) ~= argbHex {
        let hex = Int64(argbHex)
        self.init(red: CGFloat( (hex & 0x00FF0000) >> 16 ) / 255.0,
                  green: CGFloat( (hex & 0x0000FF00) >> 8 ) / 255.0,
                  blue:  CGFloat( (hex & 0x000000FF) >> 0 ) / 255.0,
                  alpha: CGFloat( (hex & 0xFF000000) >> 24 ) / 255.0)
    } else {
        self.init()
        return nil
    }
}

convenience init?(argbHexString: String) {
    var hex = argbHexString

    // Check for hash and remove the hash
    if hex.hasPrefix("#") {
        hex = String(hex[hex.index(after: hex.startIndex)...])
    }
    
    guard hex.count == 8, let hexVal = Int64(hex, radix: 16) else {
        self.init()
        return nil
    }
    self.init(red: CGFloat( (hexVal & 0x00FF0000) >> 16 ) / 255.0,
              green: CGFloat( (hexVal & 0x0000FF00) >> 8 ) / 255.0,
              blue:  CGFloat( (hexVal & 0x000000FF) >> 0 ) / 255.0,
              alpha: CGFloat( (hexVal & 0xFF000000) >> 24 ) / 255.0)
}

}

Upvotes: 0

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)
    }
}

Upvotes: 11

Related Questions