The Color Object
Run the word color in the JavaScript console and it will return [object object]. Since Acrobat has a built-in object called color, you should never use the word color as a variable because that term is already taken and your variable will override the built-in color object, rendering it useless for the remainder of the session.
Run the following script in the console:
for(i in color)
{console.println(i)}
It will return the following list:
equal
convert
transparent
black
white
dkGray
gray
ltGray
red
green
blue
cyan
magenta
yellow
The first two in the list are functions. The color.equal function compares two color arrays to see if they are equal and returns true or false. Since color arrays can be expressed as RGB, CMYK, G, or T (transparent) the conversion is done if necessary. For example:
color.equal(["RGB",0.5,0.5,0.5],["G",0.5]);//returns true
color.equal(["RGB",0.5,0.5,0.5],["G",0.75])//returns false
The color.convert function converts one colorspace to another:
color.convert(["G",0.5],"RGB");//returns RGB,0.5,0.5,0.5
color.convert(["RGB",0.5,0.5,0.5],"G")//returns G,0.49999999999999994
To exclude functions from the list, run the following script in the console:
for(i in color)
{
if(typeof color[i]!="function")
{console.println(i)}
}
//returns the same list without equal and convert
Setting Colors
Some text field properties in which the color can be set are:
Fill color (fld.fillColor)
Border color (fld.strokeColor - replaced fld.borderColor which is discouraged but still works).
Text color (fld.textColor)
for which fld is a variable representing the field object. The following scripts will set the fill color of the field to the different colors in the list:
var fld=this.getField("Text1");
fld.fillColor=color.transparent;
fld.fillColor=color.black;
fld.fillColor=color.white;
fld.fillColor=color.dkGray;
fld.fillColor=color.gray;
fld.fillColor=color.ltGray;
fld.fillColor=color.red;
fld.fillColor=color.green;
fld.fillColor=color.blue;
fld.fillColor=color.cyan;
fld.fillColor=color.magenta;
fld.fillColor=color.yellow;
Object property names can also be called literally inside an array like this:
var fld=this.getField("Text1");
fld["fillColor"]=color["transparent"];
fld["fillColor"]=color["black"];
fld["fillColor"]=color["white"];
fld["fillColor"]=color["dkGray"];
fld["fillColor"]=color["gray"];
fld["fillColor"]=color["ltGray"];
fld["fillColor"]=color["red"];
fld["fillColor"]=color["green"];
fld["fillColor"]=color["blue"];
fld["fillColor"]=color["cyan"];
fld["fillColor"]=color["magenta"];
fld["fillColor"]=color["yellow"];
Accessing Any Color
Colors are not limited to those labeled in the color object as above. Any color in the color picker can used by getting the color array. Here are the steps:
In form editing mode, create a text field and open the field properties.
In the Appearance tab, click the Fill Color box then click More colors.
Set the desired color using the color picker.
Click OK and close the field properties.
Run the following script in the console:
this.getField("Text1").fillColor;
The script above, for the color in the image above will return RGB,0.7921600341796875,0.399993896484375,0.2078399658203125
To set the fill color of another field, use the following script:
this.getField("Text2").fillColor=["RGB",0.7921600341796875,0.399993896484375,0.2078399658203125];
In most cases, rounding the decimals to 3 decimal places will produce a color that is “close enough”:
this.getField("Text2").fillColor=["RGB",0.792,0.4,0.21];
Notice that numbers in the array do not match the Red, Green, and Blue numbers of 202, 102, and 53 from the screenshot? A lot of people make the mistake of trying to use those numbers in the array like this:
this.getField("Text2").fillColor=["RGB",202,102,53];
Usually, the result will be the fill color white. The script array numbers are actually fractional proportions of 255. In other words, the numbers in the screen shot should be divided by 255. Run these math equations in the console:
202/255;//returns 0.792156862745098
102/255;//returns 0.4
53/255;//returns 0.20784313725490197
Do these numbers look familiar? They should. When rounded, they are the same as the rounded result of running this.getField("Text1").fillColor in the console. The reason the pre-rounded numbers are not exactly the same is because the color picker numbers themselves have been rounded to whole numbers. If you are going to use the numbers from the color picker, simply insert division by 255 into each number of the array:
this.getField("Text1").fillColor=["RGB", 202/255, 102/255, 53/255];
When using the colors red, green, or blue the color is 255 in the color picker, or 1 in the script, and the other 2 are zero:
//Red
this.getField("Text1").fillColor=["RGB", 1, 0, 0];//or
this.getField("Text1").fillColor=["RGB", 255/255, 0/255, 0/255];
//Green
this.getField("Text1").fillColor=["RGB", 0, 1, 0];//or
this.getField("Text1").fillColor=["RGB", 0/255, 255/255, 0/255];
//Blue
this.getField("Text1").fillColor=["RGB", 0, 0, 1];//or
this.getField("Text1").fillColor=["RGB", 0/255, 0/255, 255/255];