Linking gradient copies by swatch
In Adobe Illustrator, gradients in the Swatches panel are stored as a global color. Changing a gradient in Swatches affects all objects with it in the active document. A similar, but non-obvious to the user, logic applies to gradients that are created directly. You don’t see them in the Swatches, but Illustrator writes each gradient as a global swatch to the document structure.
This is a serious problem for script authors and users. If you write a script that changes the colors, gradient stops of an object, it will also change all copies of the gradient when you run it. This is either a copy of the object itself with the gradient, or an Eyedropper copy of the gradient on any object. The user may not see that in the document the color has changed not on one object, but on many, where he did not expect.
var newColor = new RGBColor();
newColor.red = 255;
selection.fillColor.gradient.gradientStops.color = newColor;
At the moment, there is only a manual way to break the connection between the gradient copies before running the script. You need to select the object and change one of the parameters in the Gradient panel:
- the position of any color stop (location)
- color stop opacity
- use the Reverse Gradient button
- change gradient type. For example, linear gradient to radial
Next, return the changed parameter back. Visually, the gradient remains the same as the other object, but Illustrator will actually write a new swatch to the document. Changing the angle and aspect ratio does not create a new gradient.
As a result, for now we will have to warn users in the script descriptions and suggest that they manually separate the gradients before running the script.