-
Notifications
You must be signed in to change notification settings - Fork 22
/
Change Selected Comps Background.jsx
120 lines (99 loc) · 2.54 KB
/
Change Selected Comps Background.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
/**
* Changes the background colour of all selected comps to the specified hex value
*
* @author Zack Lovatt <[email protected]>
* @version 0.1.0
*/
(function changeSelectedCompsBackground() {
var selectedComps = app.project.selection;
if (selectedComps.length === 0) {
alert("Please select a composition!");
return;
}
var userColour = _getUserColour();
if (!userColour) {
alert("Enter a valid hex value!");
return;
}
app.beginUndoGroup("Change Selected Comps Background");
try {
changeCompsBackground(selectedComps, userColour);
} catch (e) {
alert(e, "Change Selected Comps Background");
} finally {
app.endUndoGroup();
}
/**
* Changes colour of selected comps to user-specified colour
*
* @param {Item[]} selectedComps Collection of items
* @param {ThreeDColorValue} rgb Colour to change to
*/
function changeCompsBackground(selectedComps, rgb) {
for (var ii = 0, il = selectedComps.length; ii < il; ii++) {
var comp = selectedComps[ii];
if (!(comp instanceof CompItem)) {
continue;
}
comp.bgColor = rgb;
}
}
/**
* Gets user colour hex
*
* @return {ThreeDColorValue | null} User colour as number array
*/
function _getUserColour() {
var userColour = prompt(
"Enter desired hex value",
"#888888",
"Change Selected Comps Background"
);
if (!userColour || !_isValidHex(userColour)) {
return null;
}
return _hexToRgb(userColour);
}
/**
* Checks whether hex string is valid
*
* @param {string} hexString Hex string to check
* @return {boolean} Whether it's valid
*/
function _isValidHex(hexString) {
var hasHash = hexString.indexOf("#") > -1;
if (hasHash && hexString.length !== 7) {
return false;
}
if (!hasHash && hexString.length !== 6) {
return false;
}
var colour = _hexToRgb(hexString);
for (var ii = 0, il = colour.length; ii < il; ii++) {
var colourValue = colour[ii];
if (isNaN(colourValue)) {
return false;
}
}
return true;
}
/**
* Converts hex string to RGB array
*
* @param {string} hex Hex string
* @returns {ThreeDColorValue} RGB
*/
function _hexToRgb(hex) {
var fallback = [1, 1, 1];
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
if (!result) {
/** @ts-ignore */
return fallback;
}
return [
parseInt(result[1], 16) / 255,
parseInt(result[2], 16) / 255,
parseInt(result[3], 16) / 255
];
}
})();