Copy link to clipboard
Copied
Is there a way to use javascript in Acrobat forms so that the form field "colour-one" is filled with whatever colour the user specifies in form field "RGB-values"?
For example, if users entered "203, 39, 47" in field "RGB-values", then "colour-one" would fill in red. If no value is entered in "RGB-values", then "colour-one" would remain transparent.
Thank you!
Copy link to clipboard
Copied
Place this script as a "Custom validation script" in the "RGB-values" field:
var aText = event.value.split(",");
if (aText.length > 0) {this.getField("colour-one").fillColor = ["RGB", Number(aText[0])/255, Number(aText[1])/255, Number(aText[2])/255];}
else {this.getField("colour-one").fillColor = color.transparent;}
This is just a sample, it didn't check if the user enter anything instead of the comma, and it didn't check if entered value are in the range 0-255.
Copy link to clipboard
Copied
You can specify colours in JavaScript. They must be in the range per the documentation: values are NOT in the range 0 to 255, but in the range 0.0 to 1.0.
Copy link to clipboard
Copied
Ok, so if a form user entered "203, 39, 47" ie a colour specified in RGB, is there a way for that colour to be translated to the 0.0-1.0 range per the documentation, and displayed in the form?
Copy link to clipboard
Copied
Color values can be used in a range of 0-255, JavaScript can make the conversion.
Try this, where orange values must be between 0 and 255:
app.runtimeHighlightColor = ["RGB", 204/255, 214/255, 255/255]; // default light blue
Copy link to clipboard
Copied
Sure, you (or a programmer you employ) just write the JavaScript code to split up the field based on whatever rules suit you (spaces, commas etc.), then divide each number by 255 and create a new colour array.
Copy link to clipboard
Copied
Place this script as a "Custom validation script" in the "RGB-values" field:
var aText = event.value.split(",");
if (aText.length > 0) {this.getField("colour-one").fillColor = ["RGB", Number(aText[0])/255, Number(aText[1])/255, Number(aText[2])/255];}
else {this.getField("colour-one").fillColor = color.transparent;}
This is just a sample, it didn't check if the user enter anything instead of the comma, and it didn't check if entered value are in the range 0-255.
Copy link to clipboard
Copied
This does precisely what I was needing - thank you so much @JR Boulay 🙂
Copy link to clipboard
Copied
Otherwise you can use a drop-down menu or a JavaScript popup menu that offer several predefined colors, as in this example (clik the red button to download):
https://www.abracadabrapdf.net/pdf-de-demo/formulaires/javascript-menus-pop-up/