選択アイテムを位置情報からテーブル(x,y)として扱えるようにする
以前作ったスクリプトを改良して、
選択したアイテムの位置情報からテーブル(とりあえず、左上原点)の様にあつかえる様にしました。
使い方はこんな感じ。
//アイテムを渡してグループを生成。 table_group = new hvg(app.activeDocument.selection); //上からY番目、左からX番目のアイテムを取り出す。 item = table_group.getItem(X,Y); //eachも用意しました。 while(item = table_group.each()){ //itemに対しての処理.. }
サンプル
左上から順番に用意した色を順番に適用していきます。
// hvg = function (sel){ if(sel.length == 0) return false; this.group = []; this.p_x = 0; this.p_y = 0; var slen = sel.length; var hl = new Number(); var grouping = new Array(); var order = function(a,b){return a['left'] - b ['left']} var gorder = function(a,b){return b['n'] - a ['n']} //order = function(a,b){return b['top'] - a ['top']} for(var i = 0; i <slen ; i++)hl += Math.abs(sel[i].visibleBounds[1]-sel[i].visibleBounds[3]); hl = hl/slen; var test = function (){ for(var j = 0; j < grouping.length ; j ++){ var d = Math.abs(ic - grouping[j].n); if(d < hl ){ grouping[j].item.push(sel[i]); return; } } grouping.push({n :ic , item : [sel[i]]}); return ; } for(var i = 0; i < slen ; i++){ var ic = (sel[i].visibleBounds[3]+sel[i].visibleBounds[1])/2; test(); } for(var i = 0; i < grouping.length; i++){ temp = new Array(); for(var j = 0; j < grouping[i].item.length;j++){temp.push(grouping[i].item[j])} grouping[i].item = temp.sort (order); } this.group = grouping.sort(gorder); } hvg.prototype.getItem = function(y,x){ try{ return this.group[y].item[x]; }catch(e){ return false; } } hvg.prototype.each = function(){ if(this.group[this.p_y].item.length > this.p_x){ get = this.getItem(this.p_y,this.p_x); this.p_x++; }else{ this.p_y++; this.p_x = 0; if(this.group.length > this.p_y){ get = this.getItem(this.p_y,this.p_x); this.p_x++; }else{ return false; } } return get; } //Making CMYK cmyk = function(c,m,y,k){ var cmyk = new CMYKColor(); cmyk.cyan = c; cmyk.magenta = m; cmyk.yellow = y; cmyk.black = k; return cmyk; } //Color Group col_g = { pos : 0, colors : [new cmyk(60,10,0,0), new cmyk(0,70,15,0), new cmyk(0,30,70,0),new cmyk(60,0,40,0),new cmyk(20,0,100,0)], loop : function(){ if(this.pos < this.colors.length){ color =this.colors[this.pos] this.pos++; }else{ this.pos = 0; color =this.colors[this.pos] this.pos++; } return color; } } //main table_g = new hvg(app.activeDocument.selection); while(get = table_g.each()){ get.fillColor = col_g.loop(); }