Close

Relief for ODA Team in Ukraine

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

For working with database 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, setting a color name and book name, setting a keyword, getting a color name, book name, or keyword, 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 colorNames() method to set a color name and book name, the setNamesFromDictionaryKey() method to set a color keyword, 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, the colorName() method to get the color name, the colorBook() method to get the book name, the colorNameForDisplay() method to get the display name, the getDictionaryKey() method to get the color keyword, and the colorMethod() method to get the current color method. The ColorMethod enumerator defines the available color methods.

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, name[32], book[32], key[64];
   int red, green, blue, value;
   OdCmColor 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 RGBMI-components";
     wcout << L"\nC. Print color information";
     wcout << L"\nD. Set color keyword";
     wcout << L"\nN. Set color name and book";
     wcout << L"\nL. Clear color name and book";
     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':
         if(color.isByColor())
         {
           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";  }
         }
         else wcout << L"\nModifying a red component is correct only for byColor method\n";
         break;

       case L'G':
       case L'g':
         if(color.isByColor())
         {
           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";  }
         }
         else wcout << L"\nModifying a green component is correct only for byColor method\n";
         break;

       case L'B':
       case L'b':
         if(color.isByColor())
         {
           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";  }
         }
         else wcout << L"\nModifying a blue component is correct only for byColor method\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,";
         wcout << L"\n\t3-ByACI, 4-ByPen, 5-Foreground, 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 == 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::kNone:
             wcout << L"none\n";
             break;
         }
         break;

       case L'C':
       case L'c':
         wcout << L"\nEntity color = " << hex << color.entityColor().color() << dec
               << L"\nMethod = " << (color.colorMethod() & 0x0F)
               << L"\nIndex = " << color.colorIndex()
               << L"\nRed = " << color.red()
               << L", Green = " << color.green()
               << L", Blue = " << color.blue() 
               << L"\nName = " << color.colorName()
               << L"\nBook = " << color.bookName()
               << L"\nDictionaryKey = " << color.getDictionaryKey()
               << L"\nDisplayName = " << color.colorNameForDisplay() 
               << L"\nDescription = " << color.getDescription()
               << L"\nExplanation = " << color.getExplanation() << "\n";
         break;

       case L'N':
       case L'n':
         wcout << L"\nEntry a color name:>" ;
         wcin >> name;
         wcout << L"Entry a book name:>" ;
         wcin >> book;

         color.setNames(name, book);

         wcout << L"\nName = " << color.colorName()
               << L", Book = " << color.bookName() << L"\n";
         break;

       case L'L':
       case L'l':
         color.setNames(L"", L"");
         wcout << L"\nName = " << color.colorName()
               << L", Book = " << color.bookName() << L"\n";
         break;

       case L'D':
       case L'd':
         wcout << L"\nEntry a color keyword [book$name]:>" ;
         wcin >> key;

         color.setNamesFromDictionaryKey(key);

         wcout << L"\nName = " << color.colorName()
               << L", Book = " << color.bookName() << L"\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"\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 [book$name]-named color or [$]-unnamed color:>" ;
         wcin >> key;
         wcout << L"Entry seven hexadecimal digits for color value to be compared:>" ;
         wcin >> hex >> value >> dec;

         if(!wcin.fail())
           try {
             OdCmColor xcolor; 
             xcolor.setColor(value & 0x0FFFFFFF | 0xC0000000);

             if(!(key[0] == L'$' && key[1] == L'\0'))
               xcolor.setNamesFromDictionaryKey(key);

             wcout << L"\nCompare [" << hex << color.color() << "] (" << color.getDictionaryKey()
                   << ") == [" << xcolor.color() << "] (" << xcolor.getDictionaryKey() << ") => " 
                   << 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 RGBMI-components
C. Print color information
D. Set color keyword
N. Set color name and book
L. Clear color name and book
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 P-"Print RGBMI-components":


Color value [c0000000] Information:
Color method 0 is by Layer

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 RGBMI-components":


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

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


Modifying a red component is correct only for byColor method

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


Entry a red value [0...255]:>14
Entry a green value [0...255]:>26
Entry a blue value [0...255]:>21
Color value [c20e1a15]

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


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

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


Entry a red value [0...255]:>51
Color value [c2331a15]

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


Entry a green value [0...255]:>34
Color value [c2332215]

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


Entry a blue value [0...255]:>17
Color value [c2332211]

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


Color value [c2332211] Information:
Color method 2 is by RGB, Red = 51, Green = 34, Blue = 17

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


Entity color = c2332211
Method = 2
Index = 38
Red = 51, Green = 34, Blue = 17
Name = 
Book = 
DictionaryKey = 
DisplayName = 51,34,17
Description =
Explanation =

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


Entry [book$name]-named color or [$]-unnamed color:>$
Entry seven hexadecimal digits for color value to be compared:>2332211

Compare [c2332211] () == [c2332211] () => true

Entry [book$name]-named color or [$]-unnamed color:>$
Entry seven hexadecimal digits for color value to be compared:>2112233

Compare [c2332211] () == [c2112233] () => false

Entry [book$name]-named color or [$]-unnamed color:>mybook$aaa
Entry seven hexadecimal digits for color value to be compared:>2332211

Compare [c2332211] () == [c2332211] (mybook$aaa) => false

When the operation is N-"Set color name and book":


Entry a color name:>aaa
Entry a book name:>mybook

Name = aaa, Book = mybook

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


Entity color = c2332211
Method = 2
Index = 38
Red = 51, Green = 34, Blue = 17
Name = aaa
Book = mybook
DictionaryKey = mybook$aaa
DisplayName = aaa
Description =
Explanation =

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


Entry [book$name]-named color or [$]-unnamed color:>mybook$aaa
Entry seven hexadecimal digits for color value to be compared:>2332211

Compare [c2332211] (mybook$aaa) == [c2332211] (mybook$aaa) => true

Entry [book$name]-named color or [$]-unnamed color:>mybook$aaa
Entry seven hexadecimal digits for color value to be compared:>2112233

Compare [c2332211] (mybook$aaa) == [c2112233] (mybook$aaa) => false

Entry [book$name]-named color or [$]-unnamed color:>mybook$bbb
Entry seven hexadecimal digits for color value to be compared:>2332211

Compare [c2332211] (mybook$aaa) == [c2332211] (mybook$bbb) => false

Entry [book$name]-named color or [$]-unnamed color:>$aaa
Entry seven hexadecimal digits for color value to be compared:>2332211

Compare [c2332211] (mybook$aaa) == [c2332211] (UNNAMED$aaa) => false

Entry [book$name]-named color or [$]-unnamed color:>$
Entry seven hexadecimal digits for color value to be compared:>2332211

Compare [c2332211] (mybook$aaa) == [c2332211] () => false

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


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

When the operation is D-"Set color keyword":


Entry a color keyword [book$name]:>xbook$ccc

Name = ccc, Book = xbook

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


Entity color = c1000000
Method = 1
Index = 0
Red = 0, Green = 0, Blue = 0
Name = ccc
Book = xbook
DictionaryKey = xbook$ccc
DisplayName = ccc
Description =
Explanation =

When the operation is L-"Clear color name and book":


Name = , Book = 

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


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

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


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

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


Entity color = c3000007
Method = 3
Index = 0
Red = 255, Green = 255, Blue = 255
Name =
Book =
DictionaryKey = 
DisplayName = white
Description =
Explanation =

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


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

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


Entity color = c0000000
Method = 0
Index = 256
Red = 0, Green = 0, Blue = 0
Name =
Book =
DictionaryKey =
DisplayName = BYLAYER
Description =
Explanation =

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


Entry seven hexadecimal digits:>30000A8
Color value [c30000A8]

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


Entity color = c30000a8
Method = 3
Index = 168
Red = 0, Green = 19, Blue = 76
Name =
Book =
DictionaryKey =
DisplayName = 168
Description =
Explanation =

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

Color Functionality as a Book Name

Example of Entering and Displaying for Color Objects

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