Swift 5.4将十六进制转换为NSColor

3

我正在为macOS开发一个程序。

我需要将十六进制颜色转换为NSColor。

我查看了这里提出的解决方案:

将十六进制颜色代码转换为NSColor

如何将十六进制转换为NSColor?

但是,在Xcode 12.5.1下,它们都无法正常工作。

目前,我使用以下方法可以正常工作:

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)

可能不需要使用Cocoa。

我想要一个这样的函数:hexToNSColor("#2196f3")

你能帮我一下吗?


永远不要忘记先将颜色空间转换为RGB(链接中的代码没有这样做)。 - Marek H
2
问题是将十六进制转换为NSColor,而不是从NSColor获取十六进制。 - workingdog support Ukraine
@Paul,我有一个可能的答案,但无法在这里发布,因为“他们”关闭了它。 问另一个问题,比如:“Swift 5.4中的十六进制转NSColor而不是来自cocoa的NSColor的十六进制值”。 - workingdog support Ukraine
嘿,看起来我可以重新打开这个问题并发布我的答案。希望它能在所有这些之后有所帮助。 - workingdog support Ukraine
https://stackoverflow.com/questions/27430275/how-to-convert-hex-to-nscolor - Marek H
显示剩余2条评论
2个回答

9
你可以尝试像这样做:

编辑:包括toHex(alpha:),来自我多年前从网络上获得的代码。

编辑3,4:包括#RRGGBBAA的情况

编辑5:去掉十六进制字符串中的空格,以使NSColor(hex:“#2196f380”)也能正常工作。

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 #

编辑2:

你可以对UIColor进行相同操作,也可以使用UIColor或NSColor对Color进行操作:

extension Color {
    public init(hex: String) {
        self.init(UIColor(hex: hex))
    }

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

我更新了我的答案,并附上了alpha的代码。至于三元运算符,我将其留作练习。 - workingdog support Ukraine
添加了一个三元运算符的可能用法。 - workingdog support Ukraine
我不明白如何使用toHex函数。 例如,我有这样的代码:let fillColor = NSColor(hex: "#2196f380") - Paul
那么为什么 lroundf(a * 255) 的值,alpha 值不会从 0.0 到 1.0 呢? 我记错了吗? - Paul
我正在测试代码,但如果我这样做:NSColor (hex:" # 2196f380 ")似乎可以直接工作,而不必使用toHex,对吗? - Paul
显示剩余8条评论

0
/*
 // 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)
}

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接