Skip to content

Instantly share code, notes, and snippets.

@stuaxo
Created June 13, 2013 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuaxo/5773269 to your computer and use it in GitHub Desktop.
Save stuaxo/5773269 to your computer and use it in GitHub Desktop.
import cairocffi as cairo
s = cairo.ImageSurface(cairo.constants.FORMAT_ARGB32, 600, 400)
img = cairo.ImageSurface.create_from_png("/usr/share/icons/gnome/48x48/devices/joystick.png")
w, h = img.get_width(), img.get_height()
cr = cairo.Context(s)
def before():
cr.save()
cr.rectangle(0, 0, w, h)
cr.clip()
cr.set_source_surface(img)
cr.paint()
def after():
cr.restore()
cr.translate(w, 0)
def try_operator(op, alpha):
before()
cr.set_operator(op)
cr.set_source_rgba(1, 1, 1, alpha)
cr.paint()
after()
def try_alpha(alpha):
cr.save()
before()
after()
try_operator(cairo.constants.OPERATOR_ATOP, alpha)
try_operator(cairo.constants.OPERATOR_COLOR_DODGE, alpha)
try_operator(cairo.constants.OPERATOR_SOFT_LIGHT, alpha)
try_operator(cairo.constants.OPERATOR_HARD_LIGHT, alpha)
try_operator(cairo.constants.OPERATOR_HSL_HUE, alpha)
try_operator(cairo.constants.OPERATOR_HSL_SATURATION, alpha)
try_operator(cairo.constants.OPERATOR_HSL_COLOR, alpha)
try_operator(cairo.constants.OPERATOR_HSL_LUMINOSITY, alpha)
cr.restore()
cr.translate(0, h)
try_alpha(1)
try_alpha(0.75)
try_alpha(0.5)
try_alpha(0.25)
try_alpha(0)
s.write_to_png("/tmp/t.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment