Created
June 13, 2013 12:23
-
-
Save stuaxo/5773269 to your computer and use it in GitHub Desktop.
Different cairo operator examples - ported to python from http://stackoverflow.com/questions/9955964/creating-a-highlight-effect-with-the-cairo-library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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