Different results depending on a slight alpha value change
Consider the following code:
#include <stddef.h>
#include <cairo/cairo.h>
int main() {
cairo_surface_t *recording_surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
cairo_t *cr2 = cairo_create (recording_surface);
cairo_rectangle (cr2, 0, 0, 120, 90);
cairo_set_source_rgba (cr2, 0.7, 0, 0, 0.8);
cairo_fill (cr2);
cairo_set_operator (cr2, CAIRO_OPERATOR_XOR);
cairo_rectangle (cr2, 40, 30, 120, 90);
cairo_set_source_rgba (cr2, 0, 0, 0.9, 0.4);
cairo_fill (cr2);
cairo_destroy (cr2);
{
cairo_surface_t *image_surfce = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 160, 120);
cairo_t *cr = cairo_create (image_surfce);
cairo_set_source_rgba (cr, 0, 0, 0, 0.01);
cairo_paint (cr);
cairo_set_source_surface (cr, recording_surface, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
cairo_surface_write_to_png (image_surfce, "out1.png");
cairo_surface_destroy (image_surfce);
}
{
cairo_surface_t *image_surfce = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 160, 120);
cairo_t *cr = cairo_create (image_surfce);
cairo_set_source_rgba (cr, 0, 0, 0, 0);
cairo_paint (cr);
cairo_set_source_surface (cr, recording_surface, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
cairo_surface_write_to_png (image_surfce, "out2.png");
cairo_surface_destroy (image_surfce);
}
cairo_surface_destroy(recording_surface);
return 0;
}
The results are:
out1.png | out2.png |
---|---|
The code blocks differ only in the alpha value (0.01 or 0) of a paint with black color, but the resulting images are very different. Not doing the paint with black color at all has the same result as paint with the alpha of 0 (so the out2.png
image).
How can a paint with black color on a COLOR surface at the beginning even change something?
This behavior does not make much sense to me. I tried to emulate it in afdw/cairo@1ea06735, but looks like the rules of how this works are more complicated than I thought, and I do not understand them.