Drawings SDK Developer Guide > Working with .dwg Files > Working with Colors > Example of Working with Transparency
Example of Working with Transparency

For working with transparency, this example uses the console menu that represents the following operations: specifying a transparency method, setting an alpha value, setting a percent of the transparency, getting information about a transparency, verifying a transparency method, entering a transparency as an integer value, and comparing two transparency 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 transparency component (alpha or percent), it checks whether the entered value is inside the scope 0...255 or the scope 0...100%. The main() function uses the setAlpha() method to set an alpha value, the setAlphaPercent() method to set a percent of the transparency, the setMethod() method to set a transparency method, and the serializeIn() method to set a transparency as an integer value. The main() function prints the result and uses the serializeOut() method to get the transparency as an integer value, the alpha() method to get the alpha value, the alphaPercent() method to get the percent of the transparency, and the method() method to get the current transparency method. The transparencyMethod enumerator defines the available transparency methods.

The Main module has the following implementation:


#include "OdaCommon.h"
#include "OdToolKit.h"
#include "..\..\Teigha.dwg\Extensions\ExServices\ExHostAppServices.h"
#include "..\..\Teigha.dwg\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 value;
   double percent;
   OdCmTransparency clarity;

   do {
     wcout << L"\nT. Set Alpha component";
     wcout << L"\nR. Set Alpha component in percent";
     wcout << L"\nM. Specify transparency method";
     wcout << L"\nP. Print transparency information";
     wcout << L"\nE. Compare two transparency instances";
     wcout << L"\nH. Set transparency hex value (7 digits)";
     wcout << L"\nV. Verify transparency method using isBy()";
     wcout << L"\nQ. Quit";
     wcout << L"\nSelect operation:>";
     ch = wcin.get();

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

         if(!wcin.fail())
           if(value >= 0 && value <= 255)
           {
              clarity.setAlpha(value);
              wcout << L"Transparency value [" << hex << clarity.serializeOut() << dec << L"]\n";
           }
           else wcout << L"Transparency value is outside the scope 0...255\n";
         else {  wcin.clear();  wcout << L"Invalid entered transparency value.\n";  }
         break;

       case L'R':
       case L'r':
         wcout << L"\nEntry a transparency value in percent [0...100%]:>" ;
         wcin >> percent;

         if(!wcin.fail())
           if(percent >= 0.0 && percent <= 100.0)
           {
              clarity.setAlphaPercent(percent / 100.0);
              wcout << L"Transparency value [" << hex << clarity.serializeOut() << dec << L"]\n";
           }
           else wcout << L"Percent value is outside the scope 0...100%\n";
         else {  wcin.clear();  wcout << L"Invalid entered transparency value.\n";  }
         break;

       case L'M':
       case L'm':
         wcout << L"\nMethod: 0-ByLayer, 1-ByBlock, 2-ByAlpha";
         wcout << L"\nSpecify the transparency method:>" ;
         wcin >> value;

         if(!wcin.fail())
           if(value == 0 || value == 1 || value == 2)
           {
              clarity.setMethod((OdCmTransparency::transparencyMethod)value);
              wcout << L"Transparency value [" << hex << clarity.serializeOut() << dec << L"]\n";
           }
           else wcout << L"Undefined transparency method.\n";
         else {  wcin.clear();  wcout << L"Invalid entered method number.\n";  }
         break;

       case L'P':
       case L'p':
         wcout << L"\nTransparency value [" << hex << clarity.serializeOut() << dec << L"] Information:"
               << L"\nTransparency method " << clarity.method() << L" is ";
         switch(clarity.method())
         {
           case OdCmTransparency::kByLayer:
             wcout << L"by Layer\n";
             break;
           case OdCmTransparency::kByBlock:
             wcout << L"by Block\n";
             break;
           case OdCmTransparency::kByAlpha:
             wcout << L"by Alpha, Alpha = " << clarity.alpha() 
                   << L" (" << clarity.alphaPercent() * 100 << L"%)\n"; 
             break;
         }
         break;

       case L'V':
       case L'v':
         wcout << L"\nIsClear() = " << ((clarity.isClear()) ? L"true" : L"false") 
               << L"\nIsSolid() = " << ((clarity.isSolid()) ? L"true" : L"false")
               << L"\nIsByAlpha() = " << ((clarity.isByAlpha()) ? L"true" : L"false")
               << L"\nIsByLayer() = " << ((clarity.isByLayer()) ? L"true" : L"false")
               << L"\nIsByBlock() = " << ((clarity.isByBlock()) ? 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 {
             clarity.serializeIn(value & 0x0FFFFFFF);
             wcout << L"Transparency value [" << hex << clarity.serializeOut() << 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 transparency value to be compared:>" ;
         wcin >> hex >> value >> dec;

         if(!wcin.fail())
           try {
             OdCmTransparency xclarity;
             xclarity.serializeIn(value & 0x0FFFFFFF);

             wcout << L"Compare [" << hex << clarity.serializeOut()  
                   << L"] == ["<< xclarity.serializeOut() << L"] => " << dec
                   << ((clarity == xclarity) ? 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:


T. Set Alpha component
R. Set Alpha component in percent
M. Specify transparency method
P. Print transparency information
E. Compare two transparency instances
H. Set transparency hex value (7 digits)
V. Verify transparency method using isBy()
Q. Quit
Select operation:>

When the operation is T-"Set Alpha component":


Entry a transparency value [0...255]:>20
Transparency value [2000014]

When the operation is R-"Set Alpha component in percent":


Entry a transparency value in percent [0...100%]:>80.5
Transparency value [20000cd]

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


Transparency value [20000cd] Information:
Transparency method 2 is by Alpha, Alpha = 205 (80.3922%)

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


Method: 0-ByLayer, 1-ByBlock, 2-ByAlpha
Specify the transparency method:>1
Transparency value [1000000]

Transparency value [1000000] Information:
Transparency method 1 is by Block

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


IsClear() = false
IsSolid() = false
IsByAlpha() = false
IsByLayer() = false
IsByBlock() = true

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


Entry seven hexadecimal digits:>20000FF
Transparency value [20000ff]

Transparency value [20000ff] Information:
Transparency method 2 is by Alpha, Alpha = 255 (100%)

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


IsClear() = false
IsSolid() = true
IsByAlpha() = true
IsByLayer() = false
IsByBlock() = false

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


Entry seven hexadecimal digits for transparency value to be compared:>20000FF
Compare [20000ff] == [20000ff] => true

Entry seven hexadecimal digits for transparency value to be compared:>200001A
Compare [20000ff] == [200001A] => false

Use the Q-"Quit" operation to exit.

See Also

Methods of the Transparency Definition

Transparency Functionality as an Alpha-value

Transparency Functionality as an Integer-value

Example of Entering and Displaying for Transparency Objects

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