Scripting Illustrator - Convert A Flat Process Color Into A Matching Gradient
We all are quite familiar with Adobe Illustrator and its User Interface. In our daily creative tasks we use a lot of features, such as: menu, pallets, colors, plug-ins and tools. We use these features in the form of a UI. These UI features are brought forward to us through various scripts and plug-ins.
In a simpler way, we can code scripts to automate tasks or to obtain some effect in Illustrator. Follow along to learn how to do that. In this tutorial, we will code in Javascript a script that will convert a flat process filled object into its matching gradient.
Vector Plus
Want access to the full Vector Source files and downloadable copies of every tutorial, including this one? Join Vector Plus for just 9$ a month.
Tutorial Details
- Program: Adobe Illustrator CS3 and ExtendedScript Toolkit
- Difficulty: Intermediate
- Estimated Completion Time: 3 to 4 Hours
AIM of the Script
We want this script to perform a very simple task. In Adobe Illustrator, when a user selects some objects filled with a Flat CMYK Color, and executes this Script; the objects shall get converted into a matching CMYK Gradient fill. The output gradient will be somewhat similar to a gradient overlay effect seen in multiply mode. This is shown in the image below.



Hence, the aim of our script is to convert a flat CMYK filled object into a dark-to-light matching gradient. This is a simple action that is a good exercise to learn how to script for Illustrator.
Logic and Algorithm
The logic for converting a flat color into a dark-to-light gradient can be understood in five easy steps:
- Step 1: Pick the color of the current object. i.e. currentColor = color of currently selected object.
- Step 2: Create a new gradient color with two rampstops: startColor and endColor.
- Step 3: startColor = currentColor – gShift i.e. lighten the original color.
- Step 4: endColor = currentColor + gShift i.e. darken the original color.
- Step 5: Apply the new gradient with startColor and endColor to the object.
Where, gShift is the amount of gradient shift that will be entered by the user. The above algorithm can be easily understood from the pictorial representation shown below.



Apart from the core logic, we will implement some validations and checks for proper functioning of this script. These validations will be covered at various stages of this tutorial. Before heading towards the actual tutorial, let's get familiar with some prerequisites.
Introduction to Scripting
This tutorial requires some basic knowledge of scripting and its environment. To get familiar with Illustrator's Scripting Environment, you can have a quick look into the documentation available in your Illustrator's installation folder go to installation_Directory \ Adobe \ Adobe Illustrator CS3 \ Scripting \ Documentation. Here, you will find the following documents to get started with JavaScript:
- Adobe Intro to Scripting.pdf
- Illustrator CS3 Scripting Guide.pdf
- Illustrator CS3 JavaScript Reference.pdf
- JavaScript Tools Guide CS3.pdf
Types of Scripts
There are three types of scripts we can write for Illustrator. These are: AppleScript, JavaScript and VBScript. Since JavaScript is supported on both Windows and Mac OS, we will code our logic in JavaScript. The syntax and notations are very similar to the standard JavaScript. The only difference is about the extension. For Illustrator, we will denote all the JavaScript files with an extension .jsx instead of the common .js extension.
Required Tools
Although, you can code the entire JavaScript in any of your favorite text editor; it is advisable to use the Adobe ExtendedScript Toolkit. By using this application, we can run, debug, test and check our code synchronously with Illustrator.
Note: Throughout this tutorial, we will denote ExtendedScript Toolkit with a short form - ESTK.
Step 1
Open Adobe ExtendedScript Toolkit and create a New JavaScript File. Next, select Adobe Illustrator from the drop-down list for target application. This tells the ESTK that we are writing JavaScript for a particular application, i.e. Adobe Illustrator.



Step 2 - Write a Simple Code Snippet
In the code editing area, insert the following lines of code:
1 |
if(app.documents.length > 0) |
2 |
alert("Documents are available"); |
3 |
else |
4 |
alert("No documents available"); |
In the above code, we are checking if there are any documents currently opened in Illustrator. This is done via
app.documents.length
. i.e. The top level object in any Adobe Application DOM is the application object. Next is the document object, which further holds objects like colors, layers, text, pages etc. Save this file on your hard drive as "test.jsx."
Step 2.1 - Executing the Script
There are two ways to execute the Script:
1. Executing from ESTK
If you wish to run your script through ESTK, press the Play button as shown below.



This will automatically launch the Adobe Illustrator application and will generate the alert message. You can halt or stop the execution by pressing the pause or stop buttons respectively.



2. Executing from Illustrator
To execute this script from Illustrator, go to File > Scripts > Other Script (Command + F12) and locate your newly created file "test.jsx." After you click Open, the script will get executed.
So what did you see? An alert message, "No documents available."



Next, create a couple of new documents in Illustrator and run this script again. This time, you will see an alert, "Documents are available."



Up to this point, we have written and tested a small piece of code. This was done as a warm-up exercise to get familiar with ESTK and Illustrator. In our next steps, we are going to work on the actual script.
Step 3 - The Code Structure
Return back to ESTK and delete the testing code that we created in Step 2. Next, add the following lines of code into "test.jsx:"
1 |
/* To make sure there is at least one document available in Illustrator |
2 |
and atleast one object in that document to work upon*/ |
3 |
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0) |
4 |
{ |
5 |
// To make sure the Document Color Mode is CMYK |
6 |
if(app.activeDocument.documentColorSpace == DocumentColorSpace.CMYK) |
7 |
{ |
8 |
convertToGrad(); // Call to actual function which will contain all the logic |
9 |
} |
10 |
|
11 |
else |
12 |
{ |
13 |
alert("Document Color Space is not CMYK. Change the Document Color Mode to CMYK Mode", "CMYK Document required"); |
14 |
} |
15 |
|
16 |
} |
17 |
|
18 |
else |
19 |
{ |
20 |
alert("Either no document is available or the document is empty"); |
21 |
} |
22 |
|
23 |
function convertTograd() |
24 |
{ |
25 |
//Logic for converting flat color into gradient |
26 |
} |
In the above code structure, we are simply creating a pre-requisite check for our main logical function – converToGrad(). i.e. The main logic should execute only if the following conditions are met:
- At least one document with one object exists, so that we can work on it.
- The document color space should be CMYK, because the entire logic is based upon CMYK colors.
Step 3.1 - Understanding the DOM Entities
app.documents.length
returns the total number of documents opened in Illustrator.
An activeDocument
object refers to the current (active) document in which you are working.
pathItems
refers to a collection of all the pathItem
objects within an Illustrator document.Any graphical item like rectangle, ellipse, polygon, line, custom shapes etc are denoted as pathItem
; and a collection of all these pathItem
are denoted as pathItems
. Hence, app.activeDocument.pathItems.length
will return the number of graphical entities contained in the current document.
The documentColorSpace
object defines the color mode of the document. We are making this validation because all the logic in this script will be based upon CMYK color space.
The basic code structure is all set and done. Next, we will enter into convertToGrad()
function.
Step 4 - Starting with the Main Logic
The first and foremost requirement of our main logic is to fetch the total number of selected items. If no items are selected, the script shall notify you to select at least one object. For this, add the following lines of code into convertToGrad()
function:
1 |
function convertToGrad() |
2 |
{ |
3 |
|
4 |
var items = selection; |
5 |
var totalSelected = items.length; |
6 |
|
7 |
if(totalSelected > 0) |
8 |
{ |
9 |
// proceed with the main logic |
10 |
} |
11 |
|
12 |
else |
13 |
{ |
14 |
alert("Please select atleast one object"); |
15 |
} |
16 |
|
17 |
}//end convertToGrad |



var items = selection
creates a new variable items
and assigns entire selection to it. In the next line, items.length
returns the total number of selected items, which are assigned to totalSelected
.
Step 5 - Getting the User Data
To convert a flat color into a gradient, we will prompt the user to enter some values for Gradient Shift and the Gradient Angle. But before that, let's review what they are.
Gradient Shift: This is a numeric value which decides how much color will be added or removed from the original color. This is the core part of our logic in converting a flat color into a gradient.
How does a Gradient Shift work? To understand how it works, consider a simple example of an object filled with CMYK values: C=50, M=20, Y=100 and K=30 respectively.



Next, we ask the user to enter a value for Gradient Shift. Let's denote this value by gShift
, and the user has entered 10 for gShift
.



Once we've got the gShift
value, we will create two gradient stops for our object. The first stop, i.e. the lighter one; will have all the CMYK values decremented by gShift
. Whereas, the last stop, i.e. the darker one, will have all the CMYK values incremented by gShift
.



This way, a dark to light gradient will be generated from the actual fill color. We will also perform some validations to constrain the CMYK values within 0 and 100, because adding or subtracting a gShift
value can move the CMYK values beyond 0 or 100.
Gradient Angle: Although not a part of the core logic, we will use this feature to provide some accessibility and freedom to choose a custom angle for the resultant gradient. The usage of gradient angle will be seen in the later stage of this tutorial.
Let's return back to the code and fetch the user data.
Step 6
To fetch the user data, enter the following lines of code in the if(totalSelected >0)
block:
1 |
if(totalSelected > 0) |
2 |
{ |
3 |
|
4 |
var gShift = Math.round(prompt("Enter a value for gradient shift","10","Gradient Shift")); |
5 |
if(gShift == null || gShift <=0) gShift=0; |
6 |
if(gShift >=100) gShift =100; |
7 |
|
8 |
var gradAngle = prompt("Vector angle for Gradient","0.0","Gradient Angle") |
9 |
if(gradAngle == null) gradAngle=0; |
10 |
|
11 |
} //endif |
In the above lines of code, we are prompting a user to enter some value for gradient shift and gradient angle. The Math.round()
function rounds the entered value into a whole number. (Note: You can skip Math.round()
if you wish to have decimal values accepted.) The default values for gShift
and gradAngle
are set to "10" and "0.0" respectively.
Note that we are also validating the entered values for null and zero. Null is encountered when a user clicks the Cancel button. Also, we are restricting the gShift
value within 0 and 100.
Once we have the gShift
and gradAngle
values, we can proceed ahead for the rest of the code.
Step 7 - Working on the Selected Objects
Just after the gradAngle
declaration, enter the following lines of code:
1 |
for(var j=0; j < totalSelected; j++) |
2 |
{ |
3 |
var currentObject = app.activeDocument.selection[j]; |
4 |
|
5 |
if(currentObject.typename != "CompoundPathItem" && |
6 |
currentObject.typename != "GroupItem") |
7 |
{ |
8 |
if(currentObject.filled==true && |
9 |
currentObject.fillColor.typename != "GradientColor" && |
10 |
currentObject.fillColor.typename != "PatternColor" && |
11 |
currentObject.fillColor.typename != "SpotColor" &&) |
12 |
currentObject.fillColor.typename != "GrayColor" |
13 |
{ |
14 |
|
15 |
//perform color conversion here |
16 |
} |
17 |
|
18 |
else |
19 |
{ |
20 |
alert("Fill an object with CMYK color. Any objects containing\ngradients, patterns, spot colors or empty fills will be omitted."," Only CMYK Colors Allowed"); |
21 |
} |
22 |
} |
23 |
|
24 |
else |
25 |
{ |
26 |
alert("This script only works with Non-Compound Objects or Isolated Group items.\nAny items with Groups or Compound Objects will be omitted.", "Ungroup or Isolate the Group Items"); |
27 |
} |
28 |
|
29 |
}//endfor |
app.activeDocument.selection[j]
returns the selected object one-by-one for each iteration of j
.
For each selected object, we are making some validations and checks. These checks are crucial at this stage. The first check is to determine if the object is a compound item or a group item. Since this script will not work directly on groups or compound objects, this check is mandatory.
In the next level, we are checking if the selected object is already a gradient, pattern or spot color. If so, these objects will be skipped.
There is one more check for grayColor specification. GrayColor is slightly different than CMYK Color specification.
A detailed technique is involved for treating objects with grayColor specification, which I am not including at this stage of the tutorial. However, this technique is included in the source files for your interest.
Step 8 - Pick and Split the Color
As per our logic and algorithm, we will pick the color for each selected object and split its CMYK values into four different variables, as shown below:
1 |
var currentColor = currentObject.fillColor; |
2 |
|
3 |
var currentCyan = Math.round(currentColor.cyan); |
4 |
var currentMagenta = Math.round(currentColor.magenta); |
5 |
var currentYellow = Math.round(currentColor.yellow); |
6 |
var currentBlack = Math.round(currentColor.black); |
Note: All these lines of code shall come within "Perform color conversion" block, as shown in Step 7.
pathItem.fillColor
returns the fill color of a particular path item. In our case, the pathItem is currentObject
. We are using color.cyan
, color.magenta
, color.yellow
and color.black
properties to retrieve the C, M, Y and K values respectively into four different variables. Once we set these values, we can easily offset them with gShift
.
Step 9 - Declare Colors for Gradient Rampstops
Our resultant gradient will have two gradient stops. Let's denote them with startColor
and endColor
. The variable declaration for these two rampstop colors will be as shown below:
1 |
var startColor = new CMYKColor(); |
2 |
var endColor = new CMYKColor(); |
The CMYKColor
is a CMYK color specification used for color objects. Here, the startColor
and endColor
are declared as new CMYK colors.
Step 10 - Offsetting the startColor and endColor
At this stage, we have all the necessary resources to create a new gradient. We have currentColor
(with separate CMYK values), startColor
, endColor
and gShift
. We can now set the final color values for startColor
and endColor
using offset technique.
Step 10.1 - Setting the startColor
The startColor
will be the lighter part of the gradient. Hence, we will subtract gShift
from C, M, Y and K values of currentColor
, resulting in startColor
.
1 |
startColor.cyan = currentCyan – gShift; |
2 |
startColor.magenta = currentMagenta - gShift; |
3 |
startColor.yellow= currentYellow - gShift; |
4 |
startColor.black = currentBlack - gShift; |
This is simple, but we need to perform some checks. For example, what will happen if the currentCyan
is already less than gShift
? Say, currentCyan
is 10 and gShift
is 20. The resulting cyan value for startColor
will become 10-20 = (-) 10. To overcome this issue, we will modify the above set of code into the following lines of code:
1 |
if(currentCyan < gShift) |
2 |
startColor.cyan= 0; |
3 |
else |
4 |
startColor.cyan= currentCyan - gShift; |
5 |
|
6 |
if(currentMagenta < gShift) |
7 |
startColor.magenta= 0; |
8 |
else |
9 |
startColor.magenta = currentMagenta - gShift; |
10 |
|
11 |
if(currentYellow < gShift) |
12 |
startColor.yellow= 0; |
13 |
else |
14 |
startColor.yellow= currentYellow - gShift; |
15 |
|
16 |
if(currentBlack < gShift) |
17 |
startColor.black= 0; |
18 |
else |
19 |
startColor.black = currentBlack - gShift; |
Step 10.2 - Setting the endColor
The endColor
will be the darker part of the gradient. Hence, we will add gShift
to C,M,Y and K values or currentColor
respectively. After simple addition, the code should look as follows:
1 |
endColor.cyan= currentCyan + gShift; |
2 |
endColor.magenta = currentMagenta + gShift; |
3 |
endColor.yellow= currentYellow + gShift; |
4 |
endColor.black = currentBlack + gShift; |
Once again, everything looks all right. But there is one complication: What will happen if the addition of gShift
results in a value greater than 100? For example, currentCyan
is 95 and gShift
is 20. Adding these two will result as endColor.cyan = 115
; which is not acceptable. So we will compensate this with some validations.
Let's create a new variable gShiftEnd
, which is equal to 100-gShift
. Next, we will check if the current C, M, Y or K value is greater than gShiftEnd
. If so, we will set the endColor's C,M,Y or K value to 100; else we will set endColor's C, M, Y or K value by adding gShift
to the current C, M, Y or K. This can be elaborated from the example we just saw above.
We have currentCyan = 95 and gShift = 20. Therefore gShiftEnd = 100 – 20. i.e. 80. Next, currentCyan is greater than gShiftEnd; so we will set endColor.cyan to 100.
Following this principle, our code will get modified to the following:
1 |
gShiftEnd = 100-gShift; |
2 |
|
3 |
if(currentCyan > gShiftEnd) |
4 |
endColor.cyan= 100; |
5 |
else |
6 |
endColor.cyan= currentCyan + gShift; |
7 |
|
8 |
if(currentMagenta > gShiftEnd) |
9 |
endColor.magenta= 100; |
10 |
else |
11 |
endColor.magenta = currentMagenta + gShift; |
12 |
|
13 |
if(currentYellow > gShiftEnd) |
14 |
endColor.yellow= 100; |
15 |
else |
16 |
endColor.yellow= currentYellow + gShift; |
17 |
|
18 |
if(currentBlack > gShiftEnd) |
19 |
endColor.black= 100; |
20 |
else |
21 |
endColor.black = currentBlack + gShift; |
So, up to this point, the complete code will look like this:
1 |
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0) |
2 |
{ |
3 |
if(app.activeDocument.documentColorSpace == DocumentColorSpace.CMYK) |
4 |
{ |
5 |
convertToGrad(); |
6 |
} |
7 |
else |
8 |
{ |
9 |
alert("Document Color Space is not CMYK. Change the Document Color Mode to CMYK Mode", "CMYK Document required"); |
10 |
} |
11 |
|
12 |
}//end main if |
13 |
|
14 |
else |
15 |
{ |
16 |
alert("Either no document is available or the document is empty"); |
17 |
} |
18 |
|
19 |
|
20 |
function convertToGrad() { |
21 |
|
22 |
var items = selection; |
23 |
var totalSelected = items.length; |
24 |
|
25 |
if(totalSelected > 0) |
26 |
{ |
27 |
var gShift = Math.round(prompt("Enter a value for gradient shift","10","Gradient Shift")); |
28 |
if(gShift == null || gShift <=0) gShift=0; |
29 |
if(gShift >=100) gShift =100; |
30 |
|
31 |
var gradAngle = prompt("Vector angle for Gradient","0.0","Gradient Angle") |
32 |
if(gradAngle == null) gradAngle=0; |
33 |
|
34 |
for(var j=0; j < totalSelected; j++) |
35 |
{ |
36 |
var currentObject = app.activeDocument.selection[j]; |
37 |
if(currentObject.typename != "CompoundPathItem" && currentObject.typename != "GroupItem") |
38 |
{ |
39 |
if(currentObject.filled==true && |
40 |
currentObject.fillColor.typename != "GradientColor" && |
41 |
currentObject.fillColor.typename != "PatternColor" && |
42 |
currentObject.fillColor.typename != "SpotColor" && |
43 |
currentObject.fillColor.typename != "GrayColor" |
44 |
) |
45 |
{ |
46 |
|
47 |
var currentColor = currentObject.fillColor; |
48 |
var currentCyan = Math.round(currentColor.cyan); |
49 |
var currentMagenta = Math.round(currentColor.magenta); |
50 |
var currentYellow = Math.round(currentColor.yellow); |
51 |
var currentBlack = Math.round(currentColor.black); |
52 |
|
53 |
|
54 |
// Create a color for both ends of the gradient |
55 |
var startColor = new CMYKColor(); |
56 |
var endColor = new CMYKColor(); |
57 |
|
58 |
if(currentCyan < gShift) |
59 |
startColor.cyan = 0; |
60 |
else |
61 |
startColor.cyan = currentCyan - gShift; |
62 |
|
63 |
if(currentMagenta < gShift) |
64 |
startColor.magenta = 0; |
65 |
else |
66 |
startColor.magenta = currentMagenta - gShift; |
67 |
|
68 |
if(currentYellow < gShift) |
69 |
startColor.yellow = 0; |
70 |
else |
71 |
startColor.yellow = currentYellow - gShift; |
72 |
|
73 |
if(currentBlack < gShift) |
74 |
startColor.black= 0; |
75 |
else |
76 |
startColor.black = currentBlack - gShift; |
77 |
|
78 |
gShiftEnd = 100 - gShift; |
79 |
|
80 |
if(currentCyan > gShiftEnd) |
81 |
endColor.cyan = 100; |
82 |
else |
83 |
endColor.cyan = currentCyan + gShift; |
84 |
|
85 |
if(currentMagenta > gShiftEnd) |
86 |
endColor.magenta = 100; |
87 |
else |
88 |
endColor.magenta = currentMagenta + gShift; |
89 |
|
90 |
if(currentYellow > gShiftEnd) |
91 |
endColor.yellow = 100; |
92 |
else |
93 |
endColor.yellow = currentYellow + gShift; |
94 |
|
95 |
if(currentBlack > gShiftEnd) |
96 |
endColor.black = 100; |
97 |
else |
98 |
endColor.black = currentBlack + gShift; |
99 |
|
100 |
// Gradient Declaration Block |
101 |
|
102 |
|
103 |
} //endif |
104 |
|
105 |
else |
106 |
{ |
107 |
alert("Fill an object with CMYK color. Any objects containing\ngradients, patterns, spot colors or empty fills will be omitted."," Only CMYK Colors Allowed"); |
108 |
} |
109 |
} |
110 |
|
111 |
else |
112 |
{ |
113 |
alert("This script only works with Non-Compound Objects or Isolated Group items.\nAny items with Groups or Compound Objects will be omitted.", "Ungroup or Isolate the Group Items"); |
114 |
|
115 |
} |
116 |
|
117 |
}//endfor |
118 |
}//end selected |
119 |
else |
120 |
{ |
121 |
alert("Please select atleast one object"); |
122 |
} |
123 |
} |
Step 11 - Creating the New Gradient
Now that we have set the startColor
and endColor
; we can proceed with the creation of the new gradient.
Step 11.1 - Declare the Gradient
To add a new gradient, insert the following lines of code in the "//Gradient Declaration Block".
1 |
var newGradient = app.activeDocument.gradients.add(); |
2 |
newGradient.type = GradientType.LINEAR; |
This will add a new Linear type Gradient into the current active document.
Step 11.2 - Setting the Properties of the Newly Created Gradient
Add the following lines of code just after the gradient declaration:
1 |
// Modify the first gradient stop |
2 |
newGradient.gradientStops[0].rampPoint = 0; |
3 |
newGradient.gradientStops[0].midPoint = 50; |
4 |
newGradient.gradientStops[0].color = startColor; |
5 |
|
6 |
// Modify the last gradient stop |
7 |
newGradient.gradientStops[1].rampPoint = 100; |
8 |
newGradient.gradientStops[1].color = endColor; |
gradientStop[0]
is the first gradient stop, whereas, gradientStop[1]
is the last gradient stop. If you wish to have more gradient stops, you can denote them as gradientStop[2], gradientStop[3]
and so on.
rampPoint
is used to set the ramp-point position of the associated gradientStop. We've set the first and last ramp-point as 0 and 100 respectively. midpoint
is used to set the position of the middle point between two gradient stops. We have set this to 50. Finally, we have assigned the values of startColor
and endColor
to the first and last gradientStops respectively.
Step 12 - Create a Gradient Color Object
We are almost done with color conversion and gradient formation. Next, we need to apply this newly created gradient to the currently selected object. To do that, we will create a Gradient Color Object referring to the newly created gradient.
Note: We cannot apply the newly created gradient directly as a fill Color to the object. In order to use this gradient, we will have to create a new Gradient Color object, as shown below:
1 |
// construct an Illustrator.GradientColor object referring to the |
2 |
// newly created gradient |
3 |
var colorOfGradient= new GradientColor(); |
4 |
colorOfGradient.gradient = newGradient; |
Step 13 - Apply the Gradient
Finally, we will fill the currentObject
with the colorOfGradient
, as shown below:
1 |
// get current path item, apply new gradient as its fill |
2 |
currentObject.fillColor = colorOfGradient; |
3 |
currentObject.rotate(gradAngle, false, false, true, false, Transformation.CENTER); |
We have applied a rotation matrix to the current object. This rotation matrix makes use of the gradient angle to transform the gradient. The syntax of rotation is:
rotate
(angle
[,changePositions]
[,changeFillPatterns]
[,changeFillGradients]
[,changeStrokePattern]
[,rotateAbout])
Here, we have set changeFillgradients
to "true" and the rest to "false." This is a tricky way to rotate the gradient color at a particular angle. Thanks to John Wundes for suggesting to me this intelligent technique.
Step 14 - The Final Script
The final script after complete coding is shown below:
1 |
if ( app.documents.length > 0 && app.activeDocument.pathItems.length > 0) |
2 |
{ |
3 |
if(app.activeDocument.documentColorSpace == DocumentColorSpace.CMYK) |
4 |
{ |
5 |
convertToGrad(); |
6 |
} |
7 |
else |
8 |
{ |
9 |
alert("Document Color Space is not CMYK. Change the Document Color Mode to CMYK Mode", "CMYK Document required"); |
10 |
} |
11 |
|
12 |
}//end main if |
13 |
|
14 |
else |
15 |
{ |
16 |
alert("Either no document is available or the document is empty"); |
17 |
} |
18 |
|
19 |
|
20 |
function convertToGrad() { |
21 |
|
22 |
var items = selection; |
23 |
var totalSelected = items.length; |
24 |
|
25 |
if(totalSelected > 0) |
26 |
{ |
27 |
var gShift = Math.round(prompt("Enter a value for gradient shift","10","Gradient Shift")); |
28 |
if(gShift == null || gShift <=0) gShift=0; |
29 |
if(gShift >=100) gShift =100; |
30 |
|
31 |
var gradAngle = prompt("Vector angle for Gradient","0.0","Gradient Angle") |
32 |
if(gradAngle == null) gradAngle=0; |
33 |
|
34 |
for(var j=0; j < totalSelected; j++) |
35 |
{ |
36 |
var currentObject = app.activeDocument.selection[j]; |
37 |
if(currentObject.typename != "CompoundPathItem" && currentObject.typename != "GroupItem") |
38 |
{ |
39 |
if(currentObject.filled==true && |
40 |
currentObject.fillColor.typename != "GradientColor" && |
41 |
currentObject.fillColor.typename != "PatternColor" && |
42 |
currentObject.fillColor.typename != "SpotColor" && |
43 |
currentObject.fillColor.typename != "GrayColor" |
44 |
) |
45 |
{ |
46 |
|
47 |
var currentColor = currentObject.fillColor; |
48 |
var currentCyan = Math.round(currentColor.cyan); |
49 |
var currentMagenta = Math.round(currentColor.magenta); |
50 |
var currentYellow = Math.round(currentColor.yellow); |
51 |
var currentBlack = Math.round(currentColor.black); |
52 |
|
53 |
|
54 |
// Create a color for both ends of the gradient |
55 |
var startColor = new CMYKColor(); |
56 |
var endColor = new CMYKColor(); |
57 |
|
58 |
if(currentCyan < gShift) |
59 |
startColor.cyan= 0; |
60 |
else |
61 |
startColor.cyan = currentCyan - gShift; |
62 |
|
63 |
if(currentMagenta < gShift) |
64 |
startColor.magenta = 0; |
65 |
else |
66 |
startColor.magenta = currentMagenta - gShift; |
67 |
|
68 |
if(currentYellow < gShift) |
69 |
startColor.yellow = 0; |
70 |
else |
71 |
startColor.yellow = currentYellow - gShift; |
72 |
|
73 |
if(currentBlack < gShift) |
74 |
startColor.black = 0; |
75 |
else |
76 |
startColor.black = currentBlack - gShift; |
77 |
|
78 |
gShiftEnd = 100 - gShift; |
79 |
|
80 |
if(currentCyan > gShiftEnd) |
81 |
endColor.cyan = 100; |
82 |
else |
83 |
endColor.cyan = currentCyan + gShift; |
84 |
|
85 |
if(currentMagenta > gShiftEnd) |
86 |
endColor.magenta = 100; |
87 |
else |
88 |
endColor.magenta = currentMagenta + gShift; |
89 |
|
90 |
if(currentYellow > gShiftEnd) |
91 |
endColor.yellow = 100; |
92 |
else |
93 |
endColor.yellow = currentYellow + gShift; |
94 |
|
95 |
if(currentBlack > gShiftEnd) |
96 |
endColor.black = 100; |
97 |
else |
98 |
endColor.black = currentBlack + gShift; |
99 |
|
100 |
// Create a new gradient |
101 |
// A new gradient always has 2 stops |
102 |
var newGradient = app.activeDocument.gradients.add(); |
103 |
newGradient.type = GradientType.LINEAR; |
104 |
|
105 |
|
106 |
// Modify the first gradient stop |
107 |
newGradient.gradientStops[0].rampPoint = 0; |
108 |
newGradient.gradientStops[0].midPoint = 50; |
109 |
newGradient.gradientStops[0].color = startColor; |
110 |
|
111 |
// Modify the last gradient stop |
112 |
newGradient.gradientStops[1].rampPoint = 100; |
113 |
newGradient.gradientStops[1].color = endColor; |
114 |
|
115 |
// construct an Illustrator.GradientColor object referring to the |
116 |
// newly created gradient |
117 |
var colorOfGradient= new GradientColor(); |
118 |
colorOfGradient.gradient = newGradient; |
119 |
|
120 |
// get current path item, apply new gradient as its fill |
121 |
currentObject.fillColor = colorOfGradient; |
122 |
currentObject.rotate(gradAngle, false, false, true, false, Transformation.CENTER); |
123 |
|
124 |
} //endif |
125 |
|
126 |
else |
127 |
{ |
128 |
alert("Fill an object with CMYK color. Any objects containing\ngradients, patterns, spot colors or empty fills will be omitted."," Only CMYK Colors Allowed"); |
129 |
} |
130 |
} |
131 |
|
132 |
else |
133 |
{ |
134 |
alert("This script only works with Non-Compound Objects or Isolated Group items.\nAny items with Groups or Compound Objects will be omitted.", "Ungroup or Isolate the Group Items"); |
135 |
|
136 |
} |
137 |
|
138 |
}//endfor |
139 |
}//end selected |
140 |
else |
141 |
{ |
142 |
alert("Please select atleast one object"); |
143 |
} |
144 |
} |
Step 15 - Execute the Script
Save the script and create a new document in Adobe Illustrator. Next, create some objects with flat CMYK colors. Select some of these objects and execute the script by following the methods as described in Step 2.1.



You will be prompted to enter the Gradient Shift. Enter any value for gShift
.



Next, you will be prompted for gradient angle. Enter some value for gradAngle.



Finally, you will see the output shown below.



Conclusion and Scope
We have learned how to write scripts to automate or generate an effect. The example shown in this tutorial was a simple demonstration of what we can perform through scripts. With this example, we've learnt how CMYK colors are represented in scripts, how they work and how objects behave in the DOM of Adobe Illustrator. In our next part, we will cover a Script that melts the available gradient color into a flat CMYK color. Hope you enjoyed this introductory tutorial to scripts.
Subscribe to the Vectortuts+ RSS Feed to stay up to date with the latest vector tutorials and articles.
