[cairo] Cairo, Glitz, etc. again...

Jeremy L. Moles jeremy at emperorlinux.com
Wed Jun 21 12:03:52 PDT 2006


On Wed, 2006-06-21 at 10:51 -0700, Carl Worth wrote:
> On Wed, 21 Jun 2006 12:58:15 -0400, "Jeremy L. Moles" wrote:
> >
> > 	// Fill the surface with transparency...
> > 	// This acts as a kind of "clearing" mechanism, right???
> >         cairo_set_source_rgba(cr, 0.0f, 0.0f, 0.0f, 0.0f);
> >         cairo_rectangle(cr, 0.0f, 0.0f, 1.0f, 1.0f);
> >         cairo_fill(cr);
> 
> Close, but not quite right. With the default operator
> (CAIRO_OPERATOR_OVER) you are blending the source "over" (surprising,
> I know) the destination. This means that transparent portions of the
> source, (which in your case is everything), have no effect on the
> destination.
> 
> So the above does not work to clear to transparent.
> 
> There are a couple of things you could use instead:
> 
> 	/* Clear to transparent. */
> 	cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
> 	cairo_rectangle (cr, 0.0, 0.0, 1.0, 1.0);
> 	cairo_fill (cr);
> or:
> 	/* Clear to an explicit color/alpha
> 	 * (pure transparent in this case). */
> 	cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.0);
> 	cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
> 	cairo_rectangle (cr, 0.0, 0.0, 1.0, 1.0);
> 	cairo_fill (cr);
> 
> In the first case, I'm using the CLEAR operator which is defined to
> set the destination to 0.0 in every channel. In this case, the
> contents of the source pattern are not relevant.
> 
> In the second case, I'm using the SOURCE operator which sets the
> destination equal to the source. With an all 0.0 source, as I use
> here, the result is the same as using CLEAR. But this approach code
> also be used to set the destination to something like 50% transparent
> red, (or any other translucent color).
> 
> In either case, you probably want to got back to the default OVER
> operator when you're done, so it's likely easiest to wrap the above in
> a cairo_save(cr)/cairo_restore(cr) pair.
> 
> I hope that helps.

Yes, it absolutely does, and is working absolutely wonderfully now. :)
Many, many, thanks, to both you and Reveman! I'll try and put up a more
robust piece of code before the end of the day, maybe something to put
alongside the SDL example?

> -Carl
> 
> PS. Yes, someday we'll have a good tutorial that explains the cairo
> rendering model and goes through simple cases like "erasing to
> transparent" like this. If someone wanted to start cooking something
> up (in AsciiDoc perhaps?) that would be just dandy.



More information about the cairo mailing list