将Hex Color Code转换为NSColor

时间:2012-01-02 03:39:33

标签: objective-c macos cocoa

我在将十六进制代码转换为NSColor时遇到了一些麻烦。请注意,这适用于Mac App(因此是NSColor而不是UIColor)。这是我到目前为止的代码:

- (NSColor *) createNSColorFromString:(NSString *)string {
NSString* hexNum = [string substringFromIndex:1];
NSColor* color = nil;
unsigned int colorCode = 0;
unsigned char red, green, blue;
if (string) {
    NSScanner* scanner = [NSScanner scannerWithString:hexNum];
    (void) [scanner scanHexInt:&colorCode];
}
red = (unsigned char) (colorCode >> 16);
green = (unsigned char) (colorCode >> 8);
blue = (unsigned char) (colorCode);
color = [NSColor colorWithCalibratedRed:(float)red / 0xff green:(float)green / 0xff blue:(float)blue / 0xff alpha:1.0];
return color;

}

任何帮助都将不胜感激。

5 个答案:

答案 0 :(得分:26)

+ (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]; // ignore error
    }
    redByte = (unsigned char)(colorCode >> 16);
    greenByte = (unsigned char)(colorCode >> 8);
    blueByte = (unsigned char)(colorCode); // masks off high bits

    result = [NSColor
    colorWithCalibratedRed:(CGFloat)redByte / 0xff
    green:(CGFloat)greenByte / 0xff
    blue:(CGFloat)blueByte / 0xff
    alpha:1.0];
    return result;
    }

它不考虑alpha值,它假设像“FFAABB”这样的值,但它很容易修改。

答案 1 :(得分:7)

这是两个非常有用的宏

#define RGBA(r,g,b,a) [NSColor colorWithCalibratedRed:r/255.f green:g/255.f blue:b/255.f alpha:a/255.f]

#define NSColorFromRGB(rgbValue) [NSColor colorWithCalibratedRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

答案 2 :(得分:3)

这是Zlatan上面回答的快速2兼容版本(对他来说是+1!):

func getColorFromString(webColorString : String) -> NSColor?
{
    var result : NSColor? = nil
    var colorCode : UInt32 = 0
    var redByte, greenByte, blueByte : UInt8

    // these two lines are for web color strings that start with a #
    // -- as in #ABCDEF; remove if you don't have # in the string
    let index1 = webColorString.endIndex.advancedBy(-6)
    let substring1 = webColorString.substringFromIndex(index1)

    let scanner = NSScanner(string: substring1)
    let success = scanner.scanHexInt(&colorCode)

    if success == true {
        redByte = UInt8.init(truncatingBitPattern: (colorCode >> 16))
        greenByte = UInt8.init(truncatingBitPattern: (colorCode >> 8))
        blueByte = UInt8.init(truncatingBitPattern: colorCode) // masks off high bits

        result = NSColor(calibratedRed: CGFloat(redByte) / 0xff, green: CGFloat(greenByte) / 0xff, blue: CGFloat(blueByte) / 0xff, alpha: 1.0)
    }
    return result
}

答案 3 :(得分:0)

支持7种十六进制类型:

NSColorParser.nsColor("#FF0000",1)//red nsColor
NSColorParser.nsColor("FF0",1)//red nsColor
NSColorParser.nsColor("0xFF0000",1)//red nsColor
NSColorParser.nsColor("#FF0000",1)//red nsColor
NSColorParser.nsColor("FF0000",1)//red nsColor
NSColorParser.nsColor(0xFF0000,1)//red nsColor
NSColorParser.nsColor(16711935,1)//red nsColor

http://stylekit.org/blog/2015/11/09/Supporting-7-Hex-color-types/

注意:这不是即插即用的,您必须在代码中稍微挖掘一下。但它的全部和它可能比滚动你自己更快。

答案 4 :(得分:0)

PocketSVG 中的 hexTriplet 结构包含一个有趣的替代解决方案。它输出一个 CGColor 但如果你想跨平台使用它可能是一件好事:

https://github.com/pocketsvg/PocketSVG/blob/master/Sources/SVGEngine.mm