I have this function which transform a color in hexString :
public extension NSColor {
func getHexString() -> String {
let red = Int(round(self.redComponent * 0xFF))
let green = Int(round(self.greenComponent * 0xFF))
let blue = Int(round(self.blueComponent * 0xFF))
let hexValue = NSString(format: "#%02X%02X%02X", red, green, blue)
return hexValue
}
}
Now , i have no idea how to reverse this.
All i have is this objective-c code , but i'm not able to convert it to swift.
(NSColor*)colorWithHexColorString:(NSString*)inColorString
{
NSColor* result = nil;
unsigned colorCode = 0;
unsigned char redByte, greenByte, blueByte;
if (nil != inColorString)
{
NSScanner* scanner = [NSScanner scannerWithString:inColorString];
(void) [scanner scanHexInt:&colorCode];
}
redByte = (unsigned char)(colorCode >> 16);
greenByte = (unsigned char)(colorCode >> 8);
blueByte = (unsigned char)(colorCode);
result = [NSColor
colorWithCalibratedRed:(CGFloat)redByte
green:(CGFloat)greenByte
blue:(CGFloat)blueByte
alpha:1.0];
return result;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…