Hello,
I've been trying to get the Hex value of an NSColor. I found an Apple technical Q&A for this, but can't make it work. I receive a EXC_BAD_ACCESS when calling the method.
The code from the class:
And then the main:
Does someone knows what's going on?
Thank you a lot!
I've been trying to get the Hex value of an NSColor. I found an Apple technical Q&A for this, but can't make it work. I receive a EXC_BAD_ACCESS when calling the method.
The code from the class:
Code:
@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
And then the main:
Code:
#import <Cocoa/Cocoa.h>
#import "HexConvert.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSColor *blue = [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:1.0];
NSString *hexName = [blue hexadecimalValueOfAnNSColor];
NSLog(hexName);
[pool release];
return NSApplicationMain(argc, (const char **) argv);
}
Does someone knows what's going on?
Thank you a lot!