forked from johnwun/js4ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeByLuminance.jsx
152 lines (141 loc) · 3.99 KB
/
sizeByLuminance.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
/////////////////////////////////////////////////////////////////
//Resize by Luminance v.1.0.0.0
//>=--------------------------------------
//
// Resizes all selected objects to a percentage of the objects original size
// new size is based on the relative brightness of the objects fillColor.
//
// aka: Make <b>Halftones in Illustrator</b>!!!
//
// Designed to be used with mosaics generated from
// embedded images.
// Good tutorial on making Mosaics <a target="_blank" href="http://garmahis.com/tutorials/how-to-create-mosaic-in-illustrator/">here</a>
// Further explanation on my <a href="http://js4ai.blogspot.com/2009/04/size-by-luminance-aka-halftones.html" target="_blank" >blog</a>
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( [email protected] ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//////////////////////////////////////////////////////////////////
var versionNum = app.version.split(".")[0];
var doc = activeDocument;
var sel = doc.selection;
var objectFail = 0;
var selLen = sel.length;
// get document colorspace:
if (doc.documentColorSpace==DocumentColorSpace.RGB)
{
averageColorSpace = averageRGB;
}else if (doc.documentColorSpace==DocumentColorSpace.CMYK)
{
averageColorSpace = averageCMYK;
}
if ( sel[0].typename == "GroupItem")
{
alert("You have selected a group item, please ungroup and try again.");
}else {
var msg = "Will the image be viewed against a black background?\n";
msg += " Yes = Lighter is bigger, darker is smaller\n";
msg += " No = Darker is bigger, lighter is smaller"
var whiteHeavy = confirm(msg);
if (whiteHeavy)
{
var match=0;
}else{
var match=255;
}
if (versionNum >= 13)
{
var w = new Window('window', "Resizing Objects", undefined, {independent:true});
w.frameLocation = [200,200];
w.tracker = w.add ('statictext' , [15,15,250,35], "processing");
w.prog = w.add ('progressbar' , [15,15,250,35], 0, selLen);
w.show();
}
//==========START LOOP====================
//step backwards so we can dynamically remove objects...
for (var each=selLen-1;each>=0;each--)
{
var gray = averageColors(sel[each].fillColor);
//alert(gray);
if (versionNum >= 13)
{
w.prog.value++;
w.tracker.text ="Resizing "+w.prog.value+" of "+selLen+" objects.";
}
//alert ("gray:"+gray+", match:"+match);
if (gray==match)
{
sel[each].remove();
}else{
//it must be a value:
// now reduce it to a percentace of 1.
gray = 1/(gray);
if ( !whiteHeavy)
{
gray = 1-gray;
}
// reassign
var ob = sel[each];
var otop=ob.top;
var oleft=ob.left;
var ow= ob.width;
var oh = ob.height;
ob.width *= gray;
ob.height *= gray;
ob.top = otop-(oh/2 - ob.height/2);
ob.left = oleft+(ow/2 - ob.width/2);
ob.selected=false;
}
}
//==============END LOOP===================
if (versionNum >= 13)
{
w.close();
}
if(objectFail ==1){
alert("Some objects were not understood.");
}
}//end if not groupitem
//====================END MAIN======================
function averageColors(col){
try{
if (col.typename=="GrayColor")
{
match=whiteHeavy ? 100 :0;
//invert it because 100 == black, not white like RGB...
out = 100/(100-col.gray);
if (out==Infinity)
{
out=100;
}
return out;
}else if (col.typename=="SpotColor"){
match= whiteHeavy? 100:0;
//invert it because 100 == tint, not white like RGB...
var out = 100/(100-col.tint);
if (out==Infinity)
{
out=100;
}
return out;
}
//if we got this far, it must be RGB or CMYK, otherwise, fail...
return(averageColorSpace(col));
} catch(e){
objectFail = 1;
return 100;
}
}
function averageRGB(col){
match=whiteHeavy? 0:255;
return 255/((col.red + col.green + col.blue)/3);
}
function averageCMYK(col){
match= whiteHeavy? 100:0;
// inverse because 100 = tint, not white like RGB
var out = 100/(100-((col.cyan + col.magenta + col.yellow + col.black)/4));
if (out==Infinity)
{
out=100;
}
return out;
}