Posted By

andrewroycarter on 01/20/11


Tagged

hex color UIColor


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

terababy
cnathan


UIColor from Hex & Photoshop Values


 / Published in: Objective C
 

  1. //In header
  2. #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  3.  
  4. //usage
  5. UIColor *color = UIColorFromRGB(0x000000)
  6.  
  7. //you can also use it inline
  8. [text.textField setTextColor:UIColorFromRGB(0xcccccc)];
  9.  
  10. #define PhotoshopColorValue(x) (x / 255.0)
  11.  
  12. #define UIColorFromPhotoshopRGBA(r,g,b,a) [UIColor colorWithRed:PhotoshopColorValue(r) green:PhotoshopColorValue(g) blue:PhotoshopColorValue(b) alpha:a]
  13.  
  14. #define UIColorFromPhotoshopHSBA(h,s,b,a) [UIColor colorWithHue:PhotoshopColorValue(h) saturation:PhotoshopColorValue(s) brightness:PhotoshopColorValue(b) alpha:a]
  15.  
  16. #define UIColorFromHexValue(hex) UIColorFromPhotoshopRGBA(((hex & 0xFF0000) >> 16), ((hex & 0xFF00) >> 8), (hex & 0xFF), 1.0)

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: joelgarrett on March 10, 2011

Here are a few additions/modifications:

define PhotoshopColorValue(x) (x / 255.0)

define UIColorFromPhotoshopRGBA(r,g,b,a) [UIColor colorWithRed:PhotoshopColorValue(r) green:PhotoshopColorValue(g) blue:PhotoshopColorValue(b) alpha:a]

define UIColorFromPhotoshopHSBA(h,s,b,a) [UIColor colorWithHue:PhotoshopColorValue(h) saturation:PhotoshopColorValue(s) brightness:PhotoshopColorValue(b) alpha:a]

define UIColorFromHexValue(hex) UIColorFromPhotoshopRGBA(((hex & 0xFF0000) >> 16), ((hex & 0xFF00) >> 8), (hex & 0xFF), 1.0)

You need to login to post a comment.