Advertisement
Guest User

Custom export illustrator

a guest
Sep 29th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var docRef = app.activeDocument;
  2.  
  3. var c = 0; // nombre de path
  4.     while(true){
  5.         try{
  6.            docRef.layers[0].pathItems[++c];
  7.             }catch(e){
  8.                 break;
  9.             }                  
  10.       }
  11. var pathPoints = [];
  12. var toWrite = [];
  13. toWrite.lenght = 0;
  14. for(i = 0; i < c; i++){//go though each path and get it's path points
  15.         pathPoints.push(docRef.pathItems[i].pathPoints);
  16.         fillLenght (pathPoints[i]);
  17.  }
  18.  
  19. for(i = 0; i < c; i++){
  20.         var shape = {'r':0.0, 'g':0.0,'b':0.0,'points':[]};
  21.         shape.points.lenght = 0;
  22.         var color = docRef.pathItems[i].fillColor;
  23.        
  24.         if(color.typename === "CMYKColor"){ //convert CMYK color to RGB
  25.            cyan = color.cyan / 100;
  26.             m = color.magenta / 100;
  27.             y = color.yellow / 100;
  28.             k = color.black / 100;
  29.            
  30.             //r = 1 - Math.min( 1, cyan * ( 1 - k ) + k );
  31.             //g = 1 - Math.min( 1, m * ( 1 - k ) + k );
  32.             //b = 1 - Math.min( 1, y * ( 1 - k ) + k );
  33.             r = 255 * (1-cyan) * (1-k);
  34.             g = 255 * (1-m) * (1-k);
  35.             b = 255 * (1-y) * (1-k);
  36.      
  37.             r = Math.round(r);
  38.             g = Math.round(g);
  39.             b = Math.round(b);
  40.             shape.r = r;
  41.             shape.g = g;
  42.             shape.b  = b;
  43.         }else{
  44.                 shape.r = color.r;
  45.                 shape.g = color.g;
  46.                 shape.b = color.b;
  47.         }
  48.        
  49.         for(k = 0; k < pathPoints[i].lenght; k++){
  50.                var n = {
  51.                    'x':0.0, 'y':0.0
  52.                };
  53.                n.x = pathPoints[i][k].anchor[0] / docRef.width
  54.                n.y = pathPoints[i][k].anchor[1] / docRef.height;
  55.                shape.points.push(n);
  56.                shape.points.lenght++;
  57.         }
  58.     toWrite.push(shape);
  59.     toWrite.lenght++;
  60. }
  61.  
  62. str = '';//encoding
  63. for(i = 0; i < toWrite.lenght; i++){
  64.         str += toWrite[i].r + ':' + toWrite[i].g + ':' + toWrite[i].b + 'a';
  65.         for(k = 0; k < toWrite[i].points.lenght; k++){
  66.                 str += toWrite[i].points[k].x + '!' + toWrite[i].points[k].y;
  67.                 if(k != toWrite[i].points.lenght -1){
  68.                         str += '}';
  69.                 }
  70.         }
  71.         if(i != toWrite.lenght -1){
  72.                 str += '*';
  73.         }
  74. }
  75. str += '$';
  76. var points = docRef.layers[1].pathItems[0].pathPoints;
  77. fillLenght(points);
  78. for(i = 0; i < points.lenght; i++){
  79.         var x = points[i].anchor[0] / docRef.width;
  80.         var y = points[i].anchor[1] / docRef.height;
  81.         str += x + '!' + y;
  82.         if(i != points.lenght -1)
  83.             str += '}';
  84. }
  85.  
  86. var destFolder = null;//save to disk
  87. destFolder = Folder.selectDialog('Select the directory to save the file.', 'D:\\unity\\testing\\Shaders\\Assets\\Resources');
  88. if(destFolder != null){
  89.         var reportFile = new File(destFolder + '/Icon.txt');
  90.         reportFile.open('w');
  91.         reportFile.writeln(str);
  92.         reportFile.close();
  93.         alert('done');
  94. }
  95.  
  96.  
  97. function fillLenght(points){
  98.         var c = 0;
  99.         while(true){
  100.                 try{
  101.                         points[++c].anchor[0];
  102.                     }catch(e){
  103.                         points.lenght = c;
  104.                         break;
  105.                     }
  106.         }    
  107. }
  108.  
  109. function getColorRGB(color){
  110.         if(color.typename === "CMYKColor"){
  111.             c = color.cyan / 100;
  112.             m = color.magenta / 100;
  113.             y = color.yellow / 100;
  114.             k = color.black / 100;
  115.      
  116.             r = 1 - Math.min( 1, c * ( 1 - k ) + k );
  117.             g = 1 - Math.min( 1, m * ( 1 - k ) + k );
  118.             b = 1 - Math.min( 1, y * ( 1 - k ) + k );
  119.      
  120.             r = Math.round(r * 255 );
  121.             g = Math.round(g * 255 );
  122.             b = Math.round(b * 255 );
  123.             alert('color :' + r + ':' + g + ':' + b);
  124.             return {'r' : r, 'g':g, 'b' : b};
  125.         }else if(color.typename === "RGBColor") {
  126.             return color;
  127.         }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement