MainCard has three scripts associated with it: AfterAttachment, BeforeDetachment, and MessageFromSubDeck. When the Colors deck is first activated, MainCard is attached, and its AfterAttachment script is executed. This script first determines the number of colors available to the card via the system variable, WindowColors. Because color numbers start at zero, one is subtracted from this number and assigned to the variable MaxColor. Thus, for a 32-color window, MaxColor equals 31.
Next, the value of the variable, Invocation, is checked. This variable will be equal to zero when MainCard is attached for the first time because all unassigned numerical variables default to a value of zero. Invocation is assigned a value of one later in the script. When Invocation equals zero, the global routine GetColors is executed. This routine simply assigns the red, green, and blue component values of each color in the palette to arrays named Red, Green, and Blue. Next, the window colors are set based on these array variables. Why bother determining color values and then immediately setting them to what they already are? Well, it turns out that whenever a card is attached, CanDo always resets its colors to the default colors regardless of what they were when the card was detached. Therefore, if MainCard were just one card in a multi-card CanDo deck and the ColorChange deck were used to change its colors, these colors would be lost when the card is detached and later reattached. For this reason, the BeforeDetachment script executes the GetColors routine. This saves the current palette so it can be restored when MainCard is reattached. The color component values must also be determined the first time the card is attached to prevent all the colors from being set to black due to the Red, Green, and Blue array elements defaulting to zero before they are initialized. Also, within the same loop that sets the window colors, horizontal color bars are drawn.
When the ChangePalette card is about to terminate, a message is sent to the parent deck, in this case Colors. This activates the MessageFromSubDeck script. Up to ten parameters can be passed to this script; they are automatically assigned to the system variables Arg1-Arg10. Only one parameter is passed from the ChangePalette card, the string "Quit". The ClosePendingWindow command closes any requester windows that have terminated execution. Next, the FirstCard command is used to activate the first card in the deck. Since MainCard is the only card in the deck, it terminates and then reactivates. This, of course, is totally unnecessary, but it illustrates how the new colors for the card are saved as the card detaches and then reset as the card reattaches.
When the ChangeColors button is pressed, its OnRelease script loads the ColorChange deck into a buffer named CC. It then opens the ChangePalette card using the OpenRequester command. Alternatively, the OpenWindow command could be used. There is only one difference between these two commands. OpenRequester opens the subdeck card and prevents user interaction with the parent card until the subdeck is exited. OpenWindow opens the subdeck card and allows user interaction with it and the parent card simultaneously.
Listing 2 shows a print-out of the ColorChange program; Figure 2 shows it's interface (as it appears on the MainCard card from the Colors deck). This card's window settings are shown in Figure 3. The origin is set to (20,20) so it will be offset slightly when it appears on top of another CanDo card. It's size is 200x140, smaller than the lowest resolution of the Amiga (320x200). It is important that the window be set to open onto the current screen. If not, the ChangePalette card will appear on its own screen, obscuring the parent card that invoked it. It is also important to make the ChangePalette card a 2-color window. This allows the card to work with any parent card. When CanDo attempts to open a window onto the current screen, it checks the size of the window and its color depth. If the window is smaller and its color depth is less than the screen, CanDo opens the window onto the screen, increasing the window's color depth if necessary. If the color depth of the window is greater than the screen's depth, the window opens onto its own screen. Thus, by making ChangePalette a 2-color window, it will work with a parent card of any color depth.
The ChangePalette card consists of two area buttons, six text buttons, and three proportional sliders. The two area buttons, CurrentColor and Palette, are in the upper-left and upper-right corners of the card, respectively. CurrentColor is only used as a border for a color swatch of the current color and has no scripts associated with it. Palette is used as a container for swatches of each color in the palette.
Three of the text buttons, Copy, Exchange, and Spread, are located between the two area buttons. Copy is used to copy a color to another location in the palette. Exchange is used to exchange two colors in the palette. Spread is used to create graduated intermediate colors between two colors in the palette.
The remaining three text buttons, OK, Cancel, and Restore, are located at the bottom of the card. OK is used to accept the current palette and return to the parent card. Cancel is used to restore the original palette and return to the parent card. Restore is used to restore the original palette without returning to the parent card.
The three proportional sliders, Red, Green, and Blue, are located in the center of the card. They are used to adjust the red, green, and blue component values of the current color. The letters R, G, and B to the left of the sliders indicate which slider is used for each of the color components. Also, the numbers to the right of the sliders indicate the current value of each color component. These numbers can range from zero to 255.
The draw mode is set to JAM2 so that when the color values are printed next to the proportional sliders, the new numbers will completely overwrite the old ones. Next, the characters R, G, and B are printed to the left of the sliders. Finally, PenA is set to zero (background color) with the SetPen command, the global routines Box, SetProps, and GetColors are executed, and the variable PendingCommand is set equal to "None". The global routines and the variable PendingCommand are discussed below.
This routine first determines the row and column of the current color. Next, the coordinates of the upper-left corner of the rectangle are calculated. The current color is temporarily set to the value of Arg1, the rectangle is drawn, and the current color is reset to its original value. Notice in Figure 2 that the color swatches are separated from each other by one pixel. The rectangle that is drawn around the color swatch makes use of this space.
The pointer imagery changes after pressing the Copy button to provide visual feedback that an action is pending. The pointer imagery is shown in Figure 4A. The SetPointer command is used to display this pointer. It has one argument, a string containing the name of the brush file containing the pointer imagery. As will be seen later, the default pointer can be restored using the SetPointer command with no arguments.
At this point the script determines if there is a pending command by checking the value of the variable PendingCommand. If its value is "Copy", the RGB component values of the newly selected pen are set to the values of the current pen, effecting a copy of the color.
If PendingCommand is equal to "Exchange", the RGB component values of the newly selected pen are obtained and the current pen's values are set equal to them. Then, the newly selected pen's RGB component values are set equal to the original values of the current pen, thus effecting a swap in colors.
If PendingCommand is equal to "Spread", the RGB component values of the current color and the newly selected color are determined. The difference between each of the three component values is calculated and an increment is set for each based on the number of colors between the two selected colors. Then, all of the intermediate colors are set based on these increments.
After handling any pending commands, the newly selected pen is made the current pen using the SetPen command, PendingCommand is reset to "None", and the pointer is reset to its default imagery by issuing the SetPointer command with no arguments. Finally, the large color swatch in the CurrentColor button is updated, the proportional sliders are adjusted, and a rectangle is drawn around the new current color swatch.
The OnNewPosition script determines the new position of the slider's knob. The sliders are set to vary from zero to 255 (Figure 5). This range corresponds directly with the RGB component values of the colors in the palette.
The size of a slider knob is adjustable. The knobs for the sliders on this card are set to a size of 10. Be aware that the actual maximum value that a slider can have is determined by the left side of the knob. Therefore, the actual maximum value for a slider is Right-KnobSize+1 where Right and KnobSize are defined as shown in Figure 5. When the knob size is 10, the value of Right must be 264 for the maximum value of the slider to equal 255.
The position of a slider knob is determined with the GetPropPosition command. The OnNewPosition script uses this value to change the appropriate RGB component of the current color. Also, the slider value is printed to the right of the appropriate slider using the current color. This provides instant feedback of how the current color is changing with slider knob movement.
When the mouse button is released, the OnRelease script executes. It prints the final value of the slider. Also, just in case the slider was adjusted while a command was pending, PendingCommand is set equal to "None". Finally, the pointer's default imagery is restored.
SendToAmazingComputingEditor "Quit"