How to use the cairocffi.LinearGradient function in cairocffi

To help you get started, we’ve selected a few cairocffi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Kozea / cairocffi / cairocffi / test_cairo.py View on Github external
gradient = LinearGradient(1, 2, 10, 20)
    assert gradient.get_linear_points() == (1, 2, 10, 20)
    gradient.add_color_stop_rgb(1, 1, .5, .25)
    gradient.add_color_stop_rgb(offset=.5, red=1, green=.5, blue=.25)
    gradient.add_color_stop_rgba(.5, 1, .5, .75, .25)
    assert gradient.get_color_stops() == [
        (.5, 1, .5, .25, 1),
        (.5, 1, .5, .75, .25),
        (1, 1, .5, .25, 1)]

    # Values chosen so that we can test get_data() bellow with an exact
    # byte string that (hopefully) does not depend on rounding behavior:
    # 255 / 5. == 51.0 == 0x33
    surface = ImageSurface(cairocffi.FORMAT_A8, 8, 4)
    assert surface.get_data()[:] == b'\x00' * 32
    gradient = LinearGradient(1.5, 0, 6.5, 0)
    gradient.add_color_stop_rgba(0, 0, 0, 0, 0)
    gradient.add_color_stop_rgba(1, 0, 0, 0, 1)
    context = Context(surface)
    context.set_source(gradient)
    context.paint()
    assert surface.get_data()[:] == b'\x00\x00\x33\x66\x99\xCC\xFF\xFF' * 4

    assert b'/ShadingType 2' not in pdf_with_pattern()
    assert b'/ShadingType 2' in pdf_with_pattern(gradient)
github qtile / qtile / libqtile / drawer.py View on Github external
def set_source_rgb(self, colour):
        if type(colour) == list:
            if len(colour) == 0:
                # defaults to black
                self.ctx.set_source_rgba(*utils.rgb("#000000"))
            elif len(colour) == 1:
                self.ctx.set_source_rgba(*utils.rgb(colour[0]))
            else:
                linear = cairocffi.LinearGradient(0.0, 0.0, 0.0, self.height)
                step_size = 1.0 / (len(colour) - 1)
                step = 0.0
                for c in colour:
                    rgb_col = utils.rgb(c)
                    if len(rgb_col) < 4:
                        rgb_col[3] = 1
                    linear.add_color_stop_rgba(step, *rgb_col)
                    step += step_size
                self.ctx.set_source(linear)
        else:
            self.ctx.set_source_rgba(*utils.rgb(colour))
github cubicool / cairou / tools / examples / roundbutton.py View on Github external
lw = 6.0
r  = 60.0
bc = 0.4, 0.4, 0.4

cr.set_source_rgb(*[c * 1.70 for c in bc])
cr.arc((w / 2.0) + (lw / 2.0), (h / 2.0) + (lw / 2.0), r + (lw / 2.0), 0.0, 2.0 * math.pi)
cr.fill()

cr.set_line_width(lw)
cr.arc(w / 2.0, h / 2.0, r, 0.0, 2.0 * math.pi)
cr.set_source_rgb(1.0, 1.0, 1.0)
cr.fill_preserve()

# ---
p = cairo.LinearGradient((w / 2.0) + r, (h / 2.0) + r, (w / 2.0) - r, (h / 2.0) - r)

p.add_color_stop_rgba(0.0, 0.8, 0.4, 0.0, 1.0)
p.add_color_stop_rgba(0.8, 0.8, 0.4, 0.0, 0.0)

cr.set_source(p)
cr.fill_preserve()
# ---

cr.set_source_rgb(*bc)
cr.stroke()

cr.set_line_width(lw  / 2.0)
cr.arc(w / 2.0, h / 2.0, r - (lw / 2.0), 0.0, 2.0 * math.pi)
cr.set_source_rgb(1.0, 1.0, 1.0)
cr.stroke()
github Zulko / gizeh / gizeh / gizeh.py View on Github external
def set_source(self, ctx):
        """Create a pattern and set it as source for the given context."""
        if self.type == "linear":
            (x1, y1), (x2, y2) = self.xy1, self.xy2
            pat = cairo.LinearGradient(x1, y1, x2, y2)
        elif self.type == "radial":
            (x1, y1), (x2, y2), (x3, y3) = self.xy1, self.xy2, self.xy3
            pat = cairo.RadialGradient(x1, y1, x2, y2, x3, y3)
        for stop, color in self.stops_colors:
            if len(color) == 4:
                pat.add_color_stop_rgba(stop, *color)
            else:
                pat.add_color_stop_rgb(stop, *color)
        ctx.set_source(pat)
github sde1000 / python-wayland / demo.py View on Github external
def draw_in_window(w):
    ctx = cairo.Context(w.s)
    ctx.set_source_rgba(0,0,0,0)
    ctx.set_operator(cairo.OPERATOR_SOURCE)
    ctx.paint()
    ctx.set_operator(cairo.OPERATOR_OVER)
    ctx.scale(w.width, w.height)
    pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0)
    pat.add_color_stop_rgba(1, 0.7, 0, 0, 0.5)
    pat.add_color_stop_rgba(0, 0.9, 0.7, 0.2, 1)

    ctx.rectangle(0, 0, 1, 1)
    ctx.set_source(pat)
    ctx.fill()

    del pat

    ctx.translate(0.1, 0.1)

    ctx.move_to(0, 0)
    ctx.arc(0.2, 0.1, 0.1, -math.pi/2, 0)
    ctx.line_to(0.5, 0.1)
    ctx.curve_to(0.5, 0.2, 0.5, 0.4, 0.2, 0.8)
    ctx.close_path()