Close

Relief for ODA Team in Ukraine

Learn more
ODA Drawings SDK
Example of Working with the Entity Color

For working with entity colors, this example uses the console menu that represents the following operations: specifying a color method, setting color components (red, green, blue, or index), getting the information about a color, printing the values of color components, verifying a color method, entering a color as an integer value, converting an RGB-value to a color index, converting a color index to an RGB-value, and comparing two color instances.

The main() function implements the console menu for the listed operations. It creates the loop that inquires about the operation used and uses the switch operator to select whether the case must be performed. When the main() function enters a color component (red, green, blue, or index), it checks whether the entered color component value is inside admissible scope. The main() function uses the setRed() method to set a red value, the setGreen() method to set a green value, the setBlue() method to set a blue value, the setColorIndex() method to set a color index, the setRGB() method to set an RGB-value, the setColor() method to set a color as an integer value, and the setColorMethod() method to set a color method. The main() function prints the result and uses the color() method to get the color as an integer value, the red() method to get the red value, the green() method to get the green value, the blue() method to get the blue value, the colorIndex() method to get the color index, and the colorMethod() method to get the current color method. The ColorMethod enumerator defines the available color methods. The main() function uses the lookUpACI() method to convert the RGB-value to the color index and the lookUpRGB() method to convert the color index to the RGB-value.

The Main module has the following implementation:


#include "OdaCommon.h"
#include "OdToolKit.h"
#include "..\..\Drawing\Extensions\ExServices\ExHostAppServices.h"
#include "..\..\Kernel\Extensions\ExServices\ExSystemServices.h"

class MyApp : public ExSystemServices 
{
 protected:
   ODRX_USING_HEAP_OPERATORS(ExSystemServices);
 public:
   MyApp() {};
};

#include <iostream>
using namespace std;

int main()
{
   OdStaticRxObject<MyApp> svcs;

   odInitialize(&svcs);

   wchar_t ch;
   int red, green, blue, value;
   OdCmEntityColor color;

   do {
     wcout << L"\nR. Set RED color component";
     wcout << L"\nG. Set GREEN color component";
     wcout << L"\nB. Set BLUE color component";
     wcout << L"\nS. Set RGB Color";
     wcout << L"\nI. Set ACI Color";
     wcout << L"\nM. Specify color method";
     wcout << L"\nP. Print color information";
     wcout << L"\nC. Print RGBMI-components";
     wcout << L"\nN. Get Index for RGB-value";
     wcout << L"\nU. Get RGB-value for Index";
     wcout << L"\nE. Compare two color instances";
     wcout << L"\nH. Set color hex value (7 digits)";
     wcout << L"\nV. Verify color method using isBy()";
     wcout << L"\nQ. Quit";
     wcout << L"\nSelect operation:>";
     ch = wcin.get();

     switch(ch)
     {
       case L'R':
       case L'r':
         wcout << L"\nEntry a red value [0...255]:>" ;
         wcin >> red;

         if(!wcin.fail())
           if(red >= 0 && red <= 255)
           {
              color.setRed(red);
              wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
           }
           else wcout << L"Red value is outside the scope 0...255\n";
         else {  wcin.clear();  wcout << L"Invalid entered red value.\n";  }
         break;

       case L'G':
       case L'g':
         wcout << L"\nEntry a green value [0...255]:>" ;
         wcin >> green;

         if(!wcin.fail())
           if(green >= 0 && green <= 255)
           {
              color.setGreen(green);
              wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
           }
           else wcout << L"Green value is outside the scope 0...255\n";
         else {  wcin.clear();  wcout << L"Invalid entered green value.\n";  }
         break;

       case L'B':
       case L'b':
         wcout << L"\nEntry a blue value [0...255]:>" ;
         wcin >> blue;

         if(!wcin.fail())
           if(blue >= 0 || blue <= 255)
           {
              color.setBlue(blue);
              wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
           }
           else wcout << L"Blue value is outside the scope 0...255\n";
         else {  wcin.clear();  wcout << L"Invalid entered blue value.\n";  }
         break;

       case L'S':
       case L's':
         wcout << L"\nEntry a red value [0...255]:>" ;
         wcin >> red;

         if(!wcin.fail())
           if(red >= 0 && red <= 255)
           {
              wcout << L"Entry a green value [0...255]:>" ;
              wcin >> green;

              if(!wcin.fail())
                if(green >= 0 && green <= 255)
                {
                   wcout << L"Entry a blue value [0...255]:>" ;
                   wcin >> blue;

                   if(!wcin.fail())
                     if(blue >= 0 && blue <= 255)
                     {
                        color.setRGB(red, green, blue);
                        wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
                     }
                     else wcout << L"Blue value is outside the scope 0...255\n";
                   else {  wcin.clear();  wcout << L"Invalid entered blue value.\n";  }
                }
                else wcout << L"Green value is outside the scope 0...255\n";
              else {  wcin.clear();  wcout << L"Invalid entered green value.\n";  }
           }
           else wcout << L"Red value is outside the scope 0...255\n";
         else {  wcin.clear();  wcout << L"Invalid entered red value.\n";  }
         break;

       case L'I':
       case L'i':
         wcout << L"\nEntry an index [0...257]:>" ;
         wcin >> value;

         if(!wcin.fail())
           if(value >= 0 && value <= 257)
           {
              color.setColorIndex(value);
              wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
           }
           else wcout << L"Index is outside the scope 0...257\n";
         else {  wcin.clear();  wcout << L"Invalid entered index.\n";  }
         break;

       case L'M':
       case L'm':
         wcout << L"\nMethod: 0-ByLayer, 1-ByBlock, 2-ByRGB, 3-ByACI,";
         wcout << L"\n\t4-ByPen, 5-Foreground, 7-ByDgnIndex, 8-None" ;
         wcout << L"\nSpecify the color method:>" ;
         wcin >> value;

         if(!wcin.fail())
           if(value == 0 || value == 1 || value == 2 || value == 3 ||
              value == 4 || value == 5 || value == 7 || value == 8)
           {
              color.setColorMethod((OdCmEntityColor::ColorMethod)(0xC0 | value));
              wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
           }
           else wcout << L"Undefined color method.\n";
         else {  wcin.clear();  wcout << L"Invalid entered method number.\n";  }
         break;

       case L'P':
       case L'p':
         wcout << L"\nColor value [" << hex << color.color() << dec << L"] Information:"
               << L"\nColor method " << (color.colorMethod() & 0x0F) << L" is ";
         switch(color.colorMethod())
         {
           case OdCmEntityColor::kByLayer:
             wcout << L"by Layer\n";
             break;
           case OdCmEntityColor::kByBlock:
             wcout << L"by Block\n";
             break;
           case OdCmEntityColor::kByColor:
             wcout << L"by RGB, Red = " << color.red() 
                   << L", Green = " << color.green() 
                   << L", Blue = " << color.blue() << L"\n";
             break;
           case OdCmEntityColor::kByACI:
             wcout << L"by ACI, Index = " << color.colorIndex() << L"\n";
             break;
           case OdCmEntityColor::kByPen:
             wcout << L"by Pen Table\n";
             break;
           case OdCmEntityColor::kForeground:
             wcout << L"by Foreground\n";
             break;
           case OdCmEntityColor::kByDgnIndex:
             wcout << L"by DGN Pen Table\n";
             break;
           case OdCmEntityColor::kNone:
             wcout << L"none\n";
             break;
         }
         break;

       case L'C':
       case L'c':
         wcout << L"\nMethod = " << (color.colorMethod() & 0x0F)
               << L", Index = " << color.colorIndex()
               << L", Red = " << color.red()
               << L", Green = " << color.green()
               << L", Blue = " << color.blue() << "\n";
         break;

       case L'V':
       case L'v':
         wcout << L"\nIsNone() = " << ((color.isNone()) ? L"true" : L"false") 
               << L"\nIsByACI() = " << ((color.isByACI()) ? L"true" : L"false")
               << L"\nIsByColor() = " << ((color.isByColor()) ? L"true" : L"false")
               << L"\nIsByLayer() = " << ((color.isByLayer()) ? L"true" : L"false")
               << L"\nIsByBlock() = " << ((color.isByBlock()) ? L"true" : L"false")
               << L"\nIsForeground() = " << ((color.isForeground()) ? L"true" : L"false")
               << L"\nIsByDgnIndex() = " << ((color.isByDgnIndex()) ? L"true" : L"false") << L"\n";
         break;

       case L'N':
       case L'n':
         wcout << L"\nEntry a red value:>" ;
         wcin >> red;

         if(!wcin.fail())
         {
            wcout << L"Entry a green value:>" ;
            wcin >> green;

            if(!wcin.fail())
            {
               wcout << L"Entry a blue value:>" ;
               wcin >> blue;

               if(!wcin.fail())
									
                 wcout << L"Index = " << (int)color.lookUpACI(red, green, blue) << L"\n";

               else {  wcin.clear();  wcout << L"Invalid entered blue value.\n";  }
            }
            else {  wcin.clear();  wcout << L"Invalid entered green value.\n";  }
         }
         else {  wcin.clear();  wcout << L"Invalid entered red value.\n";  }
         break;

       case L'U':
       case L'u':
         wcout << L"\nEntry an index:>" ;
         wcin >> value;

         if(!wcin.fail())
				
            wcout << L"RGB-value = " << hex << color.lookUpRGB(value) << dec << L"\n";
				
         else {  wcin.clear();  wcout << L"Invalid entered index.\n";  }
         break;

       case L'H':
       case L'h':
         wcout << L"\nEntry seven hexadecimal digits:>" ;
         wcin >> hex >> value >> dec;

         if(!wcin.fail())
           try {
             color.setColor(value & 0x0FFFFFFF | 0xC0000000);
             wcout << L"Color value [" << hex << color.color() << dec << L"]\n";
           }
           catch(OdError e) {  
             wcout << e.code() << L" - " << e.description();  
           }
         else {  wcin.clear();  wcout << L"Invalid entered value.\n";  }
         break;

       case L'E':
       case L'e':
         wcout << L"\nEntry seven hexadecimal digits for color value to be compared:>" ;
         wcin >> hex >> value >> dec;
         if(!wcin.fail())
           try {
             OdCmEntityColor xcolor; 
             xcolor.setColor(value & 0x0FFFFFFF | 0xC0000000);

             wcout << L"Compare [" << hex << color.color() 
                   << "] == [" << xcolor.color() << "] => " << dec
                   << ((color == xcolor) ? L"true\n" : L"false\n");
           }
           catch(OdError e) {  
             wcout << e.code() << L" - " << e.description();  
           }
         else {  wcin.clear();  wcout << L"Invalid entered value.\n";  }
         break;

       default:
         wcout << L"Incorrect operation\n";
     }
     std::wcin.sync();
   }
   while(ch != L'Q' && ch != L'q');

   odUninitialize();

   return 0;
}

The testing action gives the following results:


R. Set RED color component
G. Set GREEN color component
B. Set BLUE color component
S. Set RGB Color
I. Set ACI Color
M. Specify color method
P. Print color information
C. Print RGBMI-components
N. Get Index for RGB-value
U. Get RGB-value for Index
E. Compare two color instances
H. Set color hex value (7 digits)
V. Verify color method using isBy()
Q. Quit
Select operation:>

When the operation is R-"Set RED color component":


Entry a red value [0...255]:>32
Color value [c2200000]

When the operation is G-"Set GREEN color component":


Entry a green value [0...255]:>18
Color value [c2201200]

When the operation is B-"Set BLUE color component":


Entry a blue value [0...255]:>68
Color value [c2201244]

When the operation is P-"Print color information":


Color value [c2201244] Information:
Color method 2 is by RGB, Red = 32, Green = 18, Blue = 68

When the operation is I-"Set ACI Color":


Entry an index [0...257]:>0
Color value [c1000000]

Entry an index [0...257]:>256
Color value [c0000000]

Entry an index [0...257]:>257
Color value [c8000000]

Entry an index [0...257]:>90
Color value [c300005a]

When the operation is P-"Print color information":


Color value [c300005a] Information:
Color method 3 is by ACI, Index = 90

When the operation is M-"Specify color method":


Method: 0-ByLayer, 1-ByBlock, 2-ByRGB, 3-ByACI,
        4-ByPen, 5-Foreground, 7-ByDgnIndex, 8-None
Specify the color method:>0
Color value [c0000000]

When the operation is V-"Verify color method using isBy()":


IsNone() = false
IsByACI() = false
IsByColor() = false
IsByLayer() = true
IsByBlock() = false
IsForeground() = false
IsByDgnIndex() = false

When the operation is S-"Set RGB Color":


Entry a red value [0...255]:>10
Entry a green value [0...255]:>34
Entry a blue value [0...255]:>19
Color value [c20a2213]

When the operation is C-"Print RGBMI-components":


Method = 2, Index = 108, Red = 10, Green = 34, Blue = 19

When the operation is H-"Set color hex value (7 digits)":


Entry seven hexadecimal digits:>50000A8
Color value [c3000007]

Color value [c3000007] Information:
Color method 3 is by ACI, Index = 7

When the operation is N-"Get Index for RGB-value":


Entry a red value:>20
Entry a green value:>60
Entry a blue value:>90
Index = 159

When the operation is U-"Get RGB-value for Index":


Entry an index:>100
RGB-value = ff3f

When the operation is E-"Compare two color instances":


Entry seven hexadecimal digits for color value to be compared:>3000007
Compare [c3000007] == [c3000007] => true

Entry seven hexadecimal digits for color value to be compared:>20A0B0C
Compare [c3000007] == [c20A0B0C] => false

Use the Q-"Quit" operation to exit.

See Also

Methods of the Color Definition

Color Functionality as an RGB-value

Color Functionality as an ACI-value

Color Functionality as an Integer-value

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