在 PS 文件中使用渐变 | Python

Contents
[ Hide Show ]

在 PS 文档中添加渐变

本文探讨了如何使用 Aspose.Page for Python 在 PS 文档中实现渐变。

渐变表示颜色之间的平滑过渡,可增强绘制图像的真实感。

在 Aspose.Page for Python 中,渐变作为 aspose.pydrawing.Brush 的子类实现,具体如下:

要在 PsDocument 中应用绘画或描边,需要将用于绘画的 aspose.pydrawing.Brush 类和用于描边的 aspose.pydrawing.Pen 类的对象传递到相应的方法中。 Aspose.Page for Python 支持 .NET 平台提供的 aspose.pydrawing.Brush 中的所有基本类,包括 aspose.pydrawing.Coloraspose.pydrawing.TextureBrushaspose.pydrawing.drawing2d.LinearGradientBrushaspose.pydrawing.drawing2d.PathGradientBrush。描边颜色与描边属性分开分配,可通过 aspose.pydrawing.Pen 对象 中的 aspose.pydrawing.Brush 进行设置。

要使用渐变绘制图形对象,请创建 aspose.pydrawing.drawing2d.LinearGradientBrushaspose.pydrawing.drawing2d.PathGradientBrush,并将其传递给 set_paint()fill_text()fill_and_stroke_text() 方法(这些方法接受 aspose.pydrawing.Brush 作为参数)。

要使用渐变勾勒图形对象的轮廓,请将 aspose.pydrawing.drawing2d.LinearGradientBrushaspose.pydrawing.drawing2d.PathGradientBrush 传递给 set_paint()outline_text()fill_and_stroke_text() 方法(这些方法接受描边绘制作为参数)。

在下面的示例中,我们演示了如何填充形状和文本,并使用渐变勾勒文本轮廓。

在新的 PS 文档中使用渐变绘制图形对象的算法包括以下步骤:

  1. 为生成的 PS 文件创建输出流。
  2. 启动 PsSaveOptions
  3. 使用已创建的输出流和保存选项创建 PsDocument
  4. 根据我们要填充或勾勒轮廓的对象,创建必要的图形路径或字体。
  5. 根据所需的渐变形式,创建 aspose.pydrawing.drawing2d.LinearGradientBrushaspose.pydrawing.drawing2d.PathGradientBrush 对象。
  6. 为该画笔设置必要的变换。
  7. 将渐变画笔设置为 PsDocument 中的当前画笔。
  8. 使用当前画笔填充图形路径或填充文本。如果我们使用接受 aspose.pydrawing.Brush 作为参数的文本填充方法,则可以忽略上一步。
  9. 关闭页面。
  10. 保存文档。

如果我们需要使用渐变来描边(勾勒轮廓)图形对象,则无需执行后四步:

  1. 将渐变设置为 PsDocument 中的当前画笔。
  2. 创建 aspose.pydrawing.Pen 对象。
  3. 将此描边设置为 PsDocument 中的当前描边。
  4. 使用当前描边勾勒图形路径或文本的轮廓。如果我们使用接受 aspose.pydrawing.Pen 作为参数的文本勾勒轮廓方法,则可以忽略上一步。
  5. 关闭页面。
  6. 保存文档。

这里有 5 个独立的代码片段,演示了不同渐变的用法。

在这个代码片段中,我们用两种颜色创建水平线性渐变,填充一个矩形,填充一个文本,并用此渐变勾勒出文本的轮廓。

 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_gradient()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "HorizontalGradient_outPS.ps", "wb") as out_ps_stream:
 6    # Create the save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    offset_x = 200.
13    offset_y = 100.
14    width = 200.
15    height = 100.
16    
17    # Create a graphics path from the first rectangle
18    path = aspose.pydrawing.drawing2d.GraphicsPath()
19    path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
20    
21    # Create a linear gradient brush with a rectangle as a bounds, start and end colors
22    brush = GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, width, height),
23        aspose.pydrawing.Color.from_argb(150, 0, 0, 0), aspose.pydrawing.Color.from_argb(50, 40, 128, 70), 0)
24    # Create a transform for brush. X and Y scale component must be equal to the width and height of the rectangle correspondingly.
25    # Translation components are offsets of the rectangle
26    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
27    # Set the transform
28    brush.transform = brush_transform
29    
30    # Set the paint
31    document.set_paint(brush)
32    
33    # Fill the rectangle
34    document.fill(path)
35    
36    # Fill the text with the gradient
37    font = ExternalFontCache.fetch_dr_font("Arial", 96, aspose.pydrawing.FontStyle.BOLD)
38    document.fill_and_stroke_text("ABC", font, 200, 300, brush,
39    GraphicsFactory.create_pen_by_brush_and_width(aspose.pydrawing.SolidBrush(aspose.pydrawing.Color.black), 2))
40    
41    # Set a current stroke
42    document.set_stroke(GraphicsFactory.create_pen_by_brush_and_width(brush, 5))
43    # Outline text with the gradient
44    document.outline_text("ABC", font, 200, 400)
45    
46    # Close the current page
47    document.close_page()
48    
49    # Save the document
50    document.save()

运行此代码的结果是

水平渐变

在此代码片段中,您可以看到如何从 5 种颜色创建垂直线性渐变,并用此渐变填充矩形。

 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_gradient()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "VerticalGradient_outPS.ps", "wb") as out_ps_stream:
 6    # Create the save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    offset_x = 200.
13    offset_y = 100.
14    width = 200.
15    height = 100.
16    
17    # Create a graphics path from the first rectangle
18    path = aspose.pydrawing.drawing2d.GraphicsPath()
19    path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
20    
21    # Create an array of interpolation colors
22    colors = [ aspose.pydrawing.Color.red, aspose.pydrawing.Color.green, aspose.pydrawing.Color.blue,
23    aspose.pydrawing.Color.orange, aspose.pydrawing.Color.dark_olive_green ]
24    positions = [ 0.0, 0.1873, 0.492, 0.734, 1.0 ]
25    color_blend = aspose.pydrawing.drawing2d.ColorBlend()
26    color_blend.colors = colors
27    color_blend.positions = positions
28    
29    # Create a linear gradient brush with a rectangle as a bounds, start and end colors
30    brush = GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, width, height),
31    aspose.pydrawing.Color.beige, aspose.pydrawing.Color.dodger_blue, 0)
32    # Set interpolation colors
33    brush.interpolation_colors = color_blend
34    # Create a transform for brush. X and Y scale component must be equal to the width and height of the rectangle correspondingly.
35    # Translation components are offsets of the rectangle
36    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
37    # Rotate transform to get colors change in the vertical direction from up to down
38    brush_transform.rotate(90.)
39    # Set the transform
40    brush.transform = brush_transform
41    
42    # Set the paint
43    document.set_paint(brush)
44    
45    # Fill the rectangle
46    document.fill(path)
47    
48    # Close the current page
49    document.close_page()
50    
51    # Save the document
52    document.save()

结果如下

垂直渐变

在此代码片段中,我们用两种颜色创建了一个对角线性渐变,并用该渐变填充了一个矩形。

 1# Create an output stream for PostScript document
 2with open(data_dir + "DiagonaGradient_outPS.ps", "wb") as out_ps_stream:
 3    # Create the save options with A4 size
 4    options = PsSaveOptions()
 5    
 6    # Create a new 1-paged PS Document
 7    document = PsDocument(out_ps_stream, options, False)
 8    
 9    offset_x = 200.
10    offset_y = 100.
11    width = 200.
12    height = 100.
13    
14    # Create a graphics path from the first rectangle
15    path = aspose.pydrawing.drawing2d.GraphicsPath()
16    path.add_rectangle(aspose.pydrawing.RectangleF(offset_x, offset_y, width, height))
17    
18    # Create a linear gradient brush with a rectangle as a bounds, start and end colors
19    brush = GraphicsFactory.create_linear_gradient_brush_by_rect_and_angle(aspose.pydrawing.RectangleF(0, 0, width, height),
20    aspose.pydrawing.Color.from_argb(255, 255, 0, 0), aspose.pydrawing.Color.from_argb(255, 0, 0, 255), 0)
21    
22    # Create a transform for brush. X and Y scale component must be equal to the width and height of the rectangle correspondingly.
23    # Translation components are offsets of the rectangle                
24    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
25    # Rotate gradient, than scale and translate to get the visible color transition in the required rectangle
26    brush_transform.rotate(-45.)
27    hypotenuse = float(math.sqrt(200. * 200. + 100. * 100.))
28    ratio = hypotenuse / 200.
29    brush_transform.scale(-ratio, 1.)
30    brush_transform.translate(100. / brush_transform.elements[0], 0.)
31    
32    # Set the transform
33    brush.transform = brush_transform
34    
35    # Set the paint
36    document.set_paint(brush)
37    
38    # Fill the rectangle
39    document.fill(path)
40    
41    # Close the current page
42    document.close_page()
43    
44    # Save the document
45    document.save()

结果如下

![对角渐变](/page/images/python-net/Diagonal Gradient.png)

这里我们用两种颜色创建一个径向渐变,并用该渐变填充一个圆圈。

 1# The path to the documents directory.
 2data_dir = Util.get_data_dir_working_with_gradient()
 3
 4# Create an output stream for the PostScript document
 5with open(data_dir + "RadialGradient1_outPS.ps", "wb") as out_ps_stream:
 6    # Create the save options with A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    offset_x = 200.
13    offset_y = 100.
14    width = 200.
15    height = 200.
16    
17    # Create a graphics path from the rectangle bounds
18    bounds = aspose.pydrawing.RectangleF(offset_x, offset_y, width, height)
19    path = aspose.pydrawing.drawing2d.GraphicsPath()
20    path.add_ellipse(bounds)
21    
22    # Create and fill a color blend object
23    colors = [ aspose.pydrawing.Color.white, aspose.pydrawing.Color.white, aspose.pydrawing.Color.blue ]
24    positions = [ 0.0, 0.2, 1.0 ]
25    color_blend = aspose.pydrawing.drawing2d.ColorBlend()
26    color_blend.colors = colors
27    color_blend.positions = positions
28    
29    brush_rect = aspose.pydrawing.drawing2d.GraphicsPath()
30    brush_rect.add_rectangle(aspose.pydrawing.RectangleF(0, 0, width, height))
31    
32    # Create a path gradient brush with a rectangle as a bounds
33    brush = GraphicsFactory.create_path_gradient_brush_by_path(brush_rect)
34    # Set the interpolation colors
35    brush.interpolation_colors = color_blend
36    # Create a transform for brush. X and Y scale component must be equal to the width and height of the rectangle correspondingly.
37    # Translation components are offsets of the rectangle
38    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
39    # Set the transform
40    brush.transform = brush_transform
41    
42    # Set the paint
43    document.set_paint(brush)
44    
45    # Fill the rectangle
46    document.fill(path)
47    
48    # Close the current page
49    document.close_page()
50    
51    #Save the document
52    document.save()

结果

径向渐变 imag1

在此代码片段中,我们创建了一个由 6 种颜色组成的径向渐变,并用该渐变填充了一个矩形。

 1# The path to the documents directory.
 2  data_dir = Util.get_data_dir_working_with_gradient()
 3  
 4# Create an utput stream for the PostScript document
 5with open(data_dir + "RadialGradient2_outPS.ps", "wb") as out_ps_stream:
 6    # Create save options with the A4 size
 7    options = PsSaveOptions()
 8    
 9    # Create a new 1-paged PS Document
10    document = PsDocument(out_ps_stream, options, False)
11    
12    offset_x = 200.
13    offset_y = 100.
14    width = 200.
15    height = 200.
16    
17    # Create a graphics path from the rectangle bounds
18    bounds = aspose.pydrawing.RectangleF(offset_x, offset_y, width, height)
19    path = aspose.pydrawing.drawing2d.GraphicsPath()
20    path.add_rectangle(bounds)
21    
22    # Create and fill a color blend object
23    colors = [ aspose.pydrawing.Color.green, aspose.pydrawing.Color.blue, aspose.pydrawing.Color.black,
24    aspose.pydrawing.Color.yellow, aspose.pydrawing.Color.beige, aspose.pydrawing.Color.red ]
25    positions = [ 0.0, 0.2, 0.3, 0.4, 0.9, 1.0 ]
26    color_blend = aspose.pydrawing.drawing2d.ColorBlend()
27    color_blend.colors = colors
28    color_blend.positions = positions
29    
30    brush_rect = aspose.pydrawing.drawing2d.GraphicsPath()
31    brush_rect.add_rectangle(aspose.pydrawing.RectangleF(0, 0, width, height))
32    
33    # Create a path gradient brush with a rectangle as a bounds
34    brush = GraphicsFactory.create_path_gradient_brush_by_path(brush_rect)
35    # Set interpolation colors
36    brush.interpolation_colors = color_blend
37    # Create a transform for brush. X and Y scale component must be equal to the width and height of the rectangle correspondingly.
38    # Translation components are offsets of the rectangle
39    brush_transform = aspose.pydrawing.drawing2d.Matrix(width, 0., 0., height, offset_x, offset_y)
40    # Set the transform
41    brush.transform = brush_transform
42    
43    # Set the paint
44    document.set_paint(brush)
45    
46    # Fill the rectangle
47    document.fill(path)
48    
49    # Close the current page
50    document.close_page()
51    
52    # Save the document
53    document.save()

结果如下所示:

径向渐变图像 2

请参阅 .NETJava 中 PS 文档中的渐变操作。

您可以从 GitHub下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.