Last active
December 16, 2020 18:53
-
-
Save jcarpenter/4c90645e0c4b0035bbdf6f0112d653c0 to your computer and use it in GitHub Desktop.
Output `NSColor.controlAccentColor` as RGBA HEX, from macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class ViewController: NSViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
print("controlAccentColor:", NSColor.controlAccentColor.rgbaHexString) | |
} | |
} | |
public extension NSColor { | |
// Return: '#CC0000FF', | |
var rgbaHexString: String { | |
guard let rgbColor = usingColorSpace(NSColorSpace.deviceRGB) else { | |
return "FFFFFFFF" | |
} | |
let red = Int(round(rgbColor.redComponent * 0xFF)) | |
let green = Int(round(rgbColor.greenComponent * 0xFF)) | |
let blue = Int(round(rgbColor.blueComponent * 0xFF)) | |
let alpha = Int(round(rgbColor.alphaComponent * 0xFF)) | |
let rgbaHexString = NSString(format: "'#%02X%02X%02X%02X',", red, green, blue, alpha) | |
return rgbaHexString as String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment