- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 macOS 开发一个程序。
我需要将十六进制颜色转换为 NSColor。
我在这里查看了建议的解决方案:
Convert Hex Color Code to NSColor
How to convert hex to 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")
你能帮帮我吗?
最佳答案
你可以试试这样的:
编辑:包括 toHex(alpha:),来 self 可能多年前从网上某处获得的代码。
EDIT3,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 #
EDIT2:
您可以对 UIColor 和 Color(使用 UIColor 或 NSColor)执行相同的操作:
extension Color {
public init(hex: String) {
self.init(UIColor(hex: hex))
}
public func toHex(alpha: Bool = false) -> String? {
UIColor(self).toHex(alpha: alpha)
}
}
关于Swift 5.4 hex 到 NSColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68679658/
你好,我是 SMARTRF-05EB - 评估板的新手,我已经在 Linux 虚拟机中安装了 SDCC 和 Contiki-2.6。我能够构建此目录 contiki-2.6/contiki/examp
下面的简单代码会产生奇怪的输出: #include #include #include "/tmp/sha.h" #define DIGEST 64 //taken from coreutils
我不知道为什么 Hex 函数返回一个像 '0x41' 而不是 0x41 的字符串 我需要将 ASCII 值转换为十六进制。但我想要 0x INT 格式,而不是 '0x' 字符串。 ascii = 36
所以我试图从十六进制文件中读取,修改十六进制值,将新的十六进制值写入新文件。然后打开新文件,再次修改十六进制并重写到第三个文件。我正在对十六进制值进行非常简单的加密。 我用来阅读的函数是休闲的: ve
是否有一种惯用的方式将 Clojure 中的字符串编码和解码为十六进制?来自 Python 的示例: 'Clojure'.encode('hex') # ⇒ '436c6f6a757265' '436
下面的函数可以完美地将字符串转换为十六进制: function String2Hex(const Buffer: AnsiString): string; begin SetLength(Resu
我要做的是将 double 字符串转换为十六进制字符串,然后再转换回 double 。 以下代码将 double 字符串转换为十六进制字符串。 char * double2HexString(doub
我正在尝试将整数转换为十六进制。我找到了解决此问题的答案,例如 this ,尽管它们对我不起作用。我的意思是: 如果我拿这个: buf = "" buf += "\xda\xc7\xd9\x74\x
许多在线示例将哈希显示为十六进制表示,它们通常是自定义实现。改用 Apache Commons Base64 编码有什么问题或安全性降低吗?在阅读有关编码的内容时,通常是在如何将二进制表示为 XML
我需要存储这种类型的值 0xff0000或 0x00ff08 (十六进制颜色表示)在solidity智能合约中,并且能够在合约内将其转换为具有相同文本字符的字符串"ff0000" .我打算在 RSK
如果我有两种颜色: #ffcc00 和#334455 我如何获得这两种颜色之间的所有#hex 颜色,并逐渐排序? 最佳答案 你可以试试这个工具: http://www.colorhexa.com/ff
我的命令输出类似于 0x53 0x48 0x41 0x53 0x48 0x49。现在我需要将其存储在十六进制值中,然后将其转换为 ASCII 作为 SHASHI。 我试过的- 我尝试将十六进制值存储为
我正在使用计算 16 位 CRC 校验和的函数。 该函数生成一个包含校验和的 LONG(以 10 为基数的数字格式)。当然,这可以以十六进制等效形式打印到控制台,如下所示: printf("Check
我有一些以 INTEGER 形式提供给我的数字数据。我将它与其他数据一起插入到 SQLite 中,但当我将其写出时 INTEGER 数字需要为 8 位 Hex 数字,前导零。 例如 输入 400 80
在尝试附加数据对象转换为十六进制值后,我有如下代码,但它给了我以下错误: Unexpectedly found nil while unwrapping an Optional value let n
我有三个程序。程序A的代码如下: #include "stdafx.h" #include #include #include using namespace std; int _tmain(
我得到了这段代码,它从十六进制转换为 base64,反之亦然。我从另一个 SO 问题中得到了 to_base64,然后我通过一些猜测和反复试验编写了 to_hex。 class String de
我有一些关于控制建筑物访问的磁卡的信息,并且需要对软件在卡上存储数据的方式进行逆向工程,以制作具有更多功能的东西。 (我们已经和原来的程序员失去了联系。) 我不知道如何编写订单;我不是磁卡专家。 我知
我想在我拥有的 HEX 文件中找到模式并按出现次数对它们进行排序。 我不是在寻找某种特定的模式,只是为了对那里发生的事件进行一些统计并对其进行排序。 DB0DDAEEDAF7DAF5DB1FDB1DD
我在出示卡片时从 RFID 阅读器接收字节,但我无法弄清楚如何从这些字节中获取卡 ID。 例如,我有一张卡片,上面印有这些数字: 0007625328 116,23152 .我希望这是那张卡的 ID,
我是一名优秀的程序员,十分优秀!