June 15, 2007
In AmiBroker, the colors of many of the chart elements can be customized from within simple AFL formulas. The most common application of this feature is when plotting indicators, where the third parameter of the Plot() function allows users to set the color to their own preference e.g. Plot(C,"Close",colorRed, styleLine).
A short hand method, to specify the color, is to substitute colorRed with the number 32, which is the color index number for Red e.g. Plot(C,"Close",32, styleLine). It can, however, be difficult to remember the color names and the index numbers. (Refer to the AFL Reference Manual reserved variables table for the complete list).
The Color-Picker, on the Format Toolbar, is used as a graphical method for setting the color of drawing objects placed on the chart, as well as chart plots. It can also act as a handy reminder tool when stipulating color preferences in formulas. A prompt appears, when the mouse is hovered over the Color-Picker, and provides the correct name for each color. The name can then be appended to the color parameter within formulas.
To use the Color-Picker as a name prompt:
a) open it from the drop-down on the Format Toolbar. (Note that the Color-Picker can be opened in the background, to act as a dynamic prompt, while working with the Formula Editor window open);
b) position the mouse over the color of choice.
c) replace Name, in colorName parameters, with the color title exactly as written e.g. colorName becomes colorRed. (Remove all gaps, hyphens and special characters e.g. Grey-50% becomes Grey50. Note that there are a few exceptions; Grey-80% becomes DarkGrey, Grey-25% becomes LightGrey, Light Green becomes PaleGreen and Light Turquoise becomes PaleTurquoise).
To use the Color-Picker as a color index prompt:
a) open the Color-Picker;
b) choose the preferred color, and obtain it’s index number, by counting to the right from the first number in each row (the index is zero based, and the first sixteen colors are reserved for custom colors, so the first row starts with 16 and the second starts with 16 + 8 = = 24 etc. (The color index number for Red, as the first color in the third row, is therefore 32).
Color indexing is a better method of changing colors, at the formula level, than using colorNames, as it is quicker and there are no exceptions to remember. As an example, the formula in the Editor image above, which was originally used in the Users’ Knowledge Base post ’How Many Trading Days In A Calendar Year?’ with colorBlack and styleLine settings, has been changed to a colorRed styleHistogram chart using the shorthand color index 32 (text highlighted in yellow).
The Color-Picker can be torn-off the toolbar if required:
a) drop the Color-Picker down;
b) click and hold the mouse pointer on the tear-off line;
c) when a moving pointer appears the window can be dragged to the desired position.