本文整理汇总了Python中cairo.Context.set_source_rgb方法的典型用法代码示例。如果您正苦于以下问题:Python Context.set_source_rgb方法的具体用法?Python Context.set_source_rgb怎么用?Python Context.set_source_rgb使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairo.Context
的用法示例。
在下文中一共展示了Context.set_source_rgb方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
self.shape.draw_on_context(context)
context.set_source_rgb(*global_constants.background)
context.fill_preserve()
context.set_source_rgb(0, 0, 0)
context.stroke()
super().on_draw(widget, context)
示例2: outline
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def outline(self, surface):
border = Context(surface)
border.rectangle(self.w / 64, self.w / 64, self.w - self.w / 32, self.h - self.w / 32 - 2)
border.set_line_width(self.w / 32)
border.set_source_rgb(0.1, 0.1, 0.1)
border.set_line_join(rounded)
border.stroke()
示例3: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
if not self.is_shape_set:
self.layout(context)
context.set_font_size(self.font_size)
self.shape.draw_on_context(context)
context.set_source_rgb(1, 1, 1)
context.fill_preserve()
context.set_source_rgb(0, 0, 0)
context.stroke()
shape = self.shape
label = self.label
if len(label) > 0 and label[-1] == ' ':
label += '.'
xb, yb, w, h, xa, ya = context.text_extents(label)
context.rectangle(shape.start.x + self.padding,
shape.start.y,
shape.width - self.padding,
shape.height)
context.clip()
context.move_to(shape.start.x + (shape.width - self.padding - w)/2,
shape.start.y + shape.height - self.padding)
context.show_text(self.label)
示例4: _draw_cell
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def _draw_cell(self, context: cairo.Context,
cell_value: CrucipixelCellValue, area: Rectangle):
r, g, b = self.crucipixel_cell_value_to_color[cell_value]
context.set_source_rgb(r, g, b)
context.rectangle(area.start.x,
area.start.y,
area.width,
area.height)
context.fill()
if not self.victory_screen and cell_value == CrucipixelCellValue.EMPTY:
# draw the X
r, g, b = self.crucipixel_cell_value_to_color[
CrucipixelCellValue.SELECTED
]
context.set_source_rgb(r, g, b)
context.set_line_cap(cairo.LINE_CAP_ROUND)
delta_x = self.cell_width // 2.8
delta_y = self.cell_height // 2.8
context.move_to(area.start.x + area.width - delta_x,
area.start.y + delta_y)
context.line_to(area.start.x + delta_x,
area.start.y + area.height - delta_y)
context.move_to(area.start.x + area.width - delta_x,
area.start.y + area.width - delta_y)
context.line_to(area.start.x + delta_x,
area.start.y + delta_y)
context.stroke()
示例5: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
context.save()
context.set_source_rgb(*self.background_color)
self.shape.draw_on_context(context)
context.fill_preserve()
context.set_source_rgb(0,0,0)
context.set_line_width(1)
context.stroke()
context.restore()
super().on_draw(widget, context)
示例6: on_draw
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def on_draw(self, widget: Widget, context: cairo.Context):
self.guide_line.set_shape_from_context(context)
shape = self.guide_line.shape
self.guide_line.set_translate(shape.width + 10, shape.height + 10)
context.save()
context.set_source_rgb(1, 1, 1)
if self.guide_line.orientation == Orientation.HORIZONTAL:
context.rectangle(10, shape.height + 10, shape.width, 20)
else:
context.rectangle(shape.width + 10, 10, 20, shape.height)
context.fill()
context.restore()
super().on_draw(widget, context)
示例7: MapSurface
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
class MapSurface(object):
"""wrapper to render the map to svg/png"""
def __init__(self, hexmap=None, filename=None, width=None, height=None, size=None):
self.hexmap = hexmap
if self.hexmap is None:
raise ValueError("No map was passed to {}".format(self.__class__.__name__))
self.surface_name = filename or "test.svg"
self.size = size or 32.0
self.surface_width = width
if self.surface_width is None:
self.surface_width = (self.hexmap.map.cols + .5) * self.size * SQRT3
self.surface_height = height
if self.surface_height is None:
self.surface_height = (self.hexmap.map.rows * 1.5 + .25) * self.size
self.layer = []
# build base map
self.surface = SVGSurface(self.surface_name + ".svg", self.surface_width, self.surface_height)
self.context = Context(self.surface)
# background: magenta
self.context.save()
self.context.set_source_rgb(1.0, 0.0, 1.0)
self.context.paint()
self.context.restore()
def add_layer(self, renderer_cls, position=None):
if not position:
self.layer.append(renderer_cls(self))
else:
self.layer.insert(position, renderer_cls(self))
def render(self):
print "Rendering {} ({}x{})".format(self.surface_name, self.surface_width, self.surface_height)
for renderer in self.layer:
renderer.render()
def finalise(self, with_png=False):
print "finalising:"
if with_png is True:
print "PNG"
self.surface.write_to_png(self.surface_name + ".png")
print "SVG"
self.surface.finish()
print "DONE!"
示例8: renderText
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [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)
示例9: __init__
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
class Canvas:
def __init__(self, width, height):
self.xform = lambda x, y: (x, y)
self.img = ImageSurface(FORMAT_RGB24, width, height)
self.ctx = Context(self.img)
self.ctx.move_to(0, 0)
self.ctx.line_to(width, 0)
self.ctx.line_to(width, height)
self.ctx.line_to(0, height)
self.ctx.line_to(0, 0)
self.ctx.set_source_rgb(1, 1, 1)
self.ctx.fill()
self.width = width
self.height = height
def fit(self, left, top, right, bottom):
xoff = left
yoff = top
xscale = self.width / float(right - left)
yscale = self.height / float(bottom - top)
if abs(xscale) > abs(yscale):
xscale *= abs(yscale) / abs(xscale)
elif abs(xscale) < abs(yscale):
yscale *= abs(xscale) / abs(yscale)
self.xform = lambda x, y: ((x - xoff) * xscale, (y - yoff) * yscale)
def dot(self, x, y, size=4, fill=(.5, .5, .5)):
x, y = self.xform(x, y)
self.ctx.arc(x, y, size/2., 0, 2*pi)
self.ctx.set_source_rgb(*fill)
self.ctx.fill()
def line(self, points, stroke=(.5, .5, .5), width=1):
self.ctx.move_to(*self.xform(*points[0]))
for (x, y) in points[1:]:
self.ctx.line_to(*self.xform(x, y))
self.ctx.set_source_rgb(*stroke)
self.ctx.set_line_cap(LINE_CAP_ROUND)
self.ctx.set_line_width(width)
self.ctx.stroke()
def save(self, filename):
self.img.write_to_png(filename)
示例10: Face
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
face = Face('./Vera.ttf')
face.set_char_size( 4*48*64 )
flags = FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP
face.load_char('S', flags )
slot = face.glyph
glyph = slot.get_glyph()
stroker = Stroker( )
stroker.set(64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0 )
glyph.stroke( stroker , True )
blyph = glyph.to_bitmap(FT_RENDER_MODE_NORMAL, Vector(0,0), True )
bitmap = blyph.bitmap
width, rows, pitch = bitmap.width, bitmap.rows, bitmap.pitch
surface = ImageSurface(FORMAT_ARGB32, 600, 800)
ctx = Context(surface)
Z = make_image_surface(bitmap)
ctx.set_source_surface(Z, 0, 0)
scale = 640.0 / rows
patternZ = ctx.get_source()
SurfacePattern.set_filter(patternZ, FILTER_BEST)
scalematrix = Matrix()
scalematrix.scale(1.0/scale,1.0/scale)
scalematrix.translate(-(300.0 - width *scale /2.0 ), -80)
patternZ.set_matrix(scalematrix)
ctx.set_source_rgb (0 , 0, 1)
ctx.mask(patternZ)
ctx.fill()
surface.flush()
surface.write_to_png("glyph-outline-cairo.png")
Image.open("glyph-outline-cairo.png").show()
示例11: main
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
def main(marker, paper, format, bbox, emergency, place, recipient, sender, text, sender_is_recipient, map_href):
"""
"""
mark = Location(*marker)
handle, filename = mkstemp(prefix='safetymap-', suffix='.pdf')
close(handle)
if paper == 'a4':
surf = PDFSurface(filename, 210*ptpmm, 297*ptpmm)
elif paper == 'letter':
surf = PDFSurface(filename, 8.5*ptpin, 11*ptpin)
ctx = Context(surf)
ctx.scale(ptpmm, ptpmm)
set_font_face_from_file(ctx, 'assets/HelveticaNeue.ttc')
if paper == 'a4':
draw_a4_master(ctx, format, map_href)
ctx.translate(19, 24)
elif paper == 'letter':
draw_letter_master(ctx, format, map_href)
ctx.translate(21, 18)
ctx.set_line_width(.25 * mmppt)
ctx.set_source_rgb(*md_gray)
ctx.set_dash([3 * mmppt])
reps = {'4up': 4, '2up-fridge': 2, 'poster': 0}
if reps[format]:
card_img, mark_point = get_map_image(bbox, 84, 39, mark)
for i in range(reps[format]):
# dashed outlines
ctx.move_to(0, 61)
ctx.line_to(0, 0)
ctx.line_to(173, 0)
ctx.line_to(173, 61)
#ctx.move_to(86, 0)
#ctx.line_to(86, 61)
ctx.stroke()
# two card sides and contents
draw_card_left(ctx, recipient, sender, text, sender_is_recipient)
ctx.translate(86.5, 0)
draw_card_right(ctx, card_img, mark_point, emergency, place)
ctx.translate(-86.5, 61)
if format == '4up':
# bottom dashed outline
ctx.move_to(0, 0)
ctx.line_to(172, 0)
ctx.stroke()
elif format == '2up-fridge':
# prepare to draw sideways
ctx.translate(0, 122.5)
ctx.rotate(-pi/2)
ctx.rectangle(0, 0, 122.5, 173)
ctx.stroke()
poster_img, mark_point = get_map_image(bbox, 109, 77, mark)
draw_small_poster(ctx, poster_img, mark_point, emergency, place, recipient, sender, text, sender_is_recipient)
elif format == 'poster':
ctx.rectangle(0, 0, 173, 245)
ctx.stroke()
poster_img, mark_point = get_map_image(bbox, 153, 108, mark)
draw_large_poster(ctx, poster_img, mark_point, emergency, place, recipient, sender, text, sender_is_recipient)
surf.finish()
chmod(filename, 0644)
return filename
示例12: Floor64
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
scale = 3
def Floor64(x):
return (x//64) * 64
def Ceil64(x):
return ((x+63)//64) * 64
width_s = (width * 64)//scale + 2 * MARGIN
height_s = (rows * 64)//scale + 2 * MARGIN
surface = SVGSurface('glyph-vector-2-cairo.svg',
width_s,
height_s)
ctx = Context(surface)
ctx.set_source_rgb(1,1,1)
ctx.paint()
ctx.save()
ctx.scale(1.0/scale,1.0/scale)
ctx.translate(-Floor64(bbox.xMin) + MARGIN * scale,-Floor64(bbox.yMin) + MARGIN * scale)
ctx.transform(Matrix(1,0,0,-1))
ctx.translate(0, -(Ceil64(bbox.yMax) + Floor64(bbox.yMin))) # difference!
start, end = 0, 0
VERTS, CODES = [], []
# Iterate over each contour
for i in range(len(outline.contours)):
end = outline.contours[i]
points = outline.points[start:end+1]
points.append(points[0])
示例13: ImageSurface
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
surface = ImageSurface(FORMAT_ARGB32,
(cbox.xMax - cbox.xMin)//4 + 20,
(cbox.yMax - cbox.yMin)//4 + 20)
ctx = Context(surface)
ctx.scale(0.25,0.25)
ctx.translate(-cbox.xMin + 40,-cbox.yMin + 40)
ctx.transform(Matrix(1,0,0,-1))
ctx.translate(0, -(cbox.yMax + cbox.yMin)) # difference!
Curve_Tag = [FT_Curve_Tag(tag) for tag in outline.tags]
start, end = 0, 0
VERTS, CODES = [], []
# Iterate over each contour
ctx.set_source_rgb(0.5,0.5,0.5)
for i in range(len(outline.contours)):
end = outline.contours[i]
ctx.move_to(outline.points[start][0],outline.points[start][1])
for j in range(start, end+1):
point = outline.points[j]
ctx.line_to(point[0],point[1])
#back to origin
ctx.line_to(outline.points[start][0], outline.points[start][1])
start = end+1
ctx.fill_preserve()
ctx.set_source_rgb(0,1,0)
ctx.stroke()
start, end = 0, 0
示例14: _paint_panel
# 需要导入模块: from cairo import Context [as 别名]
# 或者: from cairo.Context import set_source_rgb [as 别名]
#.........这里部分代码省略.........
colors = [[1, 0, 0], [0, 0.65, 0.31], [0, 0, 1], [1, 0, 1]]
off_y = (max_y - min_y) / 10
min_y -= off_y
max_y += off_y
try:
kx = (max_x - min_x) / (width - left - right)
ky = (max_y - min_y) / (height - bottom)
if ky == 0:
ky = 1
except:
kx, ky = 1, 1
img = ImageSurface(FORMAT_ARGB32, width, height)
ctx = Context(img)
width -= right
ctx.set_line_width(1)
# Рисуем сетку
ctx.set_font_size(12)
try:
b_w, b_h = ctx.text_extents("00-00-0000")[2:4]
# Метки на оси Y
count = math.ceil(max_y) - math.ceil(min_y)
space_count = math.ceil(count / ((height - bottom) / (b_h * 1.5)))
sc = 0
for i in range(math.ceil(min_y), math.ceil(max_y)):
if sc == 0:
y = height - bottom + (min_y - i) / ky
ctx.set_source_rgb(*(color_x_line))
ctx.move_to(left, y)
ctx.line_to(width, y)
ctx.stroke()
ctx.set_source_rgb(0, 0, 0)
num = str(i)
tw, th = ctx.text_extents(num)[2:4]
ctx.move_to(left - 5 - tw, y + th // 2)
ctx.show_text(num)
sc = space_count
sc -= 1
# Метки на оси Х
x_step = 3600
if interval == "-6 hour" or interval == "-12 hour" or interval == "-1 day":
# Дополнительно метки часов
x_step = 3600
for i in range(math.ceil(min_x / x_step), math.ceil(max_x / x_step)):
x = (i * x_step - min_x) / kx + left
ctx.set_source_rgb(*(color_x_line_2))
ctx.move_to(x, 0)
ctx.line_to(x, height - bottom)
ctx.stroke()
num = datetime.datetime.fromtimestamp(i * x_step).strftime("%H")
tw, th = ctx.text_extents(num)[2:4]
ctx.move_to(x + 2, height - bottom - 3)
ctx.set_source_rgb(*(color_x_line))
ctx.show_text(num)
x_step = 3600 * 24
space_count = 1