user1053078
user1053078

Reputation: 153

How to input an argument into CGContextSetRGBFillColor?

I'm trying to input the arguments for CGContextSetRGBFillColor using a data type. For example:

NSString *colorcode = ctx, 0, 1, 0, 0; 
CGContextSetRGBFillColor(colorcode);

But I get an error saying that I have too few arguments.

I want to change the arguments (ctx, 0, 1, 0, 1 ) sent to CGContextSetRGBFillColor depending on the users actions.

I want to input the argument for CGContextSetRGBFillColor using a data type because the values of it is set in a separate view controller. Or can I directly input the arguments to CGContextSetRGBFillColor and then bring it over to the other view controller to use it?

Upvotes: 12

Views: 6903

Answers (3)

Peter Hosey
Peter Hosey

Reputation: 96333

I want to input the argument for CGContextSetRGBFillColor using a data type because the values of it is set in a separate view controller.

You may be interested in the CGColor class, or, on iOS specifically, UIColor.

Or can I directly input the arguments to CGContextSetRGBFillColor …

That's the only way to input the arguments to CGContextSetRGBFillColor.

… and then bring it over to the other view controller to use it?

That doesn't make sense. Bring what over?

If you want to bring the color from one view controller to another, that's best done by creating a color object—either a CGColor or a UIColor—and passing that.

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385600

Try using a UIColor object to store the user's selected color. You can create one like this:

UIColor *color = [UIColor colorWithRed:0 green:1 blue:0 alpha:0];

Then when it's time to use it as the fill color, you can do this:

CGContextSetFillColorWithColor(ctx, color.CGColor);

I should mention that if you are not using ARC, you need to retain and release color appropriately.

Upvotes: 40

Michael Dautermann
Michael Dautermann

Reputation: 89509

Sounds like what you really need to be doing is:

CGContextSetRGBFillColor (ctx, 0.0f, 1.0f, 0.0f, 1.0f);

Where each color component is some fraction between 0.0 and 1.0.

Why are you using a NSString?

Here is the documentation on Apple's website.

Upvotes: 1

Related Questions