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

Carl Worth cworth at cworth.org
Wed Jun 21 10:51:46 PDT 2006


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.

-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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20060621/f7cd6e50/attachment.pgp


More information about the cairo mailing list