Posted By

suibhne on 10/14/11


Tagged

Objective-c iphone


Versions (?)

HTML Hex Color to NSColor


 / Published in: Objective C
 

Code that will convert a simple Hex code value (from HTML colors) to an actual useable NSColor.

  1. + (UIColor *) colorFromHexRGB:(NSString *) inColorString
  2. {
  3. UIColor *result = nil;
  4. unsigned int colorCode = 0;
  5. unsigned char redByte, greenByte, blueByte;
  6.  
  7. if (nil != inColorString)
  8. {
  9. NSScanner *scanner = [NSScanner scannerWithString:inColorString];
  10. (void) [scanner scanHexInt:&colorCode]; // ignore error
  11. }
  12. redByte = (unsigned char) (colorCode >> 16);
  13. greenByte = (unsigned char) (colorCode >> 8);
  14. blueByte = (unsigned char) (colorCode); // masks off high bits
  15. result = [UIColor
  16. colorWithRed: (float)redByte / 0xff
  17. green: (float)greenByte/ 0xff
  18. blue: (float)blueByte / 0xff
  19. alpha:1.0];
  20. return result;
  21. }

Report this snippet  

You need to login to post a comment.