Drawings SDK Developer Guide > Working with .dwg Files > Working with Colors > Color Functionality as an Integer-value
Color Functionality as an Integer-value

A color is represented as a 32-bit packed integer value that stores the color method and color components together. The assignment of components depends on the color method. The database color object and entity color object use Integer-values. The color objects are declared by the following lines:


OdCmEntityColor eColor;
OdCmColor nColor;

To get the color as an integer value, use the color() method that returns the color as a value of the OdUInt32 type. Use the hexadecimal format to represent the returned value. For example:


odPrintConsoleString(L"\nEntity Color value = %x", eColor.color());
odPrintConsoleString(L"\nDatabase Color value = %x", nColor.color());

The obtained color value can be separated into components using bit-operations. For example:


OdUInt32 value = eColor.color();

if(((value >> 24) & 0x0F) == 3)
  odPrintConsoleString(L"\nColor Method is ACI and index = %d", (value & 0xFF));

To set the color using an integer value, use the setColor() method that requires an integer value to set the color. The setColor() method uses 24 to 31 bits to specify the color method and 0 to 23 bits to specify the color components. If these bits contain an incorrect color value, the setColor() method generates the eInvalidInput exception. Use the try-catch statement when this method is called. For example:


try {
  eColor.setColor(0xC2030A1E);
  nColor.setColor(0xC3000012);
}
catch(OdError e) {  
  odPrintConsoleString(L"\nError %d - %s", e.code(), e.description());
}

To compare two unnamed color instances, use the (==) operator that returns True when two unnamed color instances are equal or False otherwise, and use the (!=) operator that returns True when two unnamed color instances are not equal or False otherwise. These operators take into account the color method and color components when they compare color instances—they compare the colors as integer numbers. For example:


eColor.setColor(0xC2010A0E);
OdCmEntityColor eColorX; 

eColorX.setRGB(1, 10, 14);
odPrintConsoleString(L"\n[%x] == [%x] => %s", eColor.color(), eColorX.color(), 
                      ((eColor == eColorX) ? L"true" : L"false");              // [0xC2010A0E] == [0xC2010A0E] => true
eColorX.setRGB(10, 15, 12);
odPrintConsoleString(L"\n[%x] == [%x] => %s", eColor.color(), eColorX.color(), 
                      ((eColor == eColorX) ? L"true" : L"false");              // [0xC2010A0E] == [0xC20A0F0C] => false
eColorX.setColorIndex(26);
odPrintConsoleString(L"\n[%x] != [%x] => %s", eColor.color(), eColorX.color(), 
                      ((eColor != eColorX) ? L"true" : L"false");              // [0xC2010A0E] == [0xC300001A] => true

nColor.setColor(0xC2010A0E);
OdCmColor nColorX; 

nColorX.setRGB(1, 10, 14);
odPrintConsoleString(L"\n[%x] == [%x] => %s", nColor.color(), nColorX.color(), 
                      ((nColor == nColorX) ? L"true" : L"false");              // [0xC2010A0E] == [0xC2010A0E] => true
nColorX.setColorIndex(32);
odPrintConsoleString(L"\n[%x] == [%x] => %s", nColor.color(), nColorX.color(), 
                      ((nColor == nColorX) ? L"true" : L"false");              // [0xC2010A0E] == [0xC3000020] => false
nColorX.setColor(0xC1000000);
odPrintConsoleString(L"\n[%x] != [%x] => %s", nColor.color(), nColorX.color(), 
                      ((nColor != nColorX) ? L"true" : L"false");              // [0xC2010A0E] == [0xC3000020] => true

The assignment operator of unnamed colors copies the color value from one instance to another instance keeping the color method and color components. For example:


eColorX.setColor(0xC21A1C1E);
nColorX.setColor(0xC300000E);

eColor = eColorX;
nColor = nColorX;

odPrintConsoleString(L"\neColor = [%x] , nColor = [%x]", eColor.color(), nColor.color());

// Result:  eColor = [0xC21A1C1E] , nColor = [0xC300000E]

See Also

Working with Colors and Transparencies

Methods of the Color Definition

Example of Working with the Entity Color

Example of Working with the Database Color

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.