senthilmuthu
senthilmuthu

Reputation:

Can give different color in CGContextSetRGBFillColor?

How can I give the color HEXCOLOR(0xe3f3fbff) in CGContextSetRGBFillColor?

Upvotes: 0

Views: 3480

Answers (2)

Philippe Leybaert
Philippe Leybaert

Reputation: 171774

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);

Upvotes: 3

Thomas Müller
Thomas Müller

Reputation: 15625

Try

CGContextSetRGBFillColor(context, 0xe3 / 255.0, 0xf3 / 255.0, 0xfb / 255.0, 0xff / 255.0);

Upvotes: 5

Related Questions