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.
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);
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.
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
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.
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)?
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?
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);
Copy link to clipboard
Copied
Setting it to that number did worked, but how to calculate that without running that line?
Copy link to clipboard
Copied
You have to manually round the values in the color array, before comparing the two colors.
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?
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.
Copy link to clipboard
Copied
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