[cairo] Questions about linear gradient results using image surface

Petr Kobalíček kobalicek.petr at gmail.com
Thu Jun 28 06:53:13 PDT 2012


Hello,

I think that this behavior is caused by the fact, that Cairo uses
[0.5, 0.5] as pixel center. The value here would be different that
expected value at [0, 0]

I think that:

gradient = cairo_pattern_create_linear(1, 0, 199, 0);

is workaround for your problem.

Best,
Petr

On Thu, Jun 28, 2012 at 11:49 AM, Dongyeon Kim <dy5.kim at samsung.com> wrote:
> 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
> --
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo


More information about the cairo mailing list