我有一个输入 RGV 值的 NSColorPanel:
NSColorPanel * sharedPanel = [NSColorPanel sharedColorPanel];
[sharedPanel setTarget: self];
[sharedPanel setAction: updateColor:];
[sharedPanel orderFront: self];
彩色面板显示和我设置这个值:r66,g114,b170
根据我的计算,这应该是#4272AA。我使用以下代码转换为十六进制:
- (void) updateColor: (NSColorPanel*) panel
{
NSString * hexString = [panel.color hexadecimalValueOfAnNSColor];
NSLog(@"%@", hexString);
}
哪个注销#345d9a
(不是我所期望的)。
我直接从developer.apple.com使用以下方法将颜色转换为十六进制:
#import <Cocoa/Cocoa.h>
@interface NSColor(NSColorHexadecimalValue)
-(NSString *)hexadecimalValueOfAnNSColor;
@end
@implementation NSColor(NSColorHexadecimalValue)
-(NSString *)hexadecimalValueOfAnNSColor
{
float redFloatValue, greenFloatValue, blueFloatValue;
int redIntValue, greenIntValue, blueIntValue;
NSString *redHexValue, *greenHexValue, *blueHexValue;
//Convert the NSColor to the RGB color space before we can access its components
NSColor *convertedColor=[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
if(convertedColor)
{
// Get the red, green, and blue components of the color
[convertedColor getRed:&redFloatValue green:&greenFloatValue blue:&blueFloatValue alpha:NULL];
// Convert the components to numbers (unsigned decimal integer) between 0 and 255
redIntValue=redFloatValue*255.99999f;
greenIntValue=greenFloatValue*255.99999f;
blueIntValue=blueFloatValue*255.99999f;
// Convert the numbers to hex strings
redHexValue=[NSString stringWithFormat:@"%02x", redIntValue];
greenHexValue=[NSString stringWithFormat:@"%02x", greenIntValue];
blueHexValue=[NSString stringWithFormat:@"%02x", blueIntValue];
// Concatenate the red, green, and blue components' hex strings together with a "#"
return [NSString stringWithFormat:@"#%@%@%@", redHexValue, greenHexValue, blueHexValue];
}
return nil;
}
@end
关于我做错了什么的任何建议?