Here's what we use to convert from an ARGB hex value to an NSColor -
you could perhaps use a similar method and just shift the bytes and or
them together as required for your unsigned int.
+ (NSColor *)colorWithHexARGB:(NSString*)hexString {
unsigned int redByte, greenByte, blueByte, alphaByte;
if ([hexString length] != 9) {
return nil;
}
NSScanner *scanner = [NSScanner scannerWithString:[hexString
substringWithRange:NSMakeRange(1, 2)]];
[scanner scanHexInt:&alphaByte];
scanner = [NSScanner scannerWithString:[hexString
substringWithRange:NSMakeRange(3, 2)]];
[scanner scanHexInt:&redByte];
scanner = [NSScanner scannerWithString:[hexString
substringWithRange:NSMakeRange(5, 2)]];
[scanner scanHexInt:&greenByte];
scanner = [NSScanner scannerWithString:[hexString
substringWithRange:NSMakeRange(7, 2)]];
[scanner scanHexInt:&blueByte];
return [NSColor colorWithCalibratedRed:(float)redByte / 0xff
green:(float)greenByte / 0xff
blue:(float)blueByte / 0xff
alpha:(float)alphaByte / 0xff];
}
HTH
Gideon
I have some xml where an rgb color value is specified via a
"#XXYYZZ" type string. The xml is being read in via NSXMLParser. I
can't seem to find an easy Cocoa way to convert this value to an
unsigned int. I need an unsigned int rather than an NSColor as the
value is used in C++ code. Does anyone know of something I may be
missing?
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to arch...@mail-archive.com