📜  PyCairo - 设置上下文颜色

📅  最后修改于: 2022-05-13 01:55:19.854000             🧑  作者: Mango

PyCairo - 设置上下文颜色

在本文中,我们将看到如何在 pycairo Python中设置上下文颜色。 Pycairo 是一个Python模块,为 Cairo 图形库提供绑定。这个库用于在Python中创建 SVG 即矢量文件。打开 SVG 文件进行查看(只读)的最简单快捷的方法是使用现代 Web 浏览器,如 Chrome、Firefox、Edge 或 Internet Explorer——几乎所有这些浏览器都应该为 SVG 格式提供某种渲染支持。可以使用 RGBA 模型设置颜色,RGBA 代表红绿蓝 alpha。虽然它有时被描述为色彩空间,但它实际上是补充有第四个 Alpha 通道的三通道 RGB 颜色模型。

示例 1:

Python
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek007 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek007.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
  
    # setting color of the context
    context.set_source_rgba(0, 0, 0, 1)
  
    # setting of line width
    context.set_line_width(4)
  
    # move the context to x,y position
    context.move_to(40, 30)
  
    # creating a rectangle(square)
    context.rectangle(100, 100, 100, 100)
  
    # stroke out the color and width property
    context.stroke()
  
  
# printing message when file is saved
print("File Saved")


Python3
# importing pycairo
import cairo
  
# creating a SVG surface 
# here geek007 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek007.svg", 700, 700) as surface:
      
    # creating a cairo context object for SVG surface
    #useing Context method
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(4, 0, 4, 0.5)
      
    # setting of line width
    context.set_line_width(40)
  
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
     #creating a rectangle(square)
    context.rectangle(100, 100, 100, 100)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")


Python3
# importing pycairo
import cairo
  
# creating a SVG surface
# here geek007 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek007.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
  
    # setting color of the context
    context.set_source_rgba(180, 150, 0, 0.5)
  
    # setting of line width
    context.set_line_width(40)
  
    # move the context to x,y position
    context.move_to(40, 30)
  
    # creating a rectangle(square)
    context.rectangle(100, 100, 100, 100)
  
    # stroke out the color and width property
    context.stroke()
  
  
# printing message when file is saved
print("File Saved")


输出 :

示例 2:

蟒蛇3

# importing pycairo
import cairo
  
# creating a SVG surface 
# here geek007 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek007.svg", 700, 700) as surface:
      
    # creating a cairo context object for SVG surface
    #useing Context method
    context = cairo.Context(surface)
      
    # setting color of the context
    context.set_source_rgba(4, 0, 4, 0.5)
      
    # setting of line width
    context.set_line_width(40)
  
  
    # move the context to x,y position
    context.move_to(40, 30) 
      
     #creating a rectangle(square)
    context.rectangle(100, 100, 100, 100)
      
    # stroke out the color and width property
    context.stroke()
  
    
      
# printing message when file is saved
print("File Saved")

输出 :

示例 3:

蟒蛇3

# importing pycairo
import cairo
  
# creating a SVG surface
# here geek007 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek007.svg", 700, 700) as surface:
  
    # creating a cairo context object for SVG surface
    # useing Context method
    context = cairo.Context(surface)
  
    # setting color of the context
    context.set_source_rgba(180, 150, 0, 0.5)
  
    # setting of line width
    context.set_line_width(40)
  
    # move the context to x,y position
    context.move_to(40, 30)
  
    # creating a rectangle(square)
    context.rectangle(100, 100, 100, 100)
  
    # stroke out the color and width property
    context.stroke()
  
  
# printing message when file is saved
print("File Saved")

输出 :