script to color change

Explorer ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

I'm trying to change field colors with loop but it doesn't work.

I used this script as calculation

for(var i=1; i<=3; i++){
if(color.equal(this.getField("c"+i).fillColor, color.yellow))
this.getField("Text"+i).fillColor = color.yellow;}

 

I'm not getting any errors.

TOPICS
JavaScript

Views

74

Likes

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 3 Correct answers

Adobe Community Professional , Jun 14, 2022 Jun 14, 2022

Run this code to see the reason for this issue:

 

this.getField("Text1").fillColor=["CMYK",0,.03,1,0];

console.println(this.getField("Text1").fillColor);

Likes

Translate

Translate
Adobe Community Professional , Jun 14, 2022 Jun 14, 2022

When using precision calculator, try setting decimals to the same number of decimals you get from that script that try67 provided.

TIP: you can also use same method for RGB colors too.

Likes

Translate

Translate
Adobe Community Professional , Jun 15, 2022 Jun 15, 2022

You can do it like this:

var c1 = ["CMYK",0,.03,1,0];
this.getField("Text1").fillColor=c1;
var c2 = this.getField("Text1").fillColor;
console.println(color.equal(c1,c2)); // returns false
for (var i=1; i<c1.length; i++) c1[i]=Number(c1[i].toFixed(2));
for (var i=1; i<c2.length; i++) c2[i]=Number(c2[i].toFixed(2));
console.println(color.equal(c1,c2)); // returns true, after the values have been rounded

Likes

Translate

Translate
Adobe Community Professional ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

How did you apply to fill color to the "c" fields originally?

My guess is it's an RGB value, while color.yellow returns a CMYK value. And while the equal method should (in principle) know how to compare colors of different spaces it often fails to do so due to rounding issues.

Likes

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 ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

Are the fields concerned definitely THIS shade of yellow - rather than some other similar yellow. (Really, that is, was the existing colour set in JavaScript to color.yellow)?

Likes

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
Explorer ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

I was using rgb.

I tried RGB with RGB not working.

color.yellow and color.yellow works.

But I want to use different shades instead of predefined colors so I tried:

["CMYK",0,0,1,0 ] vs ["CMYK",0,0,1,0 ] and it works but that is also predefined yellow, while

["CMYK",0,.03,1,0 ] vs ["CMYK",0,.03,1,0 ] doesn't work?

 

Likes

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
Adobe Community Professional ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

Run this code to see the reason for this issue:

 

this.getField("Text1").fillColor=["CMYK",0,.03,1,0];

console.println(this.getField("Text1").fillColor);

Likes

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
Explorer ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

Setting it to that number did worked, but how to calculate that without running that line?

Likes

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
Adobe Community Professional ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

You have to manually round the values in the color array, before comparing the two colors.

Likes

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
Explorer ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

How would I do that?

I tried using precision calculator but result was not the same as from the script above and it didn't work.

Is there some formula to calculate?

Likes

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
Adobe Community Professional ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

When using precision calculator, try setting decimals to the same number of decimals you get from that script that try67 provided.

TIP: you can also use same method for RGB colors too.

Likes

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
Adobe Community Professional ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

LATEST

You can do it like this:

var c1 = ["CMYK",0,.03,1,0];
this.getField("Text1").fillColor=c1;
var c2 = this.getField("Text1").fillColor;
console.println(color.equal(c1,c2)); // returns false
for (var i=1; i<c1.length; i++) c1[i]=Number(c1[i].toFixed(2));
for (var i=1; i<c2.length; i++) c2[i]=Number(c2[i].toFixed(2));
console.println(color.equal(c1,c2)); // returns true, after the values have been rounded

Likes

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