Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex color value in objective-c

I have hex value in mind that I need to implement, working on ipad by the way. Anyways, how can I go about implementing that with objective-c

Help much needed, pleas and thank you!

like image 465
Shane Da Silva Avatar asked Nov 05 '10 20:11

Shane Da Silva


1 Answers

UIColor's range is from 0 to 1. So you just need to convert the hex color string into decimal, then divide by 255 to get the desired numbers.

For example, if the color is #E0EAF1:

  1. Convert hex to decimal: E0 → 224, EA → 234, F1 → 241
  2. Divide by 255: 224 → 0.878, 234 → 0.918, 241 → 0.945

So to create this color, use

UIColor* clr = [UIColor colorWithRed:0.878f green:0.918f blue:0.945f alpha:1];

or let the compiler do the calculation for you:

UIColor* clr = [UIColor colorWithRed:0xE0/255.0f
                               green:0xEA/255.0f
                                blue:0xF1/255.0f alpha:1];
like image 126
kennytm Avatar answered Oct 14 '22 19:10

kennytm