The key to setting the color of an entity using an RGB value is to specify the “color method” explicitly.
Here’s some code which shows how to get and set the color RGB values. Notice, in the second part of the function, before setting the nValue value, we call the setColorMethod() method on AcCmColor object.
void asdkGetColor()
{
Adesk::UInt8 blue, green, red;
AcCmColor colors;
// set the color method
Acad::ErrorStatus es =
colors.setColorMethod(AcCmEntityColor::kByColor);
// ok, lets try and set the color using setRGB and setColor
es = colors.setRGB(3, 2, 1); // nValue is an RGB value
// get the RGB value as an Adesk::Int32
Adesk::Int32 nValue = colors.color();
// now convert back to the original values, first 8 bits blue
blue = nValue;
// now move nValue right 8 bits, green
nValue = nValue>>8;
// now get the green
green = nValue;
// move right to the next 8 bits red
nValue = nValue>>8;
red = nValue;
// should be red = 3, green = 2, blue = 1 :-)
{
// lets try the other way
AcCmColor colors;
Acad::ErrorStatus es =
colors.setColorMethod(AcCmEntityColor::kByColor);
// simply get the value with no colours
// set, only the color method bits
Adesk::Int32 nValue = colors.color();
// now set up the colours from our prev
ious values
Adesk::Int8 *ptr =
reinterpret_cast<Adesk::Int8 *> (&nValue);
// now reconstruct
*(ptr) = blue;
*(ptr+1) = green;
*(ptr+2) = red;
// now we can try setColor
es = colors.setColor(nValue);
}
// all works
}

Leave a Reply