Convert NSColor to RGB

倖福魔咒の 提交于 2019-12-06 03:53:38

问题


I'm trying to convert an NSColor to RGB, but it seems to give an entirely incorrect result:

NSColor *testColor = [NSColor colorWithCalibratedWhite:0.65 alpha:1.0];

const CGFloat* components = CGColorGetComponents(testColor.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]);
NSLog(@"Blue: %f", components[2]);
NSLog(@"Alpha: %f", CGColorGetAlpha(testColor.CGColor));

I get back : red = 0.65 - green = 1.0 - blue = 0.0 and alpha is 1.0 - which results in an entirely different color. (It should be gray, now it's green).

Am I doing something wrong?


回答1:


You need to convert the color to an RGB color space using an NSColorSpace object first, then you can get the components using the various NSColor accessor methods




回答2:


For a NSColor * color

CGFloat red = [color redComponent];
CGFloat green = [color greenComponent];
CGFloat blue = [color blueComponent];



回答3:


Extracting RGBA values from NSColor: (Swift 3)

let nsColor:NSColor = NSColor.red
let ciColor:CIColor = CIColor(color: nsColor)!
print(ciColor.red)//1.0
print(ciColor.green)//0.0
print(ciColor.blue)//0.0
print(ciColor.alpha)//1.0 /*or use nsColor.alphaComponent*/

NOTE: NSColor.blackColor().redComponent will crash the app, but the above code won't




回答4:


I had the same problem when I wanted to convert a picked color to hexadecimal. NSColor components values was not correct. I managed to resolve my problem with your comment above.

Example in Swift:

let colorTest = NSColor.init(calibratedWhite: 0.65, alpha: 1.0)
let color = colorTest.usingColorSpace(NSColorSpace.deviceRGB) ?? colorTest
print(colorTest)
// NSCalibratedWhiteColorSpace 0.65 1
print(colorTest.colorSpace) 
// Generic Gray colorspace
print("red: \(color.redComponent) green:\(color.greenComponent) blue:\(color.blueComponent)") 
// red: 0.708725869655609 green:0.708725869655609 blue:0.708725869655609



回答5:


I have used this in the past, and it worked for me.

    NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];
    NSColor *testColor = [NSColor colorWithColorSpace:colorSpace components:SRGB];

    CGFloat red = [testColor redComponent];

    CGFloat green = [testColor greenComponent];

    CGFloat blue = [testColor blueComponent];



回答6:


You have to check the colorspace first

then if it's rgb you can use

CGFloat red = [testColor redComponent];
...

For grayscale you have to convert it differently

CGFloat red = [testColor whiteComponent];
CGFloat blue = [testColor whiteComponent];
CGFloat green = [testColor whiteComponent];


来源:https://stackoverflow.com/questions/15682923/convert-nscolor-to-rgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!