[cairo] gradients

David Reveman c99drn at cs.umu.se
Thu Dec 25 18:46:30 PST 2003


I'd like to start working on proper gradient support for cairo. This is
something that needs to be done before the GL backend can do efficient
and accurate gradients.

As soon as we can agree on how gradients should be implemented and if
nobody minds I can start work on it. 

Here's a suggestion for how gradient support could be implemented in
cairo:

Add a new type of surface 'cairo_gradient_surface' which will only store
information about the gradient. For example a linear gradient should
contain width, height, start_{x,y}, stop_{x,y} and a list of gradient
stops each with color and offset. We can also have gradient surfaces for
radial gradients, function-based gradients etc.

A gradient surface can then be used as source (maybe also mask) surface
in the composite_traps and composite functions. The backend used for the
composite functions can then recognize the gradient surface and
implicitly create the the gradient source surface in whatever way it
likes. Backends that can not create the gradient on there own, will (as
they do with all surfaces that are not in the destination type) do a
clone_similar which will call the get_image function for gradient
backend. The get_image function for the gradient backend can then create
the gradient source surface as an image surface.

I think this model will fit well into the cairo library structure. One
thing that might be a bit confusing for the user is that a gradient
surface can never be set as target surface, only used with show_surface
function or as fill pattern with set_pattern function.

Here's an example that shows how it could be used:

start_x = 0;
start_y = h / 2;
stop_x = w;
stop_y = h / 2;

hgrad = cairo_linear_gradient_surface_create (w, h, start_x, start_y,
stop_x, stop_y);
cairo_gradient_surface_add_stop (hgrad, 0.0, r, g, b, a);
cairo_gradient_surface_add_stop (hgrad, 1.0, r2, g2, b2, a2);

cairo_set_pattern (cr, hgrad);
cairo_fill (cr); // fill the current path with the gradient 


So what do you think? Is there a better way to do it?

-- 
David Reveman ( c99drn at cs.umu.se )
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://lists.freedesktop.org/archives/cairo/attachments/20031226/03d6bcc2/attachment.pgp


More information about the cairo mailing list