[cairo] Linear Gradients

Luigi Castelli superbigio at yahoo.com
Fri Apr 10 09:44:29 PDT 2009


Hello there,

I have a square path filled with a horizontal linear gradient.
The gradient has the option to be rotated any number of degrees.

The code to produce this is:

//-----------------------------------

cairo_pattern_t *pattern;
cairo_matrix_t matrix;
		
pattern = cairo_pattern_create_linear( 0., 0., rect.width, 0. );
		
cairo_pattern_add_color_stop_rgba( pattern, 0., r0, g0, b0, a0 );
cairo_pattern_add_color_stop_rgba( pattern, 1., r1, g1, b1, a1 );
		
cairo_pattern_get_matrix( pattern, &matrix );
		
cairo_matrix_translate( &matrix, -rect.width * 0.5, -rect.height * 0.5 );
cairo_matrix_rotate( &matrix, angle * ( PI / 180. ) );
cairo_matrix_translate( &matrix, rect.width * 0.5, rect.height * 0.5 );
		
cairo_rectangle( context, 0., 0., rect.width, rect.height );
		
cairo_pattern_set_matrix( pattern, &matrix );
cairo_set_source( context, pattern );
cairo_fill( context );
		
cairo_pattern_destroy( pattern );

//-----------------------------------

The problem arises if I change the dimension of the rectangle when the gradient is rotated.
If I have some rotation applied to the gradient and the width and height of the rectangle are no longer equal, then the start and end colors of the gradient seem to end up out of the visible area. (i.e. my gradient gets deformed)

Can someone give me a little help here ?

Thanks so much.

- Luigi




      


More information about the cairo mailing list