setColor() - Media Method
The setColor() method sets the background color, foreground color, and transparency state of an image. Very few formats support saving of this information, so this function is primarily used in internal calculations in conjunction with other functions, such as arc()and drawText(), and supports the CMYK colorspace.
When an image is initially loaded into memory, the foreground and background colors are initialized according to the following order of precedence:
» For indexed images:
• Background color will be index 0.
• Foreground color will be the last indexed color.
» For all other images:
• Background color will be black.
• Foreground color will be white.
Note: If the image’s file type supports them and its background, transparency and/or foreground colors have been set, those values will be used.
Unless specifically changed, the initial values will be retained and used throughout all subsequent transformations. To be sure of the values used, it is best to use specific settings.
Syntax
setColor(
[BackColor @ <color in hexadecimal, rgb, or cmyk>]
[ForeColor @ <color in hexadecimal, rgb, or cmyk>]
[BackIndex @ <value 0..16777215>]
[ForeIndex @ <value 0..16777215>]
[Transparency @ (true, false)]
[Popular @ (true, false)]
[Precise <true, false>]
[layers @ <"layer list">] // (PSD files only)
);
Parameters
Backcolor - specifies the background color.
Forecolor - specifies the foreground color.
This parameter supports a hexidecimal, RGB, or CMYK color specification:
» hexidecimal - color value expressed as a value from 0x000000 to 0xFFFFFF (RGB colorspace) or from 0x00000000 to 0xFFFFFFFF (CMYK colorspace)
» RGB - color value expressed as a value from 0 to 16,777,215
» CMYK - color value expressed as a value from 0 to 4,294,967,295
Colorspace
Always pass a color value appropriate to the colorspace. You can ensure this using the getPixelFormat() function in your script and then using different hexadecimal values for the RGB and the CMYK colorspaces in an IF/THEN construction. If getPixelFormat() returns "CMYK," use the CMYK value (0x plus eight more digits), and otherwise use the RGB value (0x plus six more digits).
Backindex - specifies the background color as an index value. Direct indexing is primarily used for indexed images, but can be used for any image type to select a specific pixel value.
Foreindex - specifies the foreground color as an index value.
Important: You cannot specify values for both the Backcolor and Backindex parameters or for both the Forecolor and Foreindex parameters.
Transparency - if this parameter is set to false, the whole image is considered opaque. If set to true, the pixels in the image that match the background color are considered transparent. Transparency is typically used when generating an alpha channel for an image (such as compositing an image that is not 32-bit). Transparency is also supported when saving to the GIF format and, if 8-bit or less, to the PNG format.
Popular - if set to true, finds the most popular color or index in the image. For images above 16-bit color depth, the image is processed at 18-bit resolution.
Note: The Popular parameter overrides any settings specified by the Backcolor, Forecolor, Backindex or Foreindex parameter. In addition, this parameter does not support the CMYK colorspace.
Precise - If Popular is specified and this is set to true, the method uses precision in the calculation of the most popular color. If set to false (default), the color returned will be a close approximation of the actual color that appears most often in the image.
layers - for PSD files, specifies the layers to be included. The layer numbers begin at 0 (background) and go up. For more information see load() - Media Method.
Example
var image = new Media();
image.load(name @ "car.tga");
image.setColor(backcolor @ 0xC2270B);
image.crop(alg @ "backcolor");
image.save(type @ "jpeg", compressed @ true);
On-Line Documentation