Frequently Asked: How can I modify drawn colors from my application during display?

Andrew Markovich

October 24, 2017

Sometimes rendering applications require a modification to drawn colors during rendering without a long update of geometry cache. For example, this functionality is helpful to draw graphics using a corrected color palette, inverted colors, grayscale or monochrome rendering modes, color gamma, brightness and contrast correction. This functionality is available for all primary Teigha vectorization modules ("WinGDI.txv", "WinOpenGL.txv", "WinDirectX.txv" and "WinGLES2.txv").

Include the color converter interface:

// 1) Include color conversion callback interface
#include "Teigha/Kernel/Extensions/ExRender/ExColorConverterCallback.h"

Implement your own color conversion function:

// 2) Define your own color conversion callback method. In this example we invert R, G and B color components.
class UserDefColorConversionCallback : public OdColorConverterCallback
{
  virtual ODCOLORREF convert(ODCOLORREF originalColor)
  {
    return ODRGBA(~ODGETRED(originalColor), ~ODGETGREEN(originalColor), ~ODGETBLUE(originalColor), ODGETALPHA(originalColor));
  }
};

Attach the converted color to a Teigha vectorization module using OdGsDevice properties:

// 3) Construct and attach user-defined color conversion callback to Teigha vectorization device using device properties.
void attachColorConverter(OdGsDevice *pGsDevice)
{
  if (pGsDevice && pGsDevice->properties() && pGsDevice->properties()->has(OD_T("ColorConverter")))
  {
    OdColorConverterCallbackPtr pCC = OdRxObjectImpl<UserDefColorConversionCallback>::createObject()
    pGsDevice->properties()->putAt(OD_T("ColorConverter"), pCC);
  }
}

Example of a drawing before color conversion:

image1

Example of the same drawing after color conversion using the color inversion function from the source code example:

image2