Sample Maplet Application: Get Color
This worksheet demonstrates how to write Maplet applications that function similarly to the Get Color Maplet application available in the Maplets[Examples] package. It is designed for experienced Maple authors.
Simple Example
restart:
# Invoke the Maplets Elements subpackage. with( Maplets[Elements] ):
# Define the Maplet application. maplet := Maplet( # Create a standard color dialog window # from which the user can select a color. ColorDialog['CD1']( 'onapprove' = Shutdown(['CD1']), 'oncancel' = Shutdown() ) ): # Run the Maplet application. Maplets[Display](maplet):
Refining the Simple Example
To refine the Maplet application:
1. Add a title to the Maplet application.
2. Return a default value if the user does not select a color.
By default, the title of the color dialog is Select Color. A title is added by giving the ColorDialog element a title attribute. If the user selects a color, the color is returned in the form "#RRGGBB" where RR, GG, and BB are hexadecimal numbers between 00 and FF.
with(Maplets[Elements]): maplet := Maplet( ColorDialog['CD1']( 'onapprove' = Shutdown(['CD1']), 'oncancel' = Shutdown(), 'title' = "Select a color" ) ): # Run the Maplet application. Assign result to the value returned by # the Maplet application. result := Maplets[Display](maplet): # Specify the default value if no color is selected. if result = NULL then "#FF0000"; # red else result[1]; end if;
Using Maplet Applications in Modules
In the following example, the Maplet application is assigned to a local variable when the module is created. This reduces the need for the Maplet application to be constructed each time the procedure MyProc is called and results in faster response times. Any selected color is then converted into a COLOR( RGB, r, g, b ) data structure where r, g, and b are values between 0 and 255.
# Define the module MyModule. MyModule := module() # Declare the module as a package and # protect its exports. option package; # Declare mymaplet as a local variable. local mymaplet; # Declare MyProc as an export. export MyProc; # Invoke the Maplets Elements subpackage. use Maplets[Elements] in # Define the Maplet application mymaplet. mymaplet := Maplet( # Call ColorDialog. ColorDialog['CD1']( 'onapprove' = Shutdown(['CD1']), 'oncancel' = Shutdown(), 'title' = "Select a color" ) ): end use; # Define the procedure MyProc. MyProc := proc() local result; # Run the Maplet application. Assign result to the value # returned by the Maplet application. result := Maplets[Display]( mymaplet ); if result = NULL then 'COLOR( RGB, 1, 0, 0 )'; # default color else result := result[1]; # Convert the color into a COLOR(RGB, r, g, b) # data structure. 'COLOR'( 'RGB', convert( result[2..3], 'decimal', 16 )/255, convert( result[4..5], 'decimal', 16 )/255, convert( result[6..7], 'decimal', 16 )/255 ); end if; end proc; end module:
# Invoke the module MyModule. with(MyModule):
# Call the procedure MyProc. MyProc();
Maplets[Examples][GetColor]
The Maplets[Examples][GetColor] function displays a Maplet application similar to that of the previous examples. The function, however, allows the user to choose from a variety of possible colors.
For help on this Maplet application, see:
?Maplets[Examples][GetColor]
To view the source code, enter:
print( Maplets[Examples][GetColor] );
See Also
Maplets[Examples], Maplets[Examples][GetColor], Maplets[Elements][ColorDialog]
Return to Index for Example Worksheets
Download Help Document