-
Notifications
You must be signed in to change notification settings - Fork 114
/
CycleGradient.jsx
297 lines (253 loc) · 7.79 KB
/
CycleGradient.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
CycleGradient.jsx for Adobe Illustrator
Description: Rearrange colors in a gradient
Date: October, 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
function main() {
var SCRIPT = {
name: 'CycleGradient',
version: 'v.0.1'
},
CFG = {
dlgWidth: 130,
btnWidth: 110,
uiOpacity: .96 // UI window opacity. Range 0-1
};
if (!documents.length) return;
if (!selection.length || selection.typename == 'TextRange') return;
var selGradients = [],
tmp = []; // Array of temp paths for fix compound paths
getGradients(selection, selGradients, tmp);
// Selection has no gradients
if (!selGradients.length) {
// Clear changes in compound paths
for (var j = 0, tmpLen = tmp.length; j < tmpLen; j++) {
tmp[j].remove();
}
return;
}
// DIALOG
var dialog = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
dialog.preferredSize.width = CFG.dlgWidth;
dialog.alignChildren = ['fill', 'center'];
dialog.opacity = CFG.uiOpacity;
// ROWONE
var firstRow = dialog.add('group');
firstRow.orientation = 'row';
firstRow.alignChildren = ['left', 'center'];
var leftBtn = firstRow.add('button', undefined, 'Rotate Left');
leftBtn.preferredSize.width = CFG.btnWidth;
var rightBtn = firstRow.add('button', undefined, 'Rotate Right');
rightBtn.text = 'Rotate Right';
rightBtn.preferredSize.width = CFG.btnWidth;
// ROWTWO
var secondRow = dialog.add('group');
secondRow.orientation = 'row';
secondRow.alignChildren = ['left', 'center'];
var randBtn = secondRow.add('button', undefined, 'Randomize');
randBtn.preferredSize.width = CFG.btnWidth;
var distBtn = secondRow.add('button', undefined, 'Distribute');
distBtn.preferredSize.width = CFG.btnWidth;
leftBtn.onClick = function() {
processing(selGradients, shiftColorsBackward);
}
rightBtn.onClick = function() {
processing(selGradients, shiftColorsForward);
}
randBtn.onClick = function() {
processing(selGradients, shuffleColors);
}
distBtn.onClick = function() {
processing(selGradients, distributeStops);
}
dialog.onClose = function() {
// Clear changes in compound paths
for (var j = 0, tmpLen = tmp.length; j < tmpLen; j++) {
tmp[j].remove();
}
}
dialog.center();
dialog.show();
}
/**
* Get gradient colors 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 getGradients(collection, arr, tmp) {
for (var i = 0, iLen = collection.length; i < iLen; i++) {
var currItem = collection[i];
try {
switch (currItem.typename) {
case 'GroupItem':
getGradients(currItem.pageItems, arr);
break;
case 'PathItem':
if (currItem.filled && isGradient(currItem.fillColor)) {
arr.push(currItem.fillColor.gradient);
}
if (currItem.stroked && isGradient(currItem.strokeColor)) {
arr.push(currItem.strokeColor.gradient);
}
break;
case 'CompoundPathItem':
// Fix compound path created from groups
if (!currItem.pathItems.length) {
tmp.push(currItem.pathItems.add());
}
if (currItem.pathItems[0].filled && isGradient(currItem.pathItems[0].fillColor)) {
arr.push(currItem.pathItems[0].fillColor.gradient);
}
if (currItem.pathItems[0].stroked && isGradient(currItem.pathItems[0].strokeColor)) {
arr.push(currItem.pathItems[0].strokeColor.gradient);
}
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';
}
/**
* Rearrange colors in a gradient
* @param {object} collection - gradient colors
* @param {function} fn - rearrange function
*/
function processing(collection, fn) {
for (var i = 0, len = collection.length; i < len; i++) {
fn(collection[i]);
}
redraw();
}
/**
* 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];
}
}
/**
* Shift gradient colors forward
* @param {object} color - current item color
*/
function shiftColorsForward(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 = 0; i < gStopsLength; i++) {
if (i == 0) {
color.gradientStops[i].color = oldStops[oldStops.length - 1];
continue;
}
color.gradientStops[i].color = oldStops[i - 1];
}
}
/**
* Shuffle the colors of gradient stops
* @param {object} color - current item color
*/
function shuffleColors(color) {
var gStopsLength = color.gradientStops.length,
oldStops = [],
i;
// Get all stops colors
for (i = 0; i < gStopsLength; i++) {
oldStops.push(color.gradientStops[i].color);
}
shuffle(oldStops);
for (i = 0; i < gStopsLength; i++) {
color.gradientStops[i].color = oldStops[i];
}
}
/**
* Shuffle array
* @param {array} arr - array of colors
*/
function shuffle(arr) {
var j, temp;
for (var i = arr.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
/**
* Distribute uniform spacing between gradient stops
* @param {object} color - current item color
*/
function distributeStops(color) {
var stops = color.gradientStops.length,
dist = color.gradientStops[stops - 1].rampPoint - color.gradientStops[0].rampPoint,
step = dist / (stops - 1),
first = color.gradientStops[0].rampPoint;
// Trick to avoid reshuffle of gradient stops
resetRampPoints(color, stops, first, 0.0001);
for (var i = stops - 2; i > 0; i--) {
color.gradientStops[i].rampPoint = first + i * step;
}
}
/**
* Distribute minimal distance between gradient stops
* @param {object} color - current item color
* @param {number} stops - gradient stops length
* @param {number} first - position of first gradient stop
* @param {number} step - minimum position shift
*/
function resetRampPoints(color, stops, first, step) {
var delta = .0001;
for (var i = 0; i < stops - 1; i++) {
color.gradientStops[i].rampPoint = first + i * delta;
}
}
// Run script
try {
main();
} catch (e) {}