Compositing

In this lesson, we'll learn about compositing in Pycairo.

When we first get introduced to Pycairo compositing, it can be a little confusing and daunting. There are 29 different compositing operators with names like Saturate, Color Dodge, Hard Light, and so on. They are defined by complicated-looking formulae, and often illustrated with a series of diagrams. These diagrams show overlapping rectangles, which give almost random-seeming effects in accordance with the specific operator that is selected.

But, what does this all mean?

In this lesson, we will look at several practical ways in which operators can be used. Therefore, we will meet several different operators. We won’t attempt to describe all 29 operators but we will meet enough of them that we may also confidently experiment with those that aren’t covered in this lesson.

Compositing terms

We will introduce some terms here, with the use of the same conventions as the main Cairo documentation at cairographics.org.

The destination is the Pycairo surface we use as our output, complete with anything that was already drawn there. In the initial examples, this surface contains a red rectangle on a transparent background.

Note: An important point to note is that compositing relies on transparency to distinguish which areas form part of the destination that was drawn. In the examples given below, we assume that we started with an ARGB32 surface (a transparent surface), which was initially filled with transparent black. In the examples where we use a red rectangle as the destination content, the shape is drawn on the transparent background.

The source is the item you are drawing. In the initial examples, it is a blue rectangle.

The result is the state of the destination after the source has been painted.

Here is an example of these terms:

Get hands-on with 1200+ tech skills courses.