[cairo] need help making background on custom cairo widget transparent

Carl Worth cworth at cworth.org
Sat Jul 15 07:25:01 PDT 2006


On Fri, 14 Jul 2006 18:51:34 -0400 (EDT), "Michael L. Gualtieri" wrote:
> 
> After many hours trying to get this working I broke down and decided to
> join the mailing list here :-)

Hi Michael,

I hope you're having fun with cairo so far. And I'm glad you decided
to come talk with us rather than just struggling alone.

> My plans are to have one cairo widget be the background image of the
> application (let's call it "background widget") and another widget that
> rests on top of the "background widget" (let's call it "image
> widget").

That sounds like a fine plan, and something very desirable. However, I
think GTK+ itself may need some code changes to support what you
want. You see, GTK+ is involved with the creation, drawing and
combination of widgets in ways other than the cairo-based drawing of
the widgets that you are doing yourself.

For example, GTK+ creates the cairo surface you are drawing to and
will need to create one with alpha content in order for you to get the
result you want. It likely also clears to an original background
color, (which you could still change), but also probably copies things
around, and may or may not do that in a way that respects the alpha
channel you are carefully trying to put into your widgets.

I'm not sure what the status of that might be in GTK+. Perhaps someone
here on the cairo list knows. If not, then the right place to ask is
probably the gtk-devel-list.

> The problem is that the "image widget" appears in an ugly grey box.  I
> would like the background of the image widget to show what is under it
> (namely the "background widget" or whatever else I decide to put it on),
> leaving only the image inside the "image widget" visible.  I have tried
> countless ways to make this happen but have had no luck.
> 
> I have been able to change the color of the widget, but when I use
> the rgba channel, 100% alpha just shows up as a black background.  I have
> also tried to set the GTK_NO_WINDOW flag for the "image widget", but this
> makes it disappear completely.

I'm not sure what you mean exactly by "use the rgba channel". I should
point out that while we usually think of alpha as allowing
translucence, the alpha value itself is a measure of opacity. So if
you talk about 100% alpha (or an alpha value of 1.0) then you're
talking about something entirely opaque. An alpha value of 0.0 would
be entirely transparent.

Also, the easiest ways of using alpha in cairo involve setting up a
source that has a desired alpha value in it. For example, 50%
translucent red can be achieved with:

	cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.5); /* 50% red */

When you draw with this, (with the default OVER operator), the red
source does blend nicely with the background. However, that background
won't start acquiring any translucence if it started out as opaque
already.

So shoving a non-opaque alpha value into your destination does take a
bit more work. One way is to use the SOURCE operator to directly copy
all color and alpha channels from the source to the destination.

So here's the test I would recommend you try. Do the following drawing
operation (and no other drawing) in the widget you want to appear with
translucence over another widget:

	cairo_save (cr);
	cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
	cairo_set_source_rgba (cr, 1.0, 0.0, 0.0, 0.5); /* 50% red */
	cairo_paint (cr);
	cairo_restore (cr);

That will set the entire widget to red with 50% alpha. If that doesn't
blend at all then you've identified an enhancement that you can
request in GTK+.

If that does work, then you probably just need to start your drawing
out by clearing to translucent with something like:

	cairo_save (cr);
	cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
	cairo_paint (cr);
	cairo_restore (cr);

and then doing your drawing from there.

Good luck, and have fun with cairo.

-Carl
-------------- 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/20060715/6a16e911/attachment.pgp


More information about the cairo mailing list