Re: How to Convert "#FF00FF" String Specifying RGB Color to 32 bit int
Re: How to Convert "#FF00FF" String Specifying RGB Color to 32 bit int
- Subject: Re: How to Convert "#FF00FF" String Specifying RGB Color to 32 bit int
- From: Gideon King <email@hidden>
- Date: Thu, 27 Aug 2009 10:07:19 +1000
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 (email@hidden)
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:
This email sent to email@hidden