JavaScript – Debugging Colors

Somebody posted a question recently about a situation where, in a form, the user was given a list of colors to choose from, and upon selecting one, the function in question returned something like ‘color.green’ or ‘color.red’, but when that was used as input to another line of code, everything seemed to not work any more. Here is what it looked like:



var userColor = app.popUpMenu(); // returns, say,

'color.red'this.getField('mainField').textColor = userColor;// WRONG! Error at

runtime

Function return value

The problem is that popUpMenu() can only return a String. It can’t return a Color

(which is an array of floating point values). To fix this requires that we convert the

output of popUpMenu() to an array of floats representing the proper color. This could

call for messy array methods or conversion tables, but in this instance, luckily, the

programmer had set things up so that all the return values were ‘color.red’ and

‘color.green’ and other predefined colors from AForm.js (one of the scripts that is

loaded every time you run Reader or Acrobat). Therefore, fixing this problem was easy. It’s

just a matter of wrapping the output from popUpMenu() in an eval() function:


var userColor = app.popUpMenu(); // returns, say,

'color.red'this.getField('mainField').textColor = eval(userColor);// YES! Works

now.

It works now because eval() forces a lookup of whatever the color happens to be

defined as in terms of its floating-point values. (They are all given in AForm.js; I

recommend you find and study the contents of that file some day if you haven’t done so

already.

Yet another example of the joys of eval().

You May Also Like

About the Author: Kas Thomas

Leave a Reply