-
Notifications
You must be signed in to change notification settings - Fork 114
/
RemoveGradientStops.jsx
129 lines (110 loc) · 3.43 KB
/
RemoveGradientStops.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
/*
RemoveGradientStops.jsx for Adobe Illustrator
Description: Remove intermediate color stops from the gradients
Date: September, 2021
Author: Sergey Osokin, email: hi@sergosokin.ru
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
// Main function
function main() {
if (!documents.length) return;
if (!selection.length || selection.typename == 'TextRange') return;
var selPaths = [],
tmp = [];
getPaths(selection, selPaths, tmp);
if(!hasGradient(selPaths)) return;
for (var i = 0; i < selPaths.length; i++) {
if (selPaths[i].filled && isGradient(selPaths[i].fillColor)) {
removeIntermediateStops(selPaths[i].fillColor.gradient);
}
if (selPaths[i].stroked && isGradient(selPaths[i].strokeColor)) {
removeIntermediateStops(selPaths[i].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(item, arr, tmp) {
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':
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';
}
/**
* Search gradient color
* @param {array} collection - collection of items
* @return {boolean} contains at least one gradient
*/
function hasGradient(collection) {
for (var i = 0, len = collection.length; i < len; i++) {
if ((collection[i].filled && isGradient(collection[i].fillColor)) ||
(collection[i].stroked && isGradient(collection[i].strokeColor))) {
return true;
}
}
return false;
}
/**
* Remove all intermediate color stops
* @param {object} gradient - gradient color
*/
function removeIntermediateStops(gradient) {
for (var i = gradient.gradientStops.length - 2; i > 0; i--) {
gradient.gradientStops[i].remove();
}
}
// Run script
try {
main();
} catch (e) {}