Skip to main content
Inspiring
March 22, 2017
Question

Create Gradients with Custom Step Number & Color via PS Scripting

  • March 22, 2017
  • 1 reply
  • 2180 views

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.

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
March 26, 2017

Does this help?

// 2017, use it at your own risk;

#target photoshop

var theGradient = gradientLayer([[0,0,0,0], [1000,128,128,255], [2000,255,255,0]]);

function gradientLayer(theArray) {

// =======================================================

var idClr = charIDToTypeID( "Clr " );

var idRd = charIDToTypeID( "Rd  " );

var idGrn = charIDToTypeID( "Grn " );

var idBl = charIDToTypeID( "Bl  " );

var idRGBC = charIDToTypeID( "RGBC" );

var idLctn = charIDToTypeID( "Lctn" );

var idMdpn = charIDToTypeID( "Mdpn" );

var idOpct = charIDToTypeID( "Opct" );

var idPrc = charIDToTypeID( "#Prc" );

var idTrnS = charIDToTypeID( "TrnS" );

var idType = charIDToTypeID( "Type" );

var idMk = charIDToTypeID( "Mk  " );

    var desc10 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

        ref1.putClass( idcontentLayer = stringIDToTypeID( "contentLayer" ) );

    desc10.putReference( idnull, ref1 );

    var idUsng = charIDToTypeID( "Usng" );

        var desc11 = new ActionDescriptor();

        var idType = charIDToTypeID( "Type" );

            var desc12 = new ActionDescriptor();

            desc12.putBoolean( charIDToTypeID( "Dthr" ), true );

            desc12.putUnitDouble( charIDToTypeID( "Angl" ), charIDToTypeID( "#Ang" ), 0.000000 );

            desc12.putEnumerated( idType, charIDToTypeID( "GrdT" ), charIDToTypeID( "Lnr " ) );

            var idGrad = charIDToTypeID( "Grad" );

                var desc13 = new ActionDescriptor();

                var idNm = charIDToTypeID( "Nm  " );

                desc13.putString( idNm, "Custom" );

                var idGrdF = charIDToTypeID( "GrdF" );

                desc13.putEnumerated( idGrdF, idGrdF, charIDToTypeID( "CstS" ) );

                var idIntr = charIDToTypeID( "Intr" );

                desc13.putDouble( idIntr, 4096.000000 );

                    var list1 = new ActionList();

// insert color stops;

for (var m = 0; m < theArray.length; m++) {

var desc14 = new ActionDescriptor();

var desc15 = new ActionDescriptor();

desc15.putDouble( idRd, theArray[1] );

desc15.putDouble( idGrn, theArray[2] );

desc15.putDouble( idBl, theArray[3] );

desc14.putObject( idClr, idRGBC, desc15 );

var idClry = charIDToTypeID( "Clry" );

var idUsrS = charIDToTypeID( "UsrS" );

desc14.putEnumerated( idType, idClry, idUsrS );

desc14.putInteger( idLctn, theArray[0] );

desc14.putInteger( idMdpn, 50 );

var idClrt = charIDToTypeID( "Clrt" );

list1.putObject( idClrt, desc14 );

};

                desc13.putList( charIDToTypeID( "Clrs" ), list1 );

                var idTrns = charIDToTypeID( "Trns" );

                    var list2 = new ActionList();

                        var desc18 = new ActionDescriptor();

                        desc18.putUnitDouble( idOpct, idPrc, 100.000000 );

                        desc18.putInteger( idLctn, 0 );

                        desc18.putInteger( idMdpn, 50 );

                    list2.putObject( idTrnS, desc18 );

                        var desc19 = new ActionDescriptor();

                        desc19.putUnitDouble( idOpct, idPrc, 100.000000 );

                        desc19.putInteger( idLctn, 100 );

                        desc19.putInteger( idMdpn, 50 );

                    list2.putObject( idTrnS, desc19 );

                desc13.putList( idTrns, list2 );

            var idGrdn = charIDToTypeID( "Grdn" );

            desc12.putObject( idGrad, idGrdn, desc13 );

        desc11.putObject( idType, stringIDToTypeID( "gradientLayer" ), desc12 );

    desc10.putObject( idUsng,  stringIDToTypeID( "contentLayer" ), desc11 );

executeAction( idMk, desc10, DialogModes.NO );

return activeDocument.activeLayer

};