-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color Box Labeler.jsx
145 lines (106 loc) · 5.65 KB
/
Color Box Labeler.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
var gTextMargin = 10; //Margin between the bottom of the color block and the label
var PrecisionLength = 2; // How many decimal points on color channel values
var textSize = 4;
checkSelected();
writeLabels();
//Validate the selections to ensure they meet labeling criteria
function checkSelected() {
var selectedObjects = app.activeDocument.selection;
//Sanity Check: Ensure there is items selected
if(selectedObjects.length == 0){
Window.alert("Select the item(s) to color label");
//$.write("No objects selected\n");
return -1;
}
//Error precaution: Remove invalid items from selection set
for( var obj in selectedObjects ) {
if(selectedObjects[obj].typename != "PathItem"){
//$.write( "Object number " + obj + " has been removed from selection due to invalid type: ");
//$.writeln(selectedObjects[obj].toString());
selectedObjects[obj].selected = false;
}
}
//Refresh our object array after removing the invalid items
selectedObjects = app.activeDocument.selection;
//Sanity Check: Ensure there is still items selected
if(selectedObjects.length == 0){
Window.alert("No eligible objects selected");
//$.write("Wrong object types selected\n");
return -1;
}
//QC Check: Ensure that the items being labeled are fairly normal
if(!selectedObjects[0].closed){
Window.alert("Error: Path must be closed");
//$.write("Path not closed\n");
return -1;
}
if(!selectedObjects[0].filled){
Window.alert("Error: Path must be filled");
//$.write("Path not filled\n");
return -1;
}
//QC Check END
}
// Loop over selected items and put a colorText label on them
function writeLabels() {
var selectedObjects = app.activeDocument.selection;
try{
var charStyle = app.activeDocument.characterStyles.getByName("Label");
} catch(err) {
var charStyle = app.activeDocument.characterStyles.add("Label");
}
var charAtter = charStyle.characterAttributes;
charAtter.size = 120;
for( var obj in selectedObjects ) {
var x_chord = selectedObjects[obj].position[0];
var y_chord = selectedObjects[obj].position[1] - selectedObjects[obj].height - gTextMargin;
var colorText = app.activeDocument.textFrames.add(); //create the colorText label and make it look right
colorText.position = [x_chord , y_chord];
colorText.contents = getColorText(selectedObjects[obj] , obj);
var bounds = selectedObjects[obj].geometricBounds;
//$.write(bounds + "\n");
var width = bounds[2]-bounds[0];
width = Math.abs(width);
textSize = width * (4 / 78);
if(textSize < charAtter.size){
charAtter.size = textSize;
}
charStyle.applyTo(colorText.textRange);
}
app.activeDocument.characterStyles.removeAll();
}
// Lookup and return the color label depending on the fill color type
function getColorText(currentObj, count) {
count++; // Shift the count up by one so that its more readable
var resultText = count.toString() + ": ";
if(currentObj.fillColor.typename == "SpotColor"){
resultText += currentObj.fillColor.spot.toString() + " α[";
resultText += currentObj.opacity.toFixed(PrecisionLength).toString() + "]";
} else if(currentObj.fillColor.typename == "CMYKColor"){
resultText += " C[" + currentObj.fillColor.cyan.toFixed(PrecisionLength).toString() + "]";
resultText += " M[" + currentObj.fillColor.magenta.toFixed(PrecisionLength).toString() + "]";
resultText += " Y[" + currentObj.fillColor.yellow.toFixed(PrecisionLength).toString() + "]";
resultText += " K[" + currentObj.fillColor.black.toFixed(PrecisionLength).toString() + "]";
resultText += " α[" + currentObj.opacity.toFixed(PrecisionLength).toString() + "]";
} else if(currentObj.fillColor.typename == "RGBColor"){
resultText += " R[" + currentObj.fillColor.red.toFixed(PrecisionLength).toString() + "]";
resultText += " G[" + currentObj.fillColor.green.toFixed(PrecisionLength).toString() + "]";
resultText += " B[" + currentObj.fillColor.blue.toFixed(PrecisionLength).toString() + "]";
resultText += " α[" + currentObj.opacity.toFixed(PrecisionLength).toString() + "]";
} else if(currentObj.fillColor.typename == "LabColor"){
resultText += " L[" + currentObj.fillColor.l.toFixed(PrecisionLength).toString() + "]";
resultText += " A[" + currentObj.fillColor.a.toFixed(PrecisionLength).toString() + "]";
resultText += " B[" + currentObj.fillColor.b.toFixed(PrecisionLength).toString() + "]";
resultText += " α[" + currentObj.opacity.toFixed(PrecisionLength).toString() + "]";
} else if(currentObj.fillColor.typename == "GrayColor"){
resultText += " K[" + currentObj.fillColor.gray.toFixed(PrecisionLength).toString() + "]";
resultText += " α[" + currentObj.opacity.toFixed(PrecisionLength).toString() + "]";
} else if(currentObj.fillColor.typename == "GradientColor"){
resultText += "Gradients not supported";
} else if(currentObj.fillColor.typename == "NoColor"){
resultText += "No Color";
} else if(currentObj.fillColor.typename == "PatternColor"){
resultText += "Pattern fill not supported";
}
return resultText;
}