Drawings SDK Developer Guide > Working with .dwg Files > Working with Colors > Example of Entering and Displaying Transparency Objects
Example of Entering and Displaying Transparency Objects

The example demonstrates working with the transparency object, including display and modification of its properties. This example uses the console menu for the following operations: getting and setting the alpha value [A], percent of clarity [P], and selecting the transparency method [M].

The AboutTransparency() function displays information about the specified transparency value. The function requires one argument — a transparency value as the OdCmTransparency instance — and returns information about the passed value as an OdString value. The function gets the transparency definition method and uses the switch statement to select whether the method defines the transparency (byLayer, byBlock, or byAlpha). The function inserts the transparency method name in the resulting string. When the method is byAlpha, the function gets the alpha value as a percent using the alphaPercent() method and inserts it in the resulting string. If the transparency method is undefined, the function inserts the hexadecimal transparency value in the resulting string.

The AboutTransparency() function has the following implementation:


OdString AboutTransparency(OdCmTransparency clarity)
{
  OdString sAbout;
  switch(clarity.method())
  {
    case OdCmTransparency::kByLayer:
      sAbout = L"[byLayer]";
      break;
    case OdCmTransparency::kByBlock:
      sAbout = L"[byBlock]";
      break;
    case OdCmTransparency::kByAlpha:
      sAbout.format(L"[byAlpha=%.1f%%]", clarity.alphaPercent()*100);
      break;
    default:
      sAbout.format(L"[%x:???]", clarity.serializeOut());
  }
  return sAbout;
}

The EntryTransparencyValue() function edits the transparency value using the console menu and requires two arguments — a reference to the transparency value as the OdCmTransparency instance being used as a default value for editing and the definition status as a Boolean value which is True when any transparency method can be used for the transparency definition or False when only the byAlpha method can be used for the transparency definition. The function returns True when the transparency value is modified by the user, or False when the user cancels the entry. The first argument is mandatory; the second argument is optional and is True by default. The function passes the modified transparency value through the first argument. The function organizes a loop that inquires about the operation code and uses the switch statement to select whether the case must be performed. The function processes user actions in the loop until the user selects [Q] operation. The function displays the current transparency value in each iteration of the loop using the serializeOut() method that returns the transparency value as a hexadecimal number and the AboutTransparency() function that returns the string about the transparency definition.

When the user selects [A], the function inquires about the new alpha value as a positive integer value in the range 0 to 255. If an entered value is incorrect or out of the range, the function displays an error message and breaks to the console menu. If an entered value is correct, the function calls the setAlpha() method and passes it the entered value to set the clarity for the transparency object.

When the user selects [P], the function inquires about the new percent of clarity as a positive double value in the range 0 to 100%. If an entered value is incorrect or out of the range, the function displays an error message and breaks to the console menu. If an entered value is correct, the function calls the setAlphaPercent() method and passes it the entered value divided by 100 to set the clarity for the transparency object.

When the user selects [M], the function inquires about the transparency method as a positive integer value defined by the values: 0–byLayer, 1–byBlock, 2–byAlpha. If an entered value is incorrect, the function displays an error message and breaks to the console menu. If an entered value is correct, the function calls the setMethod() method and passes it the entered value converted to the OdCmTransparency::transparencyMethod enumerator. If the second argument is set to False, the function ignores the [M] operation.

When the user selects [S], the function breaks the loop, passes the modified transparency value to the calling function using the first argument, and returns True as a result. When the user selects [Q], the function breaks the loop, remains current transparency value without changes for the calling function, and returns False as a result.

The EntryTransparencyValue() function has the following implementation:


#include "OdaCommon.h"
#include "CmColor.h"
#include <iostream>
using namespace std;

bool EntryTransparencyValue(OdCmTransparency& refClarity, bool isAnyMethod = true)
{
  wchar_t ch = L'\0';
  double percent;
  OdInt16 value;
  OdCmTransparency clarity = refClarity;

  wcout << L"Start modifying of the transparency properties\n";
  do {
    switch(ch)
    {
      case L'A':          // Entry the Alpha component
      case L'a':
        wcout << L"\nEntry a value of transparency [0...255]:>" ;
        wcin >> value;

        if(!wcin.fail() && wcin.peek() == 10)
          if(value >= 0 && value <= 255)
          {
            clarity.setAlpha((OdUInt8)value);
          }
          else wcout << L"Error: Transparency value is outside the scope 0...255\n";
        else { wcin.clear();  wcin.sync();  wcout << L"Error: Invalid entered value\n"; }
        break;


      case L'P':          // Entry the Percent of transparency
      case L'p':
        wcout << L"\nEntry a percent of transparency [0...100%]:>" ;
        wcin >> percent;

        if(!wcin.fail() && wcin.peek() == 10)
          if(percent >= 0.0 && percent <= 100.0)
          {
            clarity.setAlphaPercent(percent / 100.0);
          }
          else wcout << L"Error: Percent value is outside the scope 0...100%\n";
        else {  wcin.clear();  wcin.sync();  wcout << L"Error: Invalid entered value\n";  }
        break;


      case L'M':          // Set the transparency method
      case L'm':
        if(!isAnyMethod) break;
        wcout << L"\nMethod: 0-ByLayer, 1-ByBlock, 2-ByAlpha:>";
        wcin >> value;

        if(!wcin.fail() && wcin.peek() == 10)
          if(value == 0 || value == 1 || value == 2)
          {
            clarity.setMethod((OdCmTransparency::transparencyMethod)value);
          }
          else wcout << L"Error: Undefined transparency method\n";
        else { wcin.clear();  wcin.sync();  wcout << L"Error: Invalid entered value\n"; }
        break;


      case L'S':          // Save and Exit
      case L's':
        refClarity = clarity;
        return true;


      case L'\0':         // Skip an action
        break;

      default:
        wcout << L"Error: Incorrect operation\n";
    }
    wcout << L"\nCurrent transparency value: [" << hex << clarity.serializeOut() << dec 
          << L"] = " << AboutTransparency(clarity);
    
    if(isAnyMethod) wcout << L"\nM. Set the transparency method";
    wcout << L"\nA. Entry the Alpha value";
    wcout << L"\nP. Entry the Percent value";
    wcout << L"\nQ. Quit without saving";
    wcout << L"\nS. Save and Exit";
    wcout << L"\nSelect operation:>";
    wcin >> ch;
  }
  while(ch != L'Q' && ch != L'q');

  wcout << L"Stop modifying of the transparency properties\n";
  return clarity;
}

Testing gives the following results:


Start modifying of the transparency properties

Current transparency value: [20000ff] = [byAlpha=100.0%]
M. Set the transparency method
A. Entry the Alpha value
P. Entry the Percent value
Q. Quit without saving
S. Save and Exit
Select operation:>

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


Method: 0-ByLayer, 1-ByBlock, 2-ByAlpha:>1
Current transparency value: [1000000] = [byBlock]

When the operation is A-"Enter the Alpha value":


Entry a value of transparency [0...255]:>65
Current transparency value: [2000041] = [byAlpha=25.5%]

When the operation is P-"Enter the Percent value":


Entry a percent of transparency [0...100%]:>50.5
Current transparency value: [2000080] = [byAlpha=50.2%]

Use the Q-"Quit" operation to exit:


Stop modifying of the transparency properties

The AboutTransparency() and EntryTransparencyValue() functions are used in the documented examples that require display and entry of a transparency value.

See Also

Working with Colors and Transparencies

Methods of the Transparency Definition

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