Thursday, November 28, 2024
Google search engine
HomeLanguagesPyCairo – Fill Solid Colors

PyCairo – Fill Solid Colors

In this article we will learn how  to fill colors in a closed pattern using PyCairo in python. Pycairo is a Python module providing bindings for the cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read only) is with a modern web browser like Chrome, Firefox, Edge, or Internet Explorer—nearly all of them should provide some sort of rendering support for the SVG format.

Here we will use the fill ( ) method. 

A color is an object representing a combination of Red, Green, and Blue (RGB) intensity values. PyCairo valid RGB values are in the range 0 to 1.

Steps of Implementation :

  1. Import the PyCairo module.
  2. Create a SVG surface object and add context to it.
  3. Setting color of the context & line width
  4. Creating a rectangular shape
  5. The source is used to fill the interior of a rectangle by calling the fill ( ) method.

Example :

Python




# importing pycairo
import cairo
 
# creating a SVG surface
# here geek95 is file name & 700, 700 is dimension
with cairo.SVGSurface("geek95.svg", 700, 700) as surface:
 
    # creating a cairo context object for SVG surface
    # using Context method
    context = cairo.Context(surface)
 
    # setting color of the context
    context.set_source_rgb(0.2, 0.23, 0.9)
    # creating a rectangle
    context.rectangle(10, 15, 90, 60)
 
    # Fill the color inside the rectangle
    context.fill()
 
    # setting color of the context
    context.set_source_rgb(0.9, 0.1, 0.1)
    # creating a rectangle
    context.rectangle(130, 15, 90, 60)
 
    # Fill the color inside the rectangle
    context.fill()
 
    # setting color of the context
    context.set_source_rgb(0.4, 0.9, 0.4)
    # creating a rectangle
    context.rectangle(250, 15, 90, 60)
 
    # Fill the color inside the rectangle
    context.fill()
 
    # printing message when file is saved
    print("File Saved")


Output :

Note: The set_source_rgb ( ) method sets the source to an opaque color. The parameters are the Red, Green, Blue intensity values. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments