tags:

views:

244

answers:

2

how can i give the color HEXCOLOR(0xe3f3fbff) in CGContextSetRGBFillColor?

+1  A: 

CGContextSetRGBFillColor requires CGFloat values for R,G,B and alpha (from 0.0 to 1.0), so you'll have to convert every component of the hex color to values between 0.0 and 1.0.

In your case:

// R = 0xe3 / 0xff = 0.890
// G = 0xf3 / 0xff = 0.953
// B = 0xfb / 0xff = 0.984
// A = 0xff / 0xff = 1.000

CGContextSetRGBFillColor(context,0.89,0.953,0.984,1.0);

You can convert your hex color string to r,g,b,a values like this:

NSString  *color = @"0xe3f3fbff";

unsigned r,g,b,a;

[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(2,2)]] scanHexInt:&r];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(4,2)]] scanHexInt:&g];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(6,2)]] scanHexInt:&b];
[[NSScanner scannerWithString:[color substringWithRange:NSMakeRange(7,2)]] scanHexInt:&a];

CGContextSetRGBFillColor(context,r/255.0,g/255.0,b/255.0,a/255.0);
Philippe Leybaert
how can i convert?
how did you do,please?
+3  A: 

Try

CGContextSetRGBFillColor(context, 0xe3 / 255.0, 0xf3 / 255.0, 0xfb / 255.0, 0xff / 255.0);
Thomas Müller