Linking gradient copies by swatch

AiScripts
2 min readJul 25, 2022

--

In Adobe Illustrator, gradients in the Swatches panel are stored as a global swatch. Changing a gradient in Swatches affects all objects with that gradient in the active document. A similar, but not 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 to the document structure as a global swatch.

Unnamed global gradients persist between objects until changed manually

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[0].fillColor.gradient.gradientStops[0].color = newColor;
Both copies of the gradient are changed

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.

Breaking up the swatches by changing the stop position

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.

Vote on Uservoice to add methods to scripting to change gradients on objects without affecting the gradients of other objects.

--

--

AiScripts
AiScripts

Written by AiScripts

Sergey Osokin. Product Illustrator, Icon Designer, Script Developer (Ai, Ps). Writing about bugs and tricks in Adobe Illustrator scripts

No responses yet