[cairo] Questions about linear gradient results using image surface

Dongyeon Kim dy5.kim at samsung.com
Thu Jun 28 01:49:01 PDT 2012


Hello all,

I have some questions about linear gradient results using image surface.
Below is a sample code to draw a rectangle with linear gradient pattern fill.
(You can also see the code here: http://codepad.org/kPzFYk72)

///////////////// TEST CODE BEGINS /////////////////
cairo_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 200, 50);
cr = cairo_create(cairo_surface);

cairo_pattern_t *gradient;
gradient = cairo_pattern_create_linear(0, 0, 200, 0);
cairo_pattern_add_color_stop_rgb(gradient, 0.0, 1.0, 0.0, 0.0);
cairo_pattern_add_color_stop_rgb(gradient, 0.25, 0.0, 1.0, 0.0);
cairo_pattern_add_color_stop_rgb(gradient, 0.75, 0.0, 1.0, 0.0);
cairo_pattern_add_color_stop_rgb(gradient, 1.0, 1.0, 0.0, 0.0);

cairo_set_source(cr, gradient);
cairo_rectangle (cr, 0, 0, 200, 50);
cairo_fill(cr);
cairo_surface_write_to_png(cairo_surface, "result_linear_gradient.png");
///////////////// TEST CODE ENDS /////////////////

It is a simple code to draw a gradient with four stops.
Since I have put color stops at offset 0 and 1 to red, I expected the result surface to have value RGBA(255, 0, 0, 255) at the leftmost and rightmost pixels, but the result image shows that the leftmost edge has a pixel value of RGBA(253, 1, 0, 255), and the rightmost edge a value of RGBA(252, 2, 0, 255).

Is this normal behavior in Cairo? What should I do to get exact color values at both ends?

Thanks,
Dongyeon Kim


More information about the cairo mailing list