Working with Colors in Teigha PRC

Alex Rumjantsev
November 24, 2017

Tags: prc export Example

Teigha PRC database organization

Each file structure has global colors and style arrays.

     OdPrcFileStructure 1
  OdPrcFileStructure 2
  OdPrcFileStructure 3

A color array is an array of R,G,B values, each of which are in the 0-1 range.

A styles array is an array of OdPrcObjectId(s) and contains all the OdPrcObjectId(s) that are used in styles of the file structure.

A color should be added to the color array, and then the index of the color in the colors() array can be used in a style object. By PRC standards, the index is 0, 3, 6, 9 — an index of an R value in an array of doubles.

R - 0
R - 3

In Teigha PRC version 4.3.0, methods related to color support were redesigned. Now an “index” of a color is 0,1,2,3… — an index of color in an array.,g,b>

{R,G,B} - 0

{R,G,B} - 1

Also some helper methods were added.

Inherited from OdPrcGraphics

Teigha PRC classes inherited from OdPrcGraphics work with color via the following:

OdPrcObjectId & OdPrcGraphics.styleId();

The style ID should be initialized with the OdPrcObjectId of an OdPrcCategory1LineStyle object.

Code example:

OdPrcObjectId styleId;
styleId = OdPrcCategory1LineStyle::createByColor(1, 0, 1, *curFileStructure);
pBaseWithGraphics->styleId() = styleId;

OdPrcObjectId OdPrcCategory1LineStyle::createByColor(double r, double g, double b, 
 OdPrcFileStructure &postToFileStructure, bool preventColorDuplication)

OdPrcCategory1LineStyle::createByColor is a helper method and does the following:

  • Checks that the R,G,B values are in the 0-1 range.
  • Adds the new color’s R,G,B values to the globals().colors() array.
  • Creates a new style, adds it to the file structure, and adds it to category1LineStyles().
  • Assigns an index of color to the new style.
  • Returns the ID of the new style.

If preventColorDuplication = true, a new color is not added when a color with the same R,G,B values already exists in the colors() array.

OdPrcCategory1LineStyle level methods

There are a few more color-related methods in the OdPrcCategory1LineStyle class.

void setColor(OdPrcColorIndex colorIndex = OdPrcColorIndex::kUninit);

colorIndex is an index of an R,G,B color in the globals().colors() array. This method can also be used to reset a color to an unassigned value.

void setColor(double r, double g, double b, bool preventDuplication = false);

Sets R,G,B (0-1 range) color values to a style. The style must already be added to the file structure or an exception will be thrown. If preventDuplication = true, the method will search for the same color in the colors() array and will not add a new color if the color is already available.

OdPrcColorIndex getColor() const;

Returns the color index and can be used to check whether the color was initialized.

const OdPrcRgbColor &getTrueColor() const;

Returns the color as R,G,B and will throw an exception if no color is assigned or if the object is not in the database.

Other classes

Similar methods were added to some other classes that use colors. OdPrcDottingPattern, OdPrcMaterialGeneric, OdPrcSolidPattern, and OdPrcContentLight work with properties such as specular, emissive, and diffuse colors.

All posts