Question

I want to remove parts of a previously filled shape with Cairo and C++.

Consider the following MWE:

void test(cairo_t *cr){
    cairo_set_source_rgb(cr, 1, 1, 1);
    cairo_paint(cr); //background

    cairo_rectangle(cr, 50, 50, 150, 150);
    cairo_set_source_rgb(cr, 0, 0, 1);
    cairo_fill(cr); //first rect
    cairo_set_operator(cr,CAIRO_OPERATOR_CLEAR);
    cairo_arc(cr, 100, 100, 100, 0, M_PI * 2);
    cairo_fill(cr); //circle that show remove a part of the rect
}

It results in the following picture:

enter image description here

According to the documentation I would have expected no black color at all, and all parts of the blue rectangle, that are under the circle to become removed (and therefore white as the background).

Did I misunderstand the operator? Did I make any mistake?

Was it helpful?

Solution

How would cairo know what you consider the background?

The documentation that you link to mentions that the alpha channels and all color channels are set to 0. This is fully transparent black.

The example in the documentation is an image with an alpha channel and thus the cleared parts become transparent.

You are using an image without an alpha channel and thus the cleared parts become black.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top