macos 将十六进制颜色代码转换为 NSColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8697205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:44:13  来源:igfitidea点击:

Convert Hex Color Code to NSColor

objective-cmacoscocoa

提问by mdominick

I am having some trouble converting a a hexcode to an NSColor. Note this is for a Mac App (hence the NSColor instead of UIColor). This is the code I have so far:

我在将 aa 十六进制代码转换为 NSColor 时遇到了一些麻烦。请注意,这是针对 Mac 应用程序的(因此使用 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;

}

}

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Zlatan

+ (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;
    }

It doesn't take alpha values into account, it assumes values like "FFAABB", but it would be easy to modify.

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

回答by serj

Here is two very useful macros

这是两个非常有用的宏

#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]

回答by Michael Dautermann

Here's the swift 2 compatible version of Zlatan's answer above (and +1 to him!):

这是上面 Zlatan 答案的 swift 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
}

回答by eonist

Support for 7 hex types:

支持 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/

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

NOTE:This isn't plug and play, you have to dig a little in the code. But its all there and its probably faster than rolling your own.

注意:这不是即插即用,您必须深入了解代码。但它就在那里,它可能比滚动你自己的更快。