PDF In-Depth

JavaScript - Debugging Colors

April 01, 2001

Advertisement
Advertisement
 

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().

PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Back to the past, 15 years ago! Open Publish 2002

Looking back to 2002, it's amazing how much of the prediction became a reality. Take a read and see what you think!

September 14, 2017
Platinum Sponsor





Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

Features

Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.