LinearGradient

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

What is a linear gradient?

A linear gradient is a gradient that blends smoothly from one color to another. Here is an example of a circle filled with a linear gradient:

The source is a gradient pattern. As with the solid pattern, this fills the whole user space. When this is applied to the path, the resulting output is a circle filled with the gradient. The top of the circle is red and the bottom of the circle is blue. The color gradually changes from red to blue, as we move down the circle.

This is the code to draw the gradient:

Press + to interact
import cairo
import math
# Set up surface
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 400, 400)
ctx = cairo.Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.paint()
pattern = cairo.LinearGradient(200, 100, 200, 300)
# Position 0
pattern.add_color_stop_rgb(0, 1, 0.5, 0.5)
# Position 1
pattern.add_color_stop_rgb(1, 0.5, 0.5, 1)
ctx.set_source(pattern)
ctx.arc(200, 200, 100, 0, math.pi*2)
ctx.fill()

This code is similar to the ...