How to use the cairocffi.FORMAT_ARGB32 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 cubicool / cairou / tests / python-cffi / basic-tests.py View on Github external
# this file with the python interpreter of your choice. :)

import os

os.environ["LD_LIBRARY_PATH"] = "..;../..;../../build"

import sys

sys.path.extend((".", "..", "../build", "../src"))

import os
import cairocffi as cairo
import cairocks
import math

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 400)
cr      = cairo.Context(surface)

cr.set_source_rgb(1.0, 1.0, 1.0)
cr.paint()
cr.set_source_rgb(1.0, 0.5, 0.0)

TESTS = []

def test_function(func):
	global TESTS

	def test_function_decorator(*args, **kargs):
		cr.save()

		func(*args, **kargs)
github Kozea / cairocffi / cairocffi / test_cairo.py View on Github external
def test_glyphs():
    surface = ImageSurface(cairocffi.FORMAT_ARGB32, 100, 20)
    context = Context(surface)
    font = context.get_scaled_font()
    text = 'Étt'
    glyphs, clusters, is_backwards = font.text_to_glyphs(
        5, 15, text, with_clusters=True)
    assert font.text_to_glyphs(5, 15, text, with_clusters=False) == glyphs
    (idx1, x1, y1), (idx2, x2, y2), (idx3, x3, y3) = glyphs
    assert idx1 != idx2 == idx3
    assert y1 == y2 == y3 == 15
    assert 5 == x1 < x2 < x3
    assert clusters == [(2, 1), (1, 1), (1, 1)]
    assert is_backwards == 0
    assert round_tuple(font.glyph_extents(glyphs)) == (
        round_tuple(font.text_extents(text)))
    assert round_tuple(font.glyph_extents(glyphs)) == (
        round_tuple(context.glyph_extents(glyphs)))
github joemfox / colorschemer / dress.py View on Github external
adjective2 = adjectives[random.randint(0,len(adjectives)-1)]
adjective3 = adjectives[random.randint(0,len(adjectives)-1)]
color1 = colors[random.randint(0,len(colors)-1)].split(",")
color2 = colors[random.randint(0,len(colors)-1)].split(",")
color3 = colors[random.randint(0,len(colors)-1)].split(",")
string1 = adjective1 + " " + color1[0]
string2 = adjective2 + " " + color2[0]
string3 = adjective3 + " " + color3[0]

rgbstr1 = struct.unpack("BBB",color1[1][1:].decode("hex")) + (1,)
rgbstr2 = struct.unpack("BBB",color2[1][1:].decode("hex")) + (1,)
rgbstr3 = struct.unpack("BBB",color3[1][1:].decode("hex")) + (1,)

WIDTH,HEIGHT = 512,512

schemepng = cairo.ImageSurface(cairo.FORMAT_ARGB32,WIDTH,HEIGHT)
ctx = cairo.Context(schemepng)


portion = random.randint(3,6)
rotate = random.randint(0,3)

ctx.rectangle(0,0,WIDTH/portion,HEIGHT)
ctx.set_source_rgba(rgbstr1[0]/255.0,rgbstr1[1]/255.0,rgbstr1[2]/255.0,rgbstr1[3])
ctx.fill()

ctx.rectangle(WIDTH/portion,0,WIDTH,HEIGHT)
ctx.set_source_rgba(rgbstr2[0]/255.0,rgbstr2[1]/255.0,rgbstr2[2]/255.0,rgbstr2[3])
ctx.fill()

ctx.rectangle(WIDTH/(portion-1),0,WIDTH,HEIGHT)
ctx.set_source_rgba(rgbstr3[0]/255.0,rgbstr3[1]/255.0,rgbstr3[2]/255.0,rgbstr3[3])
github neozhaoliang / pywonderland / src / aztec / random_tiling.py View on Github external
def render_with_cairo(az, imgsize, extent, filename):
    """
    Draw current tiling of `az` (possibly have holes)
    to a png image with cairo.

    Parameters
    ----------
    :az:  an instance of the AztecDiamond class.
    :imgsize:  image size in pixels, e.g. size = 600 means 600x600.
    :extent:  range of the axis: [-extent, extent] x [-extent, extent]
    :filename:  output filename, must be a .png image.
    """
    import cairocffi as cairo
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, imgsize, imgsize)
    surface.set_fallback_resolution(100, 100)
    ctx = cairo.Context(surface)
    ctx.scale(imgsize / (2.0 * extent), -imgsize / (2.0 * extent))
    ctx.translate(extent, -extent)

    ctx.set_source_rgb(1, 1, 1)
    ctx.paint()

    margin = 0.1

    for (i, j) in az.cells:
        if (az.is_black(i, j) and az.tile[(i, j)] is not None):
            if az.tile[(i, j)] == 'n':
                ctx.rectangle(i - 1 + margin, j + margin,
                              2 - 2 * margin, 1 - 2 * margin)
                ctx.set_source_rgb(*N_COLOR)
github audreyr / design / design / clouds.py View on Github external
def draw_cloud(width=140, height=60, color=rgb(255, 255, 255)):
    """ Draw a cloud with the given width, height, and color. """

    cairo_color = color / rgb(255, 255, 255)

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
    ctx = cairo.Context(surface)

    # A cloud consists of 4 circles
    draw_circle(ctx, width / 3, height / 2, height / 3, cairo_color)
    draw_circle(ctx, 2 * width / 3, height / 2, height / 3, cairo_color)
    draw_circle(ctx, width / 2, height / 3, height / 3, cairo_color)
    draw_circle(ctx, width / 2, 2 * height / 3, height / 3, cairo_color)

    surface.write_to_png('cloud.png')
github fogleman / AdventOfCode2018 / extra / 17-cairo.py View on Github external
def render(self, t):
        scale = 1
        tile_size = 12
        still = set(k for k, v in self.still_t.items() if v <= t)
        flowing = set(k for k, v in self.flowing_t.items() if v <= t)
        wet = still | flowing
        maxy = max(y for x, y in wet)
        y0 = maxy - 56
        y1 = y0 + 112
        p = 0
        # x0, y0, x1, y1 = self.x0-p, self.y0-p, self.x1+p, self.y1+p
        x0, x1 = self.x0 - p, self.x1 + p
        w = int((x1 - x0 + 1) * tile_size * scale)
        h = int((y1 - y0 + 1) * tile_size * scale)
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
        dc = cairo.Context(surface)
        dc.scale(scale)
        
        for y in range(y0, y1 + 1):
            for x in range(x0, x1 + 1):
                px = (x - x0) * tile_size
                py = (y - y0) * tile_size
                dc.set_source_surface(stone, px, py)
                dc.paint()
                tile = None
                if (x, y) in self.clay:
                    tile = dirt
                    if (x, y - 1) not in self.clay:
                        tile = grass
                if (x, y) in wet:
                    if (x, y - 1) not in wet:
github limbo018 / DREAMPlace / dreamplace / ops / draw_place / PlaceDrawer.py View on Github external
num_bins_x = int(math.ceil((xh-xl)/bin_size_x))
        num_bins_y = int(math.ceil((yh-yl)/bin_size_y))
        x = np.array(pos[:num_nodes])
        y = np.array(pos[num_nodes:])
        node_size_x = np.array(node_size_x)
        node_size_y = np.array(node_size_y)
        pin_offset_x = np.array(pin_offset_x)
        pin_offset_y = np.array(pin_offset_y)
        pin2node_map = np.array(pin2node_map)
        try: 
            tt = time.time()
            width = 800
            height = 800
            line_width = 0.1
            padding = 0 
            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
            ctx = cairo.Context(surface)
            # Do not use scale function. 
            # This is not compatible with show_text

            if num_movable_nodes < num_physical_nodes: 
                layout_xl = min(np.amin(x[num_movable_nodes:num_physical_nodes]), xl)
                layout_yl = min(np.amin(y[num_movable_nodes:num_physical_nodes]), yl)
                layout_xh = max(np.amax(x[num_movable_nodes:num_physical_nodes]+node_size_x[num_movable_nodes:num_physical_nodes]), xh)
                layout_yh = max(np.amax(y[num_movable_nodes:num_physical_nodes]+node_size_y[num_movable_nodes:num_physical_nodes]), yh)
            else:
                layout_xl = xl 
                layout_yl = yl 
                layout_xh = xh 
                layout_yh = yh 

            def bin_xl(id_x):
github SonOfLilit / wirematrix / wirematrix.py View on Github external
def __init__(self, w, h):
        self.w, self.h = w, h
        self.data = ctypes.create_string_buffer(self.h * self.w * 4)
        self.surface = cairo.ImageSurface.create_for_data(
            self.data, cairo.FORMAT_ARGB32,self.w, self.h)

        self.messages = {}
github ealgis / ealgis / django / ealgis / colour_scale.py View on Github external
def legend(self, height=400, width=160):
        img = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
        cr = cairo.Context(img)

        # padding
        PAD_Y = 10
        ATTACH_W = 3

        legend_from = PAD_Y
        legend_to = height - PAD_Y
        legend_height = legend_to - legend_from
        legend_width = width / 3
        cr.set_line_width(1)
        # draw box around the colour scale
        cr.move_to(0, PAD_Y + 0.5)  # top
        cr.line_to(legend_width + 1, PAD_Y + 0.5)
        cr.move_to(legend_width + 1.5, PAD_Y)  # right side
        cr.line_to(legend_width + 1.5, PAD_Y + 1 + legend_height)
github audreyr / design / design / gradients.py View on Github external
def vertical_strip(width=10, height=100, color=rgb(100, 100, 100),
                   subtlety=0.1):
    """
    Draws a subtle vertical gradient strip.
    """

    cairo_color = color / rgb(255, 255, 255)

    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
    ctx = cairo.Context(surface)

    ctx.scale(width / 1.0, height / 1.0)

    pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0)
    pat.add_color_stop_rgba(
        0,
        cairo_color.red,
        cairo_color.green,
        cairo_color.blue,
        0
    )
    pat.add_color_stop_rgba(
        1,
        cairo_color.red,
        cairo_color.green,