本文整理汇总了Python中cairo.Context.set_source_rgba方法的典型用法代码示例。如果您正苦于以下问题:Python Context.set_source_rgba方法的具体用法?Python Context.set_source_rgba怎么用?Python Context.set_source_rgba使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairo.Context
的用法示例。
在下文中一共展示了Context.set_source_rgba方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: glyphs
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def glyphs(*args, **kwargs):
options = Options(infname='data/vorttest.txt', saveas='output/part1/',
outfname='vorttest.png', scale=100., seed_num=20, stepsize=.01,
steps=5, directions=1, norm=False, line_width=.5)
options.update(kwargs)
print >> log, options.infname
if not path.exists(options.saveas): os.makedirs(options.saveas)
(xmin, ymin), (xmax, ymax), uv = read2vecs(options.infname)
width, height = xmax - xmin, ymax - ymin
def index2world(points, xmin=xmin, ymin=ymin, scale=options.scale,
debug=False):
if debug:
print "index2world:",
print xscale, yscale, points.dtype
if debug: print points,
points[:,0] -= xmin
if debug: print points,
points[:,1] -= ymin
if debug: print points,
points *= scale
if debug: print points
if 'seed' in options:
seed = options.seed
else:
seed = product(np.linspace(xmin, xmax, num=options.seed_num),
np.linspace(ymin, ymax, num=options.seed_num))
ctx = Context(ImageSurface(cairo.FORMAT_ARGB32, int(options.scale * width),
int(options.scale * height)))
ctx.set_source_rgba(0,0,0)
ctx.set_line_width(options.line_width)
for s in seed:
points = sline(uv, (xmin, ymin), (xmax, ymax), np.array(s),
options.stepsize, options.steps, options.norm, options.directions)
print >> log, points
index2world(points)
print >> log, points
draw_arrow(ctx, points, arrowhead_size=2)
with open(path.join(options.saveas, options.outfname), 'w') as outf:
ctx.get_target().write_to_png(outf)
示例2: _highlight_border
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def _highlight_border(self, context: cairo.Context, row: int, col: int):
width = self._total_width
height = self._total_height
line_width = 3
row_rectangles = [
Rectangle(
Point(1, row * self.cell_height - line_width / 2),
width - 2,
line_width
),
Rectangle(
Point(1, (row + 1) * self.cell_height - line_width / 2),
width - 2,
line_width
)
]
col_rectangles = [
Rectangle(
Point(col * self.cell_width - line_width / 2, 1),
line_width,
height - 2
),
Rectangle(
Point((col + 1) * self.cell_width - line_width / 2, 1),
line_width,
height - 2
)
]
context.save()
r, g, b = self.highlight_color
context.set_source_rgba(r, g, b, .6)
for row_rectangle in row_rectangles:
context.rectangle(row_rectangle.start.x,
row_rectangle.start.y,
row_rectangle.width,
row_rectangle.height)
context.fill()
for col_rectangle in col_rectangles:
context.rectangle(col_rectangle.start.x,
col_rectangle.start.y,
col_rectangle.width,
col_rectangle.height)
context.fill()
context.restore()
示例3: main
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def main(args, **argv):
from dddUtils.ioOBJ import load_2d as load
from cairo import SVGSurface, Context
from numpy import array
from glob import glob
prefix = args.prefix
size = args.size
scale = args.scale
one = 1.0/size
steps = args.steps
stride = args.stride
skip = args.skip
out = prefix + '.svg'
print('making file: {:s}'.format(out))
s = SVGSurface(out, size, size)
c = Context(s)
c.set_line_width(0.1)
c.set_source_rgba(*BLACK)
for fn in sorted(glob(prefix + '*.2obj'))[skip:steps:stride]:
print(fn)
data = load(fn)
vertices = data['vertices']
vertices *= scale*size
edges = data['edges']
# make_random_line(c, vertices, edges)
make_line(c, vertices, edges)
c.save()
return
示例4: main
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def main(args, **argv):
# from render.render import Render
from dddUtils.ioOBJ import load_2d as load
from cairo import SVGSurface, Context
from numpy import array
size = args.size
scale = args.scale
one = 1.0/size
data = load(args.fn)
print(data)
vertices = data['vertices']
faces = data['faces']
edges = data['edges']
out = '.'.join(args.fn.split('.')[:-1])+'.svg'
print('making file: {:s}'.format(out))
s = SVGSurface(out, size, size)
c = Context(s)
c.set_line_width(0.1)
c.set_source_rgba(*BLACK)
vertices -= get_mid(vertices)
vertices *= scale
vertices += array([[0.5,0.5]])
vertices *= size
make_triangles(c, vertices, faces, edges)
# make_random_length_strips(c, vertices, faces, edges)
c.save()
return
示例5: renderText
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def renderText(self, surface,
text, y_offset, size,
shade, w=(2,3), wrap=True):
if len(text) < 1:
return
def setdesc(l, size):
l.set_font_description(
FontDescription(
self.font + " " + str(size)))
origin = Context(surface)
origin.translate(self.w * (w[1] - w[0]) /
(w[1] * 2), y_offset)
box = CairoContext(origin)
# Start laying out text
layout = box.create_layout()
setdesc(layout, size)
width = self.w * w[0] / w[1]
if wrap:
layout.set_width(width * pango.SCALE)
else:
layout.set_width(-1)
layout.set_alignment(pango.ALIGN_CENTER)
layout.set_text(text)
# Resize text to make sure it doesn't exceed width.
wi, n = layout.get_pixel_size()
if wi > width:
s = size * width / wi
setdesc(layout, s)
layout.set_width(width * pango.SCALE)
# Draw a transparent pane behind the text
origin.set_source_rgba(1, 1, 1, 0.7)
origin.rectangle(*layout.get_pixel_extents()[1])
origin.fill()
# Draw text
origin.set_source_rgb(shade, shade, shade)
box.update_layout(layout)
box.show_layout(layout)
示例6: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
start_x, start_y = context.get_current_point()
context.set_font_size(self.font_size)
context.move_to(0, 0)
if not self.is_shape_set:
self.set_shape_from_context(context)
shape = self.shape
start = shape.start
padding = self.padding
h = shape.height - 2 * padding
label = self.label
context.set_source_rgb(*self.background_color)
shape.draw_on_context(context)
context.fill_preserve()
context.set_line_width(1)
context.set_source_rgb(*self.label_color)
context.stroke()
if self.disabled:
context.set_source_rgba(1, 1, 1, .7)
context.move_to(start.x + padding-1,
start.y + padding + h+1)
context.show_text(label)
context.set_source_rgba(*self.label_color, .9)
else:
context.set_source_rgb(*self.label_color)
context.move_to(start.x + padding,
start.y + padding + h)
context.show_text(label)
if self.disabled:
context.set_source_rgba(1, 1, 1, .7)
context.move_to(0, 0)
shape.draw_on_context(context)
context.fill()
示例7: ImageSurface
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
pen.x += face.glyph.advance.x
pen.y += face.glyph.advance.y
L.flush()
return L
if __name__ == '__main__':
from PIL import Image
n_words = 200
H, W, dpi = 600, 800, 72.0
I = ImageSurface(FORMAT_A8, W, H)
ctxI = Context(I)
ctxI.rectangle(0,0,800,600)
ctxI.set_source_rgba (0.9, 0.9, 0.9, 0)
ctxI.fill()
S = random.normal(0,1,n_words)
S = (S-S.min())/(S.max()-S.min())
S = sort(1-sqrt(S))[::-1]
sizes = (12 + S*48).astype(int).tolist()
def spiral():
eccentricity = 1.5
radius = 8
step = 0.1
t = 0
while True:
t += step
yield eccentricity*radius*t*cos(t), radius*t*sin(t)
示例8: range
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
verts.append(segment[1])
codes.append(CURVE3)
for i in range(1,len(segment)-2):
A,B = segment[i], segment[i+1]
C = ((A[0]+B[0])/2.0, (A[1]+B[1])/2.0)
verts.extend([ C, B ])
codes.extend([ CURVE3, CURVE3])
verts.append(segment[-1])
codes.append(CURVE3)
[tags.pop() for x in range(len(segment) - 1)]
VERTS.extend(verts)
CODES.extend(codes)
start = end+1
ctx.new_path()
ctx.set_source_rgba(1,1,0, 0.5)
i = 0
while (i < len(CODES)):
if (CODES[i] == MOVETO):
ctx.move_to(VERTS[i][0],VERTS[i][1])
i += 1
elif (CODES[i] == LINETO):
ctx.line_to(VERTS[i][0],VERTS[i][1])
i += 1
elif (CODES[i] == CURVE3):
ctx.curve_to(VERTS[i][0],VERTS[i][1],
VERTS[i+1][0],VERTS[i+1][1], # undocumented
VERTS[i+1][0],VERTS[i+1][1])
i += 2
elif (CODES[i] == CURVE4):
ctx.curve_to(VERTS[i][0],VERTS[i][1],
示例9: do_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
def do_draw(self, context: cairo.Context) -> bool:
if not self.adjustment or self.adjustment.get_upper() <= 0:
return False
height = self.get_allocated_height()
width = self.get_allocated_width()
if width <= 0 or height <= 0:
return False
base_bg, base_outline, handle_overdraw, handle_outline = (
self.get_map_base_colors())
x0 = self.overdraw_padding + 0.5
x1 = width - 2 * x0
height_scale = height * self.get_height_scale()
if self._cached_map is None:
surface = cairo.Surface.create_similar(
context.get_target(), cairo.CONTENT_COLOR_ALPHA, width, height)
cache_ctx = cairo.Context(surface)
cache_ctx.set_line_width(1)
cache_ctx.rectangle(x0, -0.5, x1, height_scale + 0.5)
cache_ctx.set_source_rgba(*base_bg)
cache_ctx.fill()
# We get drawing coordinates by tag to minimise our source
# colour setting, and make this loop slightly cleaner.
tagged_diffs = self.chunk_coords_by_tag()
for tag, diffs in tagged_diffs.items():
cache_ctx.set_source_rgba(*self.fill_colors[tag])
for y0, y1 in diffs:
y0 = round(y0 * height_scale) + 0.5
y1 = round(y1 * height_scale) - 0.5
cache_ctx.rectangle(x0, y0, x1, y1 - y0)
cache_ctx.fill_preserve()
cache_ctx.set_source_rgba(*self.line_colors[tag])
cache_ctx.stroke()
cache_ctx.rectangle(x0, -0.5, x1, height_scale + 0.5)
cache_ctx.set_source_rgba(*base_outline)
cache_ctx.stroke()
self._cached_map = surface
context.set_source_surface(self._cached_map, 0, 0)
context.paint()
# Draw our scroll position indicator
context.set_line_width(1)
context.set_source_rgba(*handle_overdraw)
adj_y = self.adjustment.get_value() / self.adjustment.get_upper()
adj_h = self.adjustment.get_page_size() / self.adjustment.get_upper()
context.rectangle(
x0 - self.overdraw_padding, round(height_scale * adj_y) + 0.5,
x1 + 2 * self.overdraw_padding, round(height_scale * adj_h) - 1,
)
context.fill_preserve()
context.set_source_rgba(*handle_outline)
context.stroke()
return True
示例10: range
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
for i in range(1,len(segment)-2):
A,B = segment[i], segment[i+1]
C = ((A[0]+B[0])/2.0, (A[1]+B[1])/2.0)
verts.extend([ C, B ])
codes.extend([ CURVE3, CURVE3])
verts.append(segment[-1])
codes.append(CURVE3)
[tags.pop() for x in range(len(segment) - 1)]
VERTS.extend(verts)
CODES.extend(codes)
start = end+1
# Draw glyph
ctx.new_path()
ctx.set_source_rgba(0.8,0.5,0.8, 1)
i = 0
while (i < len(CODES)):
if (CODES[i] == MOVETO):
ctx.move_to(VERTS[i][0],VERTS[i][1])
i += 1
elif (CODES[i] == LINETO):
ctx.line_to(VERTS[i][0],VERTS[i][1])
i += 1
elif (CODES[i] == CURVE3):
ctx.curve_to(VERTS[i][0],VERTS[i][1],
VERTS[i+1][0],VERTS[i+1][1], # undocumented
VERTS[i+1][0],VERTS[i+1][1])
i += 2
elif (CODES[i] == CURVE4):
ctx.curve_to(VERTS[i][0],VERTS[i][1],
示例11: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
#.........这里部分代码省略.........
if not over:
up_to += 1
while base > 0 and not over:
expected_height += table_extents[base-1].total_height
over = expected_height >= self._max_height
if not over:
base -= 1
how_many = up_to - base
skip = base
self.base = base
entries = self.entries[skip:skip + how_many]
def table_extents_iterator():
return table_extents.iter_over(
skip, how_many
)
start_x, start_y = context.get_current_point()
start_y += title_extents.total_height
h = title_extents.total_height
self.title_height = h
for (index, cell), data in zip(enumerate(title_extents), self.title):
context.save()
offset = title_extents.get_cell_data_left(index)
context.rectangle(start_x + offset, start_y - h, cell.width, 2*h)
context.clip()
context.move_to(
start_x + offset,
start_y
)
context.show_text(data)
context.restore()
# start_y += self.margin
curr_x, curr_y = start_x, start_y# + title_extents.total_height
for line_index, (line_extent, entry) in enumerate(zip(table_extents_iterator(), entries)):
h = line_extent.total_height
curr_y += h
if curr_y + self.margin >= self._max_height:
break
for (cell_index, cell), data in zip(enumerate(line_extent), entry):
context.save()
offset = line_extent.get_cell_data_left(cell_index)
context.rectangle(curr_x + offset, curr_y - h, cell.width, 2*h)
context.clip()
context.move_to(
curr_x + offset,
curr_y
)
context.show_text(data)
context.restore()
curr_x = start_x
end_x = table_extents.entries_width
curr_y = start_y + self.margin
end_y = table_extents.get_height_up_to(skip, how_many) + start_y + self.margin + 1
self.table_height = end_y
self.table_width = end_x
for line in table_extents_iterator():
context.move_to(curr_x, curr_y)
context.line_to(end_x, curr_y)
context.stroke()
curr_y += line.total_height
context.move_to(curr_x, curr_y)
context.line_to(end_x, curr_y)
context.stroke()
curr_x = start_x
curr_y = start_y - 1 + self.margin
if self._how_many > 0:
line = table_extents[0]
for cell in line:
context.move_to(curr_x, curr_y)
context.line_to(curr_x, end_y)
context.stroke()
curr_x += cell.total_width + 2 * self.margin
context.move_to(curr_x, curr_y)
context.line_to(curr_x, end_y)
context.stroke()
if self._to_highlight is not None:
r, g, b = global_constants.highlight
context.set_source_rgba(r, g, b, .6)
index = self._to_highlight
base = start_y + table_extents.get_height_up_to(skip, index) + self.margin
h = table_extents.get_height_of(skip, how_many, index)
context.rectangle(start_x, base, table_extents.entries_width, h)
context.fill()
context.restore()
self._shown = how_many
示例12: _paint_panel
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgba [as 别名]
#.........这里部分代码省略.........
is_first = True
for row in chart_data[ind]:
x = (row[0] - min_x) / kx + left
y = height - bottom - (row[1] - min_y) / ky
if is_first:
ctx.move_to(x, y)
else:
if row[0] - prevX > 10000:
ctx.move_to(x, y)
else:
ctx.line_to(x, y)
prevX = row[0]
is_first = False
ctx.stroke()
elif typ == 1: # Точечная
for ind in range(4):
if chart_data[ind]:
ctx.set_source_rgb(*colors[ind])
for row in chart_data[ind]:
x = (row[0] - min_x) / kx + left
y = height - bottom - (row[1] - min_y) / ky
ctx.rectangle(x - 3, y - 3, 6, 6)
ctx.fill()
elif typ == 2: # Столбчатая
cy = height - bottom - (-min_y) / ky
for ind in range(4):
for row in chart_data[ind]:
if currVarID != row[2]:
ctx.fill()
for i in range(4):
if series[i] == row[2]:
ctx.set_source_rgb(*colors[i])
x = (row[0] - min_x) / kx + left
y = height - bottom - (row[1] - min_y) / ky
ctx.rectangle(x - 5, y, 10, cy - y)
currVarID = row[2]
ctx.fill()
else: # Линейчастая
# one_vals = self._get_one_val(series, min_x_q, max_x_q)
one_vals = self._get_one_val(series, x_min_max_values, min_x_q, max_x_q)
for ind in range(4):
if series[ind]:
is_now = True
for r in one_vals[ind]:
if r[4] == 1:
chart_data[ind].insert(0, r)
else:
chart_data[ind] += [r]
is_now = False
if is_now:
if len(chart_data[ind]) > 0:
r = list(chart_data[ind][len(chart_data[ind]) - 1])
r[0] = datetime.datetime.now().timestamp()
chart_data[ind] += [r]
color = colors[ind]
color_fill = color.copy()
color_fill += [0.3]
is_first = True
y0 = height - bottom + min_y / ky
for row in chart_data[ind]:
x = (row[0] - min_x) / kx + left
y = height - bottom - (row[1] - min_y) / ky
if is_first:
is_first = False
else:
ctx.set_source_rgb(*color)
ctx.move_to(prevX, prevY)
ctx.line_to(x, prevY)
ctx.line_to(x, y)
ctx.stroke()
ctx.set_source_rgba(*color_fill)
rx, ry, rw, rh = prevX, y0, x - prevX, prevY - y0
ctx.rectangle(rx, ry, rw, rh)
ctx.fill()
prevX, prevY = x, y
# Рисуем оси
ctx.set_source_rgb(0, 0, 0)
ctx.move_to(left, 0)
ctx.line_to(left, height - bottom)
ctx.line_to(width, height - bottom)
ctx.stroke()
# ---------------------------
del ctx
byt = BytesIO()
img.write_to_png(byt)
byt.seek(0)
return byt.read()