### Color and Pattern Attributes

Color and Pattern Attributes classes specify color appearances and spaces, and allow you to create or modify existing patterns.

### IPaint

The IPaint attribute specifies the color appearance of the geometry source primitive. The 2D Graphics Framework differs from traditional graphics systems in that it does not use a mechanism for foreground color, foreground pattern, background color, or background pattern. Instead, you specify a fill and frame color appearance by using a combination of IPaint objects and a transfer mode attribute.

The IPaint class is a concrete class that encapsulates the color and image pattern you can set. If you do not specify a color or image pattern, IPaint provides a default value.

> class IPaint {
> public:
> IPaint ();
> virtual ~IPaint ();

This constructor takes a reference of a color of type IBaseColor and a mask pattern of type IMaskPattern as parameters:

> IPaint(
> const IBaseColor& aColor,
> const IMaskPattern& maskPattern = IMaskPattern::kSolid,
> const IGPoint2D& patternPhase = IGPoint2D::origin());

The patternPhase parameter defines the offset for tiling the pattern. The default value is the origin of the object. This parameter is also called patternOrigin in Visual Age C++ 3.5.

This constructor takes a reference of an image pattern of type IGImage as a parameter:

> IPaint (
> const IGImage& imagePattern);
> const IGPoint2D& patternPhase = IGPoint2D::origin());

The IPaint attribute provides the following data accessors of the IBaseColor color element:

> virtual void setColor(const IBaseColor& color);
> virtual const IBaseColor* color() const;

The IPaint attribute provides the following data accessors of the IMaskPattern mask pattern element:

> virtual const IMaskPattern* maskPattern() const;
> virtual void setMaskPattern(const IMaskPattern& MaskPattern = IMaskPattern::kSolid);

The IPaint attribute provides the following data accessors of the IGImage image pattern element:

> virtual void setImagePattern(const IGImage& imagePattern);
> virtual const IGImage* imagePattern() const;

To support the polymorphic subclass in the ease-of-use copy semantic API of the IGrafBundle use:

> StreamableDeclarationMacro(IPaint);

Each IPaint attribute contains an instance of IBaseColor. IPaint also has the image pattern which allows for the specification of a repeating image as the filled or framed appearance. Each IPaint instance contains a pointer to an instance of an IGImage.

If the IGImage is larger than 8x8 pixels, only the upper left corner of the image is used.

To extend the capability of IPaint, you can subclass IPaint to add attributes that allow you to express, for example, gradients, fills, or 2D shaders.

Equivalent functions are supported on GDI as "Bitmap as Brushes" functions.

### IBaseColor

IBaseColor is a concrete class that encapsulates all possible color spaces including RGB, HSV, and CIE-XYZ. It has the default representation of compact RGB color (where each element of red, green, and blue is represented by an 8-bit unsigned number). The IBaseColor class is as follows:

> class IBaseColor {
> public:

IBaseColor uses two data types to define color ranges and intensities:

- GIntensity
- CharIntensity

GIntensity is of type float. CharIntensity is an unsigned character.

The enumerated type EPredefinedColor defines some commonly used colors:

- kWhite
- kBlue
- kRed
- kPink
- kGreen
- kCyan
- kYellow
- kBlack
- kDarkGray
- kDarkBlue
- kDarkRed
- kDarkPink
- kDarkGreen
- kDarkCyan
- kBrown
- kPaleGray

The default constructor sets default color to black:

> IBaseColor();
> virtual ~IBaseColor();

The following constructor takes the enumerated type EPredefinedColor as a parameter.

> IBaseColor(EPredefinedColor commonlyUsedColor);

The following constructor takes gray intensities as parameters:

> IBaseColor(CharIntensity gray, CharIntensity opacity = 255);

The following constructor takes RGB colors as parameters. The precision is only 8 bit by default, but you can subclass it to enhance precision.

> IBaseColor(CharIntensity red, CharIntensity green, CharIntensity blue, CharIntensity opacity = 255);

IBaseColor provides the following data accessors for default compact RGB color support:

> CharIntensity red() const;
> CharIntensity green() const;
> CharIntensity blue() const;
> CharIntensity opacity() const;
> virtual void setRed(CharIntensity red);
> virtual void setGreen(CharIntensity green);
> virtual void setBlue(CharIntensity blue);
> virtual void setOpacity(CharIntensity opacity);

IBaseColor provides the following assignment operator, which subclasses must override:

> virtual IBaseColor& operator=(const IBaseColor&);

To support the polymorphic subclass in the ease-of-use copy semantic API of the IGrafBundle use:

> StreamableDeclarationMacro(IPaint);

For conversion to device-independent universal CIE-XYZ color space, your subclass should implement the following conversion functions that provide access for color conversion and color matching capabilities:

> virtual void elementsInXYZColorSpace(
> GIntensity& X,
> GIntensity& Y,
> GIntensity& Z) const;
> virtual void setElementsInXYZColorSpace(
> GIntensity X,
> GIntensity Y,
> GIntensity Z) const;

NOTE: GIntensity is used here for precision calculation. It is defined to be a single-precision floating-point number.

For better precision than the default compact RGB color representation, your subclass can override these virtual member functions where precision is required. The base class implementation only supports the compact RGB color.

You can subclass IBaseColor to represent color metric in different precision and different color space. Your subclass must override the assignment operator and these virtual member functions:

> virtual red(GIntensity& red) const;
> virtual green(GIntensity& green) const;
> virtual blue(GIntensity& blue) const;|
> virtual void setRed(GIntensity red);
> virtual void setGreen(GIntensity green);
> virtual void setBlue(GIntensity blue);

The subclass constructor must at least initialize the base class by calling the base class constructor that takes RGB color as the parameter as follows:.

> IBaseColor(
> CharIntensity Red,
> CharIntensity Green,
> CharIntensity Blue);

You can also do this by calling the setters in the base class. Otherwise the default compact RGB color support does not represent any specific color other than the default black. In that case, the color specified in the subclass is wrong because it differs from what appears in the base class. The subclass can override all virtual functions in the base class, thus overloading the default compact RGB color support.

### IMaskPattern

IMaskPattern is an 8-by-8 image pattern of 1-bit color depth (black = transparent, white = solid). You can create the pattern or use one of the supported patterns defined by the enumerated type EPattern:

- kSolidPattern
- kDiagonalDownPattern
- kCrossPattern
- kDiagonalCrossPattern
- kDiagonalUpPattern
- kHorizontalPattern
- kVerticalPattern

When creating an image pattern, the pre-defined IMaskPattern provides a transparent background and uses the IPaint color specified to draw the foreground. However, for a custom IMaskPattern, the background is opaque and the background color will always be the default color (black). The foreground color for the custom IMaskPattern should still be the IPaint color. In short, a user-defined IMaskPattern will always have a black background, and only a pre-defined IMaskPattern will have a transparent background.