-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathCycleGradientBackward.jsx
129 lines (109 loc) · 3.42 KB
/
CycleGradientBackward.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
CycleGradientBackward.jsx for Adobe Illustrator
Description: Shifts the colors of gradient stops backward without changing their positions
Date: August, 2021
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
if (!documents.length) return;
if (!selection.length || selection.typename == 'TextRange') return;
var selPaths = [],
tmp = []; // Array of temp paths for fix compound paths
getPaths(selection, selPaths, tmp);
// Processing
for (var i = 0, selLen = selPaths.length; i < selLen; i++) {
var currItem = selPaths[i];
// Shift fill gradient
if (currItem.filled && isGradient(currItem.fillColor)) {
shiftColorsBackward(currItem.fillColor.gradient);
}
// Shift stroke gradient
if (currItem.stroked && isGradient(currItem.strokeColor)) {
shiftColorsBackward(currItem.strokeColor.gradient);
}
}
// Clear changes in compound paths
for (var j = 0, tmpLen = tmp.length; j < tmpLen; j++) {
tmp[j].remove();
}
}
/**
* Get paths from selection
* @param {object} collection - collection of items
* @param {array} arr - output array of single paths
* @param {array} tmp - output array of temporary paths in compounds
*/
function getPaths(collection, arr, tmp) {
for (var i = 0, iLen = collection.length; i < iLen; i++) {
var currItem = collection[i];
try {
switch (currItem.typename) {
case 'GroupItem':
getPaths(currItem.pageItems, arr);
break;
case 'PathItem':
arr.push(currItem);
break;
case 'CompoundPathItem':
// Fix compound path created from groups
if (!currItem.pathItems.length) {
tmp.push(currItem.pathItems.add());
}
arr.push(currItem.pathItems[0]);
break;
default:
break;
}
} catch (e) {}
}
}
/**
* Check gradient color
* @param {object} color - current item color
* @return {boolean} is gradient color or not
*/
function isGradient(color) {
return color.typename === 'GradientColor';
}
/**
* Shift gradient colors backward
* @param {object} color - current item color
*/
function shiftColorsBackward(color) {
var gStopsLength = color.gradientStops.length,
oldStops = [],
i;
// Get all stops colors
for (i = 0; i < gStopsLength; i++) {
oldStops.push(color.gradientStops[i].color);
}
// Rearrange
for (i = gStopsLength - 1; i >= 0; i--) {
if (i == gStopsLength - 1) {
color.gradientStops[i].color = oldStops[0];
continue;
}
color.gradientStops[i].color = oldStops[i + 1];
}
}
try {
main();
} catch (e) {}