-
Notifications
You must be signed in to change notification settings - Fork 114
/
ReverseGradientColor.jsx
109 lines (90 loc) · 2.84 KB
/
ReverseGradientColor.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
/*
ReverseGradientColor.jsx for Adobe Illustrator
Description: Reverse of colors and their gradient transparency. Does not reverse the location of color stops
Date: August, 2020
Author: Sergey Osokin, email: hi@sergosokin.ru
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
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) {
alert('Error\nOpen a document and try again');
return;
}
if (!selection.length || selection.typename == 'TextRange') {
alert('Error\nPlease select atleast one object');
return;
}
var doc = activeDocument,
selPaths = [];
getPaths(selection, selPaths);
for (var i = 0, selLen = selPaths.length; i < selLen; i++) {
reverseGradient(selPaths[i]);
}
}
// Get only Paths from selection
function getPaths(item, arr) {
for (var i = 0, iLen = item.length; i < iLen; i++) {
var currItem = item[i];
try {
switch (currItem.typename) {
case 'GroupItem':
getPaths(currItem.pageItems, arr);
break;
case 'PathItem':
if (currItem.filled && isGradientFill(currItem)) {
arr.push(currItem);
} else {
badFillCount++;
}
break;
case 'CompoundPathItem':
if (currItem.pathItems[0].filled && isGradientFill(currItem.pathItems[0])) {
arr.push(currItem.pathItems[0]);
} else {
badFillCount++;
}
break;
default:
break;
}
} catch (e) {}
}
}
function isGradientFill(item) {
if (item.fillColor.typename === 'GradientColor') { return true; }
return false;
}
function reverseGradient(item) {
var gItem = item.fillColor.gradient,
tempStop = gItem.gradientStops,
tempColor, tempOpacity;
for (var i = 0, j = tempStop.length - 1; i < j; i++, j--) {
var rStop = gItem.gradientStops[j],
lStop = gItem.gradientStops[i];
tempColor = rStop.color;
tempOpacity = rStop.opacity;
rStop.color = lStop.color;
rStop.opacity = lStop.opacity;
lStop.color = tempColor;
lStop.opacity = tempOpacity;
}
}
// Run script
try {
main();
} catch (e) {}