• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

User-entered RGB value to fill colour form field

Community Beginner ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

Screen Shot 2021-02-04 at 11.44.31.pngIs 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!

TOPICS
How to , JavaScript , PDF forms

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 04, 2021 Feb 04, 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.

 

Capture_159.png

Votes

Translate

Translate
LEGEND ,
Feb 04, 2021 Feb 04, 2021

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2021 Feb 04, 2021

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 04, 2021 Feb 04, 2021

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

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.

 

Capture_159.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 04, 2021 Feb 04, 2021

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 04, 2021 Feb 04, 2021

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/

 

Capture_160.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines