Does anyone know if it's possible to get a hexadecimal value of the color from NSColor in Cocoa?
Example using blue NSColor:
NSColor* color = [NSColor blueColor];
NSString* hexString = [NSString stringWithFormat:@"%02X%02X%02X",
(int) (color.redComponent * 0xFF), (int) (color.greenComponent * 0xFF),
(int) (color.blueComponent * 0xFF)];
I like D.A.H 's solution and I make it a computed property instead of a function:
import Cocoa
extension NSColor {
var hexString: String {
let red = Int(round(self.redComponent * 0xFF))
let green = Int(round(self.greenComponent * 0xFF))
let blue = Int(round(self.blueComponent * 0xFF))
let hexString = NSString(format: "#%02X%02X%02X", red, green, blue)
return hexString as String
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With