forked from dreygur/XDPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
68 lines (56 loc) · 1.35 KB
/
main.js
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
const { Color, LinearGradient } = require('scenegraph');
const { alert } = require('./lib/dialogs');
function generateColor() {
return {
r: Math.floor(Math.random() * 255),
g: Math.floor(Math.random() * 255),
b: Math.floor(Math.random() * 255)
};
}
async function setNewColor(selection) {
if (!selection.items.length) {
await alert(
'No items selected',
'To generate a color, please select at least one item.'
);
return;
}
const color = new Color(generateColor());
selection.items.forEach(item => {
item.fill = color;
});
}
async function setNewGradient(selection) {
if (!selection.items.length) {
await alert(
'No items selected',
'To generate a gradient, please select at least one item.'
);
return;
}
const gradient = new LinearGradient();
gradient.colorStops = [
{
color: new Color(generateColor()),
stop: 0
},
{
color: new Color(generateColor()),
stop: 1
}
];
const direction = Math.round(Math.random()) ? 'x' : 'y';
gradient.startX = 0;
gradient.startY = 0;
gradient.endX = direction === 'x' ? 1 : Math.random();
gradient.endY = direction === 'y' ? 1 : Math.random();
selection.items.forEach(item => {
item.fill = gradient;
});
}
module.exports = {
commands: {
color: setNewColor,
gradient: setNewGradient
}
};