Create Gradients with Custom Step Number & Color via PS Scripting
Hello,
I'm trying to make a new gradient which should be added in gradient preset manager. I would like add more user defined gradient steps and unique colors.
Using xbytors action to javascript tool I was able to create script (at the bottom of post.) But as it gets so complex for more steps controlling them become harder, so I thought there should be a way to put following lines under a function...(I noticed that those are the lines for defining rgb color and location, and it repeating as I add more steps..)
var desc4 = new ActionDescriptor();
var desc5 = new ActionDescriptor();
desc5.putDouble(cTID('Rd '), 0);
desc5.putDouble(cTID('Grn '), 0);
desc5.putDouble(cTID('Bl '), 0);
desc4.putObject(cTID('Clr '), sTID("RGBColor"), desc5);
desc4.putEnumerated(cTID('Type'), cTID('Clry'), cTID('UsrS'));
desc4.putInteger(cTID('Lctn'), 0);
desc4.putInteger(cTID('Mdpn'), 50);
list1.putObject(cTID('Clrt'), desc4);
I tried to convert it to a function like this; ( Should I give desc5,desc4 variables unique names? )
function addgradientstep(redcolor,greencolor,bluecolor,location) {
var desc4 = new ActionDescriptor();
var desc5 = new ActionDescriptor();
desc5.putDouble(cTID('Rd '), 0); // Red Color
desc5.putDouble(cTID('Grn '), 0); // Green Color
desc5.putDouble(cTID('Bl '), 0); // Blue Color
desc4.putObject(cTID('Clr '), sTID("RGBColor"), desc5);
desc4.putEnumerated(cTID('Type'), cTID('Clry'), cTID('UsrS'));
desc4.putInteger(cTID('Lctn'), 0); // Gradient Step Location
desc4.putInteger(cTID('Mdpn'), 50);
list1.putObject(cTID('Clrt'), desc4);
}
And I thought I would call them like; ( I don't need to do anything with transparency options by the way, only color steps...) ( It gave an error at this line ;
list1.putObject(cTID('Clrt'), desc4);
)
step1();
addgradientstep(255,255,255,0);
addgradientstep(0,0,0,25);
addgradientstep(128,128,128,50);
step2();
....
Full script;
#target photoshop
cTID = function(s) { return app.charIDToTypeID(s); };sTID = function(s) { return app.stringIDToTypeID(s); };
var steps = 10;
function Simple_Gradient() {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putClass(cTID('Grdn'));
desc1.putReference(cTID('null'), ref1);
var desc2 = new ActionDescriptor();
var desc3 = new ActionDescriptor();
desc3.putString(cTID('Nm '), "GradientName");
desc3.putEnumerated(cTID('GrdF'), cTID('GrdF'), cTID('CstS'));
desc3.putDouble(cTID('Intr'), 4096);
var list1 = new ActionList();
var desc4 = new ActionDescriptor();
var desc5 = new ActionDescriptor();
desc5.putDouble(cTID('Rd '), 0);
desc5.putDouble(cTID('Grn '), 0);
desc5.putDouble(cTID('Bl '), 0);
desc4.putObject(cTID('Clr '), sTID("RGBColor"), desc5);
desc4.putEnumerated(cTID('Type'), cTID('Clry'), cTID('UsrS'));
desc4.putInteger(cTID('Lctn'), 0);
desc4.putInteger(cTID('Mdpn'), 50);
list1.putObject(cTID('Clrt'), desc4);
var desc6 = new ActionDescriptor();
var desc7 = new ActionDescriptor();
desc7.putDouble(cTID('Rd '), 255);
desc7.putDouble(cTID('Grn '), 255);
desc7.putDouble(cTID('Bl '), 255);
desc6.putObject(cTID('Clr '), sTID("RGBColor"), desc7);
desc6.putEnumerated(cTID('Type'), cTID('Clry'), cTID('UsrS'));
desc6.putInteger(cTID('Lctn'), 4096);
desc6.putInteger(cTID('Mdpn'), 50);
list1.putObject(cTID('Clrt'), desc6);
desc3.putList(cTID('Clrs'), list1);
var list2 = new ActionList();
var desc8 = new ActionDescriptor();
desc8.putUnitDouble(cTID('Opct'), cTID('#Prc'), 100); // Start Opacity
desc8.putInteger(cTID('Lctn'), 0); // Location
desc8.putInteger(cTID('Mdpn'), 50); // Mid Point
list2.putObject(cTID('TrnS'), desc8);
var desc9 = new ActionDescriptor();
desc9.putUnitDouble(cTID('Opct'), cTID('#Prc'), 38.0392156862745); // End Opacity
desc9.putInteger(cTID('Lctn'), 4096); // Location
desc9.putInteger(cTID('Mdpn'), 50); // Mid Point
list2.putObject(cTID('TrnS'), desc9);
desc3.putList(cTID('Trns'), list2);
desc2.putObject(cTID('Grad'), cTID('Grdn'), desc3);
desc1.putObject(cTID('Usng'), cTID('Grdn'), desc2);
executeAction(cTID('Mk '), desc1, DialogModes.NO);
};
Simple_Gradient();
// for (i = 0; i < steps; i++){}
Thank you,
Best Regards.
