Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAIRO_OPERATOR_CLEAR not working as expected

Tags:

c++

cairo

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?

like image 946
muffel Avatar asked Nov 23 '25 08:11

muffel


1 Answers

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.

like image 89
Uli Schlachter Avatar answered Nov 24 '25 22:11

Uli Schlachter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!