Skip to main content
Participant
February 4, 2021
Answered

User-entered RGB value to fill colour form field

  • February 4, 2021
  • 4 replies
  • 2105 views

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!

This topic has been closed for replies.
Correct answer JR Boulay

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.

 

4 replies

JR Boulay
Community Expert
Community Expert
February 4, 2021

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/

 

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
February 4, 2021

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.

 

Acrobate du PDF, InDesigner et Photoshopographe
Sarah5C7CAuthor
Participant
February 4, 2021

This does precisely what I was needing - thank you so much @JR Boulay 🙂

Legend
February 4, 2021

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. 

Legend
February 4, 2021

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. 

Sarah5C7CAuthor
Participant
February 4, 2021

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?