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

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

695

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

Community Expert , 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);

Votes

Translate

Translate
Community Expert , 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.

Votes

Translate

Translate
Community Expert , 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

Votes

Translate

Translate
Community Expert ,
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.

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 ,
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)?

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
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?

 

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 ,
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);

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
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?

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 ,
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.

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
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?

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 ,
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.

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 ,
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

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