jqueryぽく書けたら便利そうなので書き直してみた
JavaScriptの勉強がてらjquery.jsを読み解くを読んだ。
まだ、最初の方でつまづいていますが。JavaScriptってこんな書き方ができたんだという発見があります。
でjqueryっぽく自前のスクリプトを書き直してみた。
書き直したスクリプト
// var hvg = function(sel){ return new hvg.prototype.init(sel); } hvg.fn = hvg.prototype = { 'init' : 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]); var 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); return this; }, //get Item to X,Y position 'getItem' : function(y,x){ try{ return this.group[y].item[x]; }catch(e){ return false; } }, 'next' : function(){ if(this.group[this.p_y].item.length > this.p_x){ var 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){ var get = this.getItem(this.p_y,this.p_x); this.p_x++; }else{ return false; } } return get; }, 'each' : function(fnc,args){ var i; if(typeof fnc == 'function')while(i = this.next())fnc.call(i,i,args); this.p_x = 0; this.p_y = 0; return this; } } hvg.prototype.init.prototype = hvg.prototype; //
こんな感じで記述可能に
/* #includepath 'path of lib' */ #include 'hvg.js'; hvg(activeDocument.selection).each( function setcolor (obj){ var CR = String.fromCharCode(13); var C = M = Y = K = ""; C = "C =" + Math.round(obj.fillColor.cyan); M = "M =" + Math.round(obj.fillColor.magenta); Y = "Y =" + Math.round(obj.fillColor.yellow); K = "K =" + Math.round(obj.fillColor.black); //$.bp(); myT = activeDocument.textFrames.add() myT.contents = (C+CR+M+CR+Y+CR+K); myT.left = obj.left+5; myT.top = obj.top-5; } );
やってることはこちら
色のCMYK成分を表示させてます。