These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Using the switch statement.... Assistance with dynamic colorFill of text field

thegreatwhay
Registered: Nov 23 2011
Posts: 4

Good evening. I am new to the forum, so please bare with me if I talk 'stupid'.
I have a form that I want to have a set of fields populate with a color that corresponds to a color. For whatever reason, everything I have tried does not work.
 
Here is something that will work...but only for one entry.
 
var height = this.getField("Text2");
 
height.strokeColor = ["RGB",0.5,0.5,1]; //Change border to dark blue
height.fillColor = ["RGB",0,0,0.7]; //Change fill to light blue
height.textColor = ["RGB",0.5,0.5,0]; //Change text to light yellow
   
var heightEntered = this.getField("Text2");
var height = parseInt(heightEntered);
switch(height) {
case 5:
height.strokeColor = ["RGB",0.5,0.5,1]; //Change border to dark blue
height.fillColor = ["RGB",0,0,0.7]; //Change fill to light blue
height.textColor = ["RGB",0.5,0.5,0]; //Change text to light yellow
break;
case 10:
height.strokeColor = ["RGB",0.5,0.5,1]; //Change border to dark blue
height.fillColor = ["RGB",0,0,0.7]; //Change fill to light blue
height.textColor = ["RGB",0.5,0.5,0]; //Change text to light yellow
break;
default:
height.strokeColor = ["RGB",0.8,0.5,1]; //Change border to dark blue
height.fillColor = ["G", 1]; //Change fill to light blue
height.textColor = ["G", 0]; //Change text to light yellow
break;
}

   
I have tried so many variations, and I am fearful that maybe I am misunderstanding colors themselves.
I.E.- I want to do the color arrangements in CMYK.. The commented part to the right are the different case's that will exist...
 
aqua = ["CMYK", 76, 6, 37, 0]; //5,15,25,35,45,55
teal = ["CMYK", 72, 0, 60, 0]; //6,16,26,36,46,56
magenta = ["CMYK", 39, 100, 8, 1]; //7,17,27,37,47,57
lightblue = ["CMYK", 56, 14, 0, 0]; //8,18,28,38,48,58
green = ["CMYK", 82, 16, 100, 3]; //9,19,29,39,49,59
bronze = ["CMYK", 24, 58, 100, 9]; //0,10,20,30,40,50,60
vectorpurple = ["CMYK", 83, 100, 15, 10]; //11,21,31,41,51
gray = ["CMYK", 55, 54, 62, 30]; //12,22,32,42,52
darkblue = ["CMYK", 100, 80, 47, 51]; //13,23,33,43,53
gold = ["CMYK", 4, 5, 92, 0]; //14,24,34,44,54
 
Does anyone have any input they could give me, or any direction they can send me so I can find the answers to this...I am hoping to figure this out before this Friday night! 11/25/2011
 
Thanks in advance to all you JavaScript/Adobe geniuses.
 
J

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
The first line of code sets the "height" variable to the "Text2" field object, but later on in the script you're assigning it to something else. It seems like you want to get the field value, so change the lines to:

// Get the value that the user entered and convert it to an integer
var heightEntered = parseInt(this.getField("Text2").value);

switch (heightEntered) {
thegreatwhay
Registered: Nov 23 2011
Posts: 4
George,
Thanks so much for the response. After making the suggested change, things still do not work. I am so curious as to a better understanding of how to apply the code. I am going into properties of the text field, first choosing the calculate tab. There I am making the value as the calculated sum of another field. I then click the validate tab, and place the code we are discussing above in the Run custom validation script...
Is this the correct approach? One thing I need to clarify, is that the codes above are two different scripts...the first short one, works... the second one, does not work...just in case I am miscommunicating something.


try67
Expert
Registered: Oct 30 2008
Posts: 2398
If this code is present in the Text2 field, then you need to change the line George provided you with to:
var heightEntered = parseInt(event.value);

Also, it might be a good idea to publish your entire code, as you might have other errors.
And did you make sure to enable the option to show the console on warnings and errors in the preferences? It can be very useful when something is not working properly.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thegreatwhay
Registered: Nov 23 2011
Posts: 4
try67,
Much appreciated. Yeah this is all to me, so for all I know, I may be missing something very basic. The entire code is:

var heightEntered = parseInt(event.value);
if( isNaN(heightEntered) ){
alert("Please enter a valid number")
} else {
switch (heightEntered) {
case 5:
heightEntered.strokeColor = ["CMYK", 4, 5, 92, 0]; //Change border to dark blue
heightEntered.fillColor = ["CMYK", 55, 54, 62, 30]; //Change fill to light blue
heightEntered.textColor = ["CMYK", 56, 14, 0, 0]; //Change text to light yellow break;
default:
heightEntered.strokeColor = ["RGB",0.8,0.5,1]; //Change border to dark blue
heightEntered.fillColor = ["G", 1]; //Change fill to light blue
heightEntered.textColor = ["G", 0]; //Change text to light yellow
break;
}
}

I get an error of:
"ReferenceError: alert is not defined
5:Field:Validate"


Note that I am using full CMYK values, not 'percentage'. I am unsure of this conversion, but I notice that the RGB values are written as a percentage.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
There are various problems here...
The one reported by the console is because you're trying to access the alert method directly, which is wrong.
You have to access it through the app object, like so:
app.alert("some text");

Another problem is you are confusing between the different variables. heightEntered is a number. You can't change its color properties (in the switch), because it doesn't have any. You need to change the properties of the Field object.
In this case you can get access to this field either by using this.getField("Text2") or by using event.target.
So, for example:

  1. var f = event.target;
  2. switch (heightEntered) {
  3. case 5:
  4. f.strokeColor = ["CMYK", 4, 5, 92, 0]; //Change border to dark blue
  5. f.fillColor = ["CMYK", 55, 54, 62, 30]; //Change fill to light blue
  6. f.textColor = ["CMYK", 56, 14, 0, 0]; //Change text to light yellow break;
  7. break;
  8. default:
  9. f.strokeColor = ["RGB",0.8,0.5,1]; //Change border to dark blue
  10. f.fillColor = ["G", 1]; //Change fill to light blue
  11. f.textColor = ["G", 0]; //Change text to light yellow
  12. }
Notice you also must enter a break command at the end of each case, or it will "fall through" to the next one. There's no need to add break to the default case, though.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thegreatwhay
Registered: Nov 23 2011
Posts: 4
You are correct in the confusion. As a matter of fact, I am not sure how heightEntered plays into things now. Is this essentially what I am calling the switch function? I apologize for my ignorance. If I uploaded the document somewhere, would that be helpful? Email it to you?

try67
Expert
Registered: Oct 30 2008
Posts: 2398
If you want, you can email it to me, but also explain again what exactly you want to achieve.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com