[cairo] composites circle

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Mon Jan 18 20:03:09 PST 2016


On Sun, 17 Jan 2016 13:25:56 +1300, I wrote:

> So really, draw each circle with an opaque colour clipped outside the
> other circle. Then fill the mixed colour, clipped to the intersection
> of the two circles.

Like this:

    #!/usr/bin/python3

    import sys
    import os
    import qahirah
    from qahirah import \
        CAIRO, \
        Colour

    pix = qahirah.ImageSurface.create \
      (
        format = CAIRO.FORMAT_RGB24,
        dimensions = (187, 143)
      )
    (qahirah.Context.create(pix)
        .set_source_colour(Colour.grey(0))
        .set_operator(CAIRO.OPERATOR_SOURCE)
        .paint()
        .arc
          (
            centre = (63, 81),
            radius = 40,
            angle1 = 0,
            angle2 = qahirah.circle,
            negative = False
          )
        .set_source_colour(Colour.from_rgba((1, 0, 0)))
        .fill()
        .arc
          (
            centre = (123, 81),
            radius = 40,
            angle1 = 0,
            angle2 = qahirah.circle,
            negative = False
          )
        .set_source_colour(Colour.grey((1)))
        .fill()
        .arc
          (
            centre = (63, 81),
            radius = 40,
            angle1 = 0,
            angle2 = qahirah.circle,
            negative = False
          )
        .clip()
        .arc
          (
            centre = (123, 81),
            radius = 40,
            angle1 = 0,
            angle2 = qahirah.circle,
            negative = False
          )
        .set_source_colour(Colour.from_rgba((1, 0.82, 0.82)))
        .fill()
    )

    pix.flush().write_to_png("%s.png" % os.path.basename(sys.argv[0]))


More information about the cairo mailing list