Previous Document Next Document

Understanding the CorelDRAW object model : Working with shapes : Coloring shapes


Coloring shapes

You can add color to a shape by applying a fill (or Fill object) to it. The fill type for a shape is recorded by the Fill.Type property as one of the following constants for the cdrFillType enumeration:

 
cdrUniformFill — uniform fill
 
cdrFountainFill — fountain fill
 
cdrPatternFill — pattern fill
 
cdrTextureFill — texture fill
 
cdrPostScriptFill — PostScript fill
 
cdrHatchFill — hatch fill
 
cdrNoFill — no fill

The following VBA code returns the type of fill that is applied to the active shape:

Dim fillType As cdrFillType
fillType = ActiveShape.Fill.Type

 
You cannot change the fill type for a shape by modifying its Fill.Type property. Instead, you must use the appropriate Fill.Apply...Fill method, as described in the subsections that follow.

You can also add color to a shape by applying an outline (or Outline object) to it.

In addition, the object model provides a variety of properties and methods for working with the colors (or Color objects) that you apply to shapes.

For information on applying fills and outlines and on working with colors, see the following subtopics:

 
 
 
 
 
 
 
 

 
In your macros, you can include queries that search for shapes that have specific fill properties, outline properties, or color properties. For information, see “Including queries in macros.”
Applying uniform fills

Uniform fills consist of a single, solid color. A uniform fill is represented by the Fill.UniformColor property as a Color object.

You can apply a uniform fill to a shape by using the Fill.ApplyUniformFill method. The following VBA example applies a red uniform fill to the active shape:

ActiveShape.Fill.ApplyUniformFill CreateRGBColor(255, 0, 0)

You can change the color of a uniform fill by modifying its Fill.UniformColor property. The following VBA example changes the uniform fill of the active shape to deep navy blue:

ActiveShape.Fill.UniformColor.RGBAssign 0, 0, 102

 
You can remove the uniform fill from a shape by using the Fill.ApplyNoFill method.
Applying fountain fills

Fountain fills display a progression between two colors. A fountain fill is represented by the Fill.Fountain property as a FountainFill object, which specifies the various properties for the fountain fill: start color, end color, angle, blend type, and so on. The colors in a fountain fill are represented by a FountainColors collection.

You can apply a fountain fill to a shape by using the Fill.ApplyFountainFill method. This method provides optional parameters for various fountain-fill settings, such as the midpoint and offset of the blend. The following VBA example creates a simple linear fountain fill, from red to yellow, at 30 degrees to the horizontal:

Dim startCol As New Color, endCol As New Color
startCol.RGBAssign 255, 0, 0
endCol.RGBAssign 255, 255, 0
ActiveShape.Fill.ApplyFountainFill startCol, endCol, cdrLinearFountainFill, 30

You can add a color to a fountain fill by using the FountainColors.Add method. Color positions are integer values in percent, where 0% is the start-color position and 100% is the end-color position. The following VBA example adds a green color to the fountain fill at a position about one-third (33%) of the way from the existing red color:

Dim fFill As FountainFill
Set fFill = ActiveShape.Fill.Fountain
fFill.Colors.Add CreateRGBColor(0, 102, 0), 33

You can move a color in a fountain fill by using the FountainColor.Move method. The following VBA code moves the green color from the previous example to a position that is 60% of the way from the red (that is, more towards the yellow):

ActiveShape.Fill.Fountain.Colors(1).Move 60

You can use the FountainColors.Count property to determine the number of colors between the start color and end color of a fountain fill. (For the preceding example, this value is 1.) The first color in the collection is that start color, and its index number is 0; this color cannot be moved, but its color can be changed. The last color in the collection is the end color, and its index number is (Count + 1); this color cannot be moved, but its color can be changed. The following VBA code changes the end color from yellow to blue:

Dim cols As FountainColors
Set cols = ActiveShape.Fill.Fountain.Colors
cols(cols.Count + 1).Color.RGBAssign 0, 0, 102

 
You can remove the fountain fill from a shape by using the Fill.ApplyNoFill method.
Applying pattern fills

Pattern fills display a series of repeating vector objects or bitmap images. A pattern fill is represented by the Fill.Pattern property as a PatternFill object, which specifies the various properties for the pattern fill: foreground color, background color, tile offset, and so on.

 
The collection of available pattern fills is stored in the PatternCanvases collection.

You can apply a pattern fill to a shape by using the Fill.ApplyPatternFill method.

 
You can remove the pattern fill from a shape by using the Fill.ApplyNoFill method.
Applying texture fills

Texture fills are fractally generated and fill a shape with one image rather than a series of repeating images. A texture fill is represented by the Fill.Texture property as a TextureFill object, which specifies the various properties for the texture fill: origin, resolution, tile offset, and so on.

 
The properties for a texture fill are stored in a TextureFillProperties collection.

You can apply a texture fill to a shape by using the Fill.ApplyTextureFill method.

 
You can remove the texture fill from a shape by using the Fill.ApplyNoFill method.
Applying PostScript fills

PostScript fills are texture fills that are designed by using the PostScript language. A PostScript fill is represented by the Fill.PostScript property as a PostScriptFill object, which specifies the various properties for the PostScript fill.

You can apply a PostScript fill to a shape by using the Fill.ApplyPostScriptFill method.

 
You can remove the PostScript fill from a shape by using the Fill.ApplyNoFill method.
Applying hatch fills

Hatch fills are composed of vector-based lines and can be used to clearly distinguish the materials or object relationships in a drawing. A hatch fill is represented by the Fill.Hatch property as a HatchFill object, which specifies the various properties for the hatch fill.

 
The collection of available hatch-fill patterns is stored in the HatchPatterns collection, and each document stores its own library of hatch-fill patterns in a HatchLibraries collection.

You can apply a hatch fill to a shape by using the Fill.ApplyHatchFill method.

 
You can remove the hatch fill from a shape by using the Fill.ApplyNoFill method.
Applying outlines

You can use the various properties and methods of the Outline class to define the outline of a shape.

The Outline.Type property uses the following constants of the cdrOutlineType enumeration to record whether the specified shape has an outline:

 
cdrOutline — indicates that the shape has an outline
 
cdrNoOutline — indicates that the shape does not have an outline

 
If a shape has no outline, setting its Outline.Type property to cdrOutline applies the document-default outline style.
 
If a shape has an outline, setting its Outline.Type property to cdrNoOutline removes that outline.

The Outline.Width property for an outline sets its width in document units. In the following VBA example, the outline of the selected shapes is set to 1 millimeter:

ActiveDocument.Unit = cdrMillimeter
ActiveSelection.Outline.Width = 1

 
If a shape does not have an outline, its Outline.Width value is 0. Changing this value applies an outline and automatically changes the value for the Outline.Type property from cdrNoOutline to cdrOutline.
 
Similarly, if a shape has an outline, its Outline.Width value is greater than 0. Changing this value to 0 removes the outline and automatically changes the value for the Outline.Type property from cdrOutline to cdrNoOutline.

The Outline.Color property for an outline defines its color, as in the following VBA example:

ActiveSelection.Outline.Color.GrayAssign 0 ' Set to black

 
Setting the color of an outline automatically sets the Outline.Type property of that outline to cdrOutline and applies the default outline width.

The Outline.Style property for an outline specifies the dash settings of that outline. These dash settings are defined by the following properties of the OutlineStyle class:

 
DashCount — represents the number of pairs of dashes and gaps in an outline. This value ranges from 1 to 5.
 
DashLength — represents the length of each dash in an outline. This value is calculated as a multiple of the outline width, which is measured in document units. For example, if DashLength(1) is 5 and the outline is 0.2" wide, the length of the dash is 1"; however, if the width of the line is changed to 0.1", the length of the dash becomes 0.5".
 
GapLength — represents the length ofeach gap in an outline. This value is calculated as a multiple of the outline width, which is measured in document units.
 
Index — represents the index number of a predefined outline style in the OutlineStyles collection for the application. The OutlineStyles collection is customizable, so the index number that is associated with each outline style in the collection may vary from user to user; however, the expression OutlineStyles.Item(0) always specifies a solid line.

 
Outline objects have many other properties, including the following:
StartArrow and EndArrow — specify the arrowhead on each end of an open curve
LineCaps and LineJoin — respectively, specify the type of line caps (butt, round, or square) and line joins (bevel, miter, or round)
NibAngle and NibStretch — specify the shape of the nib used to draw the outline
BehindFill and ScaleWithShape — respectively, draw the outline behind the fill and scale the outline with the shape
 
Outline objects also have methods, including the following:
ConvertToObject — converts the outline to an object
SetProperties — sets most of the available outline properties in a single call
Working with color

The Color class defines the fill colors and outline colors that you apply to shapes. This class provides a number of properties and methods for working with color.

You can determine the color model of a color by accessing its Color.Type property, as in the following VBA example:

Dim colType As cdrColorType
colType = ActiveShape.Outline.Color.Type

The Color.Type property is defined by the cdrColorType enumeration, which provides the following constants (among many others) for supported color models:

 
cdrColorCMYK — specifies the CMYK color model
 
cdrColorRGB — specifies the RGB color model
 
cdrColorGray — specifies the grayscale color model

 
The color components for each supported color model are defined by additional properties of the Color class, as demostrated by the following VBA examples:
CMYK color model — is defined by the Color.CMYKCyan, Color.CMYKMagenta, Color.CMYKYellow, and Color.CMYKBlack properties
RGB color model — is defined by the Color.RGBRed, Color.RGBGreen, and Color.RGBBlue properties
grayscale color model — is defined by the Color.Gray property
 
The range of values that is supported by a color component depends on the color model for that component.

 
To create a color, you can use the automation keyword New, as in Dim col As New Color.
 
To assign a color model to a new color, you can use the desired ...Assign method (such as Color.CMYKAssign, Color.RGBAssign, or Color.GrayAssign). Each of these methods provides one parameter for each color component in its respective color model. For example, col.RGBAssign 0, 0, 102 assigns a deep-blue RGB color to the new color that was created in the previous tip.
 
To use the application’s color-management settings to change the color model that is assigned to a color, you can use the desired ConvertTo... method (such as Color.ConvertToCMYK, Color.ConvertToRGB, or Color.ConvertToGray). For example, ActiveShape.Fill.UniformColor.ConvertToRGB converts the fill of the active shape to the RGB color model.

You can copy the properties of one color to another color by using the Color.CopyAssign method, as in the following VBA example:

Dim sh As Shape
Set sh = ActiveShape
sh.Outline.Color.CopyAssign sh.Fill.UniformColor

 
The color “none” does not exist. To set a fill color or outline color to “none,” you must instead set the fill type or outline type to “none.”

Previous Document Next Document Back to Top

Copyright 2013 Corel Corporation. All rights reserved.