title: Quartz2D: How to convert a clipping rect to an inverted mask at runtime?
body: <p>Given:</p>
  
  <ol>
  <li>a CGContextRef (<code>ctx</code>) with frame <code>{0,0,100,100}</code> </li>
  <li>and a rect (<code>r</code>), with frame <code>{25,25,50,50}</code></li>
  </ol>
  
  <p>It's easy to clip the context to that rect:</p>
  
  <pre><code>CGContextClipToRect(ctx, r);
  </code></pre>
  
  <p>to mask out the red area below <em>(red == mask)</em>:</p>
  
  <p><img src="https://i.stack.imgur.com/h8rR1.png" alt="enter image description here"></p>
  
  <p>But I want to <strong>invert</strong> this clipping rect to convert it into a clipping <strong>mask</strong>. The desired outcome is to mask the red portion below <em>(red == mask)</em>:</p>
  
  <p><img src="https://i.stack.imgur.com/h0EVt.png" alt="enter image description here"></p>
  
  <p>I want to do this programmatically at runtime. </p>
  
  <p>I <strong>do not</strong> want to manually prepare a bitmap image to ship statically with my app.</p>
  
  <p>Given <code>ctx</code> and <code>r</code>, how can this be done at runtime most easily/straightforwardly?</p>