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

An RGB-value is an integer value that defines a color through intensities of red, green, and blue color components. Each color component is an 8-bit integer value that defines the intensity in the range 0 to 255. The combination of these color components forms a wide range of displayed colors. An RGB-value requires 24 bits for storing components and allows display of over 16 million colors. For example, combinations of max intensities for red and green components provide the yellow color, red and blue components provide the magenta color, green and blue components provide the cyan color, red, green, and blue components together provide the white color. The packed color integer value stores the red component from 16 to 23 bits, green component from 8 to 15 bits, and blue component from 0 to 7 bits. The byColor color method uses the RGB-value to define colors.

The database color object and entity color object use RGB-values. The color objects are declared by the following lines:


OdCmEntityColor eColor;
OdCmColor nColor;

To get the red color component, use the red() method that returns the red component as an integer value in the range 0 to 255. For example:


odPrintConsoleString(L"\nRed entity color component = %d", eColor.red());
odPrintConsoleString(L"\nRed database color component = %d", nColor.red());

To get the green color component, use the green() method that returns the green component as an integer value in the range 0 to 255. For example:


odPrintConsoleString(L"\nGreen entity color component = %d", eColor.green());
odPrintConsoleString(L"\nGreen database color component = %d", nColor.green());

To get the blue color component, use the blue() method that returns the blue component as an integer value in the range 0 to 255. For example:


odPrintConsoleString(L"\nBlue entity color component = %d", eColor.blue());
odPrintConsoleString(L"\nBlue database color component = %d", nColor.green());

To set the red color component, use the setRed() method that requires the red component as an integer value in the range 0 to 255 and modifies it in the color object. The setRed() method of the OdCmColor object generates the exception eNotApplicable when the color method is not set to byColor. For example:


int red_value;

odPrintConsoleString(L"\nEntry a red value [0...255]:>");
scanf("%d", &red_value);

if(red_value >= 0 && red_value <= 255)
{
  eColor.setRed(red_value);
  nColor.setRed(red_value);
}
else odPrintConsoleString(L"Red value is outside the scope 0...255\n");

To set the green color component, use the setGreen() method that requires the green component as an integer value in the range 0 to 255 and modifies it in the color object. The setGreen() method of the OdCmColor object generates the exception eNotApplicable when the color method is not set to byColor. For example:


int green_value;

odPrintConsoleString(L"\nEntry a green value [0...255]:>");
scanf("%d", &green_value);

if(green_value >= 0 && green_value <= 255)
{
  eColor.setGreen(green_value);
  nColor.setGreen(green_value);
}
else odPrintConsoleString(L"Green value is outside the scope 0...255\n");

To set the blue color component, use the setBlue() method that requires the blue component as an integer value in the range 0 to 255 and modifies it in the color object. The setBlue() method of the OdCmColor object generates the exception eNotApplicable when the color method is not set to byColor. For example:


int blue_value;

odPrintConsoleString(L"\nEntry a blue value [0...255]:>");
scanf("%d", &blue_value);

if(blue_value >= 0 && blue_value <= 255)
{
  eColor.setBlue(blue_value);
  nColor.setBlue(blue_value);
}
else odPrintConsoleString(L"Blue value is outside the scope 0...255\n");

To set the red, green, and blue color components together, use the setRGB() method that requires the red component value as a first argument, the green component value as a second argument, and the blue component value as a third argument. The setRGB() method sets the color method to byColor. For example:


int red_value, green_value, blue_value;

odPrintConsoleString(L"\nEntry a red value [0...255]:>");
scanf("%d", &red_value);

if(red_value >= 0 && red_value <= 255)
{
  odPrintConsoleString(L"\nEntry a green value [0...255]:>");
  scanf("%d", &green_value);

  if(green_value >= 0 && green_value <= 255)
  {
    odPrintConsoleString(L"\nEntry a blue value [0...255]:>");
    scanf("%d", &blue_value);

    if(blue_value >= 0 && blue_value <= 255 )
    {
      eColor.setRGB(red_value, green_value, blue_value);
      nColor.setRGB(red_value, green_value, blue_value);
      
      odPrintConsoleString(L"\nEntity color value = %x", eColor.color());
      odPrintConsoleString(L"\nDatabase color value = %x", nColor.color());
    }
    else odPrintConsoleString(L"Blue value is outside the scope 0...255\n");
  }
  else odPrintConsoleString(L"Green value is outside the scope 0...255\n");
}
else odPrintConsoleString(L"Red value is outside the scope 0...255\n");

To pack the red, green, and blue color components in a 32-bit integer value and work with a color as an integer, use the ODRGB(red, green, blue) macro. For example:


OdUInt32 iCyan = ODRGB(0, 255, 255);
OdUInt32 iYellow = ODRGB(255, 255, 0);
OdUInt32 iMagenta = ODRGB(255, 0, 255);

To extract the red, green, or blue color components from a 32-bit integer value, use the ODGETRED(rgb) macro, ODGETGREEN(rgb) macro, or ODGETBLUE(rgb) macro appropriately. For example:


OdUInt32 iColor = ODRGB(100, 50, 150);

odPrintConsoleString(L"\nRed = %d , Green = % d , Blue = %d", ODGETRED(iColor), ODGETGREEN(iColor), ODGETBLUE(iColor));

To work with the red, green, and blue color components and alpha transparency as with a 32-bit integer value, use the ODRGBA(red, green, blue, alpha) macro and ODGETALPHA(rgba) macro appropriately. For example:


OdUInt32 rgbaColor = ODRGBA(100, 50, 150, 25);

odPrintConsoleString(L"\nRed = %d , Green = % d , Blue = %d , Alpha = %d", 
                     ODGETRED(rgbaColor), ODGETGREEN(rgbaColor), ODGETBLUE(rgbaColor), ODGETALPHA(rgbaColor));

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.