1

I have a bunch of PDFs that I'm using as vector art in an app I'm writing. I'm using the CG Context command CGContextDrawPDFPage to draw the PDFs, and it's working great so far. A problem I've run into is that I would like to be able to modify the color of the vector art at runtime. All of the PDFs currently draw as solid black, as they were designed.

Is it possible to draw a PDF but somehow change what color it gets drawn with? Maybe modify the color palette so that black maps to something else, or actually change the PDF itself at runtime? I realize that PDFs contain color information already in the file, so maybe it would be possible to tell Quartz to ignore those colors and use something else?

More info: The PDFs I'm drawing are various icons / shapes that the program needs to draw. They are some black shape, with a transparent background. I need to be able to draw that PDF but using a different color, or do as Bill Wilson mentioned and overlay a different color using Blend Modes. I tried kCGBlendModeLighten but the problem is that if I fill a rect around the pdf, the outside transparent areas become red, so there is just a red box instead of a red shape.

So I need a way to map black to color X but leave transparent as is.

Here is an example of a PDF that I need to draw in different colors: http://davidevansgames.com/res/FON179.pdf

AstroCB
  • 12,337
  • 20
  • 57
  • 73
phosphoer
  • 433
  • 6
  • 16

1 Answers1

1

After you draw the PDF, set the blend mode of the CGConext to kCGBlendModeLighten and fill the context with the color you want, this should give you something to start with.

If you are doing any other drawing to the context ensure you set the blend mode to normal. Or save and restore context state with CGContextSaveGState and CGContextRestoreGState.

Experiment with the othe blend modes and see what works best for you...

I am not sure if this is what you want, so if it isn't can you elaborate, perhaps share a pdf that you are using.

 CGContextSaveGState(context);
 CGContextSetBlendMode(context, kCGBlendModeLighten);
 CGColorRef redColor = [UIColor colorWithRed:1.0 green:0.0 
                                         blue:0.0 alpha:1.0].CGColor;

 CGContextSetFillColorWithColor(context, redColor);
 CGContextFillRect(context, CGRectMake(0, 0, width, height));
 CGContextRestoreGState(context)
Bill Wilson
  • 954
  • 6
  • 9
  • Interesting suggestion! I tried this but unfortunately it just produced a red box around the pdf. I'm looking at different blend modes to find one that will only color the pdf, but haven't found one that works yet. – phosphoer Jul 10 '12 at 04:31
  • Hmmm... This may sound strange but fill the context with white, then draw the b&w PDF, then do the above steps . Do you have a sample PDF you could share? Oops. See you updated your Q with a PDF. I will try it in the am. – Bill Wilson Jul 10 '12 at 06:16
  • The background of the view I just realized is actually transparent, which is a requirement. So what I need is to map black to X color but leave transparent as is. – phosphoer Jul 10 '12 at 06:51