// create shape layer consisting of one rectangle and one triangle based on the last two colorsamplers; // 2016, use it at your own risk; #target photoshop if (app.documents.length > 0) { var myDocument = app.activeDocument; if (myDocument.colorSamplers.length > 1) { // pixels,; var originalRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.PIXELS var originalResolution = myDocument.resolution; myDocument.resizeImage (null, null, 72, ResampleMethod.NONE); var halfWidth = 25; var halfHeight = 12.5; // get coordinates; var theCoord1 = myDocument.colorSamplers[myDocument.colorSamplers.length-2].position; var theCoord2 = myDocument.colorSamplers[myDocument.colorSamplers.length-1].position; // define rectangle; var point1 = [theCoord1[0]-halfWidth,theCoord1[1]-halfHeight]; var point2 = [theCoord1[0]+halfWidth,theCoord1[1]-halfHeight]; var point3 = [theCoord1[0]+halfWidth,theCoord1[1]+halfHeight]; var point4 = [theCoord1[0]-halfWidth,theCoord1[1]+halfHeight]; var subPath1 = [[point1, point1, point1, false], [point2, point2, point2, false], [point3, point3, point3, false], [point4, point4, point4, false], true, 1097098272]; // get angle; var theAngle = getAngle(theCoord1, theCoord2); //alert (theAngle); // define triangle ponts; switch (true) { case (theAngle >= 27 && theAngle < 153): var tr1 = [theCoord1[0]-(halfWidth/3), theCoord1[1]+halfHeight]; var tr2 = [theCoord1[0]+(halfWidth/3), theCoord1[1]+halfHeight]; break; case (theAngle >= 153 && theAngle < 207): var tr1 = [theCoord1[0]-halfWidth, theCoord1[1]-(halfHeight/3)]; var tr2 = [theCoord1[0]-halfWidth, theCoord1[1]+(halfHeight/3)]; break; case (theAngle >= 207 && theAngle < 333): var tr1 = [theCoord1[0]-(halfWidth/3), theCoord1[1]-halfHeight]; var tr2 = [theCoord1[0]+(halfWidth/3), theCoord1[1]-halfHeight]; break; default: var tr1 = [theCoord1[0]+halfWidth, theCoord1[1]-(halfHeight/3)]; var tr2 = [theCoord1[0]+halfWidth, theCoord1[1]+(halfHeight/3)]; break; }; var subPath2 = [[tr1, tr1, tr1, false], [tr2, tr2, tr2, false], [theCoord2, theCoord2, theCoord2, false], true, 1097098272]; var theArray = [subPath1, subPath2]; // create path; var aPath = createPath2015(theArray, Math.random()); var theShape = solidColorLayer (5, 128, 205); aPath.remove(); // remove color samplers; myDocument.colorSamplers[myDocument.colorSamplers.length-1].remove(); myDocument.colorSamplers[myDocument.colorSamplers.length-1].remove(); // reset; app.preferences.rulerUnits = originalRulerUnits; myDocument.resizeImage (null, null, originalResolution, ResampleMethod.NONE); }; }; ////// create a path from collectPathInfoFromDesc2012-array ////// function createPath2015(theArray, thePathsName) { var originalRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.POINTS; // thanks to xbytor; cTID = function(s) { return app.charIDToTypeID(s); }; sTID = function(s) { return app.stringIDToTypeID(s); }; var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putProperty(cTID('Path'), cTID('WrPt')); desc1.putReference(sTID('null'), ref1); var list1 = new ActionList(); for (var m = 0; m < theArray.length; m++) { var thisSubPath = theArray; var desc2 = new ActionDescriptor(); desc2.putEnumerated(sTID('shapeOperation'), sTID('shapeOperation'), thisSubPath[thisSubPath.length - 1]); var list2 = new ActionList(); var desc3 = new ActionDescriptor(); desc3.putBoolean(cTID('Clsp'), thisSubPath[thisSubPath.length - 2]); var list3 = new ActionList(); for (var n = 0; n < thisSubPath.length - 2; n++) { var thisPoint = thisSubPath; var desc4 = new ActionDescriptor(); var desc5 = new ActionDescriptor(); desc5.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[0][0]); desc5.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[0][1]); desc4.putObject(cTID('Anch'), cTID('Pnt '), desc5); var desc6 = new ActionDescriptor(); desc6.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[1][0]); desc6.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[1][1]); desc4.putObject(cTID('Fwd '), cTID('Pnt '), desc6); var desc7 = new ActionDescriptor(); desc7.putUnitDouble(cTID('Hrzn'), cTID('#Rlt'), thisPoint[2][0]); desc7.putUnitDouble(cTID('Vrtc'), cTID('#Rlt'), thisPoint[2][1]); desc4.putObject(cTID('Bwd '), cTID('Pnt '), desc7); desc4.putBoolean(cTID('Smoo'), thisPoint[3]); list3.putObject(cTID('Pthp'), desc4); }; desc3.putList(cTID('Pts '), list3); list2.putObject(cTID('Sbpl'), desc3); desc2.putList(cTID('SbpL'), list2); list1.putObject(cTID('PaCm'), desc2); }; desc1.putList(cTID('T '), list1); executeAction(cTID('setd'), desc1, DialogModes.NO); // name work path; var check = false; var x = activeDocument.pathItems.length - 1; while (check == false) { if (activeDocument.pathItems.kind == PathKind.WORKPATH) { app.activeDocument.pathItems.name = thePathsName; var myPathItem = app.activeDocument.pathItems; check = true }; x-- }; /*for (var x = 0; x < activeDocument.pathItems.length; x++) { if (activeDocument.pathItems.kind == PathKind.WORKPATH) { app.activeDocument.pathItems.name = thePathsName; var myPathItem = app.activeDocument.pathItems } };*/ app.preferences.rulerUnits = originalRulerUnits; return myPathItem }; ////// get a distance between two points ////// function getDistance (pointOne, pointTwo) { // calculate the triangle sides; var width = pointTwo[0] - pointOne[0]; var height = pointTwo[1] - pointOne[1]; var sideC = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); return sideC }; ////// get an angle, 3:00 being 0˚, 6:00 90˚, etc. ////// function getAngle (pointOne, pointTwo) { // calculate the triangle sides; var width = pointTwo[0] - pointOne[0]; var height = pointTwo[1] - pointOne[1]; var sideC = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); // calculate the angles; if (width+width > width) {theAngle = Math.asin(height / sideC) * 360 / 2 / Math.PI} else {theAngle = 180 - (Math.asin(height / sideC) * 360 / 2 / Math.PI)}; if (theAngle < 0) {theAngle = (360 + theAngle)}; // if (theAngle > 180) {theAngle = (360 - theAngle) * (-1)}; return theAngle }; ////// create solid color layer ////// function solidColorLayer (theR, theG, theB) { // solid color layer; // ======================================================= var idMk = charIDToTypeID( "Mk " ); var desc16 = new ActionDescriptor(); var idnull = charIDToTypeID( "null" ); var ref4 = new ActionReference(); var idcontentLayer = stringIDToTypeID( "contentLayer" ); ref4.putClass( idcontentLayer ); desc16.putReference( idnull, ref4 ); var idUsng = charIDToTypeID( "Usng" ); var desc17 = new ActionDescriptor(); var idType = charIDToTypeID( "Type" ); var desc18 = new ActionDescriptor(); var idClr = charIDToTypeID( "Clr " ); var desc19 = new ActionDescriptor(); var idRd = charIDToTypeID( "Rd " ); desc19.putDouble( idRd, theR ); var idGrn = charIDToTypeID( "Grn " ); desc19.putDouble( idGrn, theG ); var idBl = charIDToTypeID( "Bl " ); desc19.putDouble( idBl, theB ); var idRGBC = charIDToTypeID( "RGBC" ); desc18.putObject( idClr, idRGBC, desc19 ); var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" ); desc17.putObject( idType, idsolidColorLayer, desc18 ); var idcontentLayer = stringIDToTypeID( "contentLayer" ); desc16.putObject( idUsng, idcontentLayer, desc17 ); executeAction( idMk, desc16, DialogModes.NO ); return activeDocument.activeLayer }; ////// sort a double array, thanks to sam, http://www.rhinocerus.net/forum/lang-javascript/ ////// function sortArrayByIndexedItem(a,b) { //var theIndex = 1; if (a[1]<b[1]) return -1; if (a[1]>b[1]) return 1; return 0; }; |