6 Replies Latest reply on Mar 27, 2012 8:37 AM by Levianth

    How can I create a new layer that is a gradient?

    Levianth

      Hi, i'm very new to photoshop scripting and am having some trouble.

       

      I'm looking for a way to take an image i have and set it to have a gradient opacity as it approaches the middle, my thought on how to do that was to just create a layer that is a gradient from top left to bottom right and then attach that as a vector mask.

       

      Any ideas on how I could create this gradient layer in script, or a better method of doing this opacity gradient?

       

      Thanks in advance,

      Levianth

        • 1. Re: How can I create a new layer that is a gradient?
          c.pfaffenbichler Level 9

          just create a layer that is a gradient from top left to bottom right and then attach that as a vector mask.

          Hardly, a Vector Mask needs a Path (Edit: and is sharp-edged, or rather as sharp as one defines in the Masks Panel).

           

          You could use ScriptingListener.plugin to record the creation of a Gradient Layer and use that code combined with DOM-code (to move the Gradient Layer behind the originally active Layer and make a Clipping Mask Group).

          • 2. Re: How can I create a new layer that is a gradient?
            c.pfaffenbichler Level 9

            You could try this:

            // 2012, use it at your own risk;
            #target photoshop
            if (app.documents.length > 0) {
            var myDocument = app.activeDocument;
            var theLayer = myDocument.activeLayer;
            if (theLayer.isBackgroundLayer == true) {theLayer.isBackgroundLayer = false};
            // create gradient layer;
            // =======================================================
            var idMk = charIDToTypeID( "Mk  " );
                var desc15 = new ActionDescriptor();
                var idnull = charIDToTypeID( "null" );
                    var ref3 = new ActionReference();
                    var idcontentLayer = stringIDToTypeID( "contentLayer" );
                    ref3.putClass( idcontentLayer );
                desc15.putReference( idnull, ref3 );
                var idUsng = charIDToTypeID( "Usng" );
                    var desc16 = new ActionDescriptor();
                    var idType = charIDToTypeID( "Type" );
                        var desc17 = new ActionDescriptor();
                        var idType = charIDToTypeID( "Type" );
                        var idGrdT = charIDToTypeID( "GrdT" );
                        var idLnr = charIDToTypeID( "Lnr " );
                        desc17.putEnumerated( idType, idGrdT, idLnr );
                        var idGrad = charIDToTypeID( "Grad" );
                            var desc18 = new ActionDescriptor();
                            var idNm = charIDToTypeID( "Nm  " );
                            desc18.putString( idNm, "Custom" );
                            var idGrdF = charIDToTypeID( "GrdF" );
                            var idGrdF = charIDToTypeID( "GrdF" );
                            var idCstS = charIDToTypeID( "CstS" );
                            desc18.putEnumerated( idGrdF, idGrdF, idCstS );
                            var idIntr = charIDToTypeID( "Intr" );
                            desc18.putDouble( idIntr, 4096.000000 );
                            var idClrs = charIDToTypeID( "Clrs" );
                                var list3 = new ActionList();
                                    var desc19 = new ActionDescriptor();
                                    var idClr = charIDToTypeID( "Clr " );
                                        var desc20 = new ActionDescriptor();
                                        var idRd = charIDToTypeID( "Rd  " );
                                        desc20.putDouble( idRd, 0.000000 );
                                        var idGrn = charIDToTypeID( "Grn " );
                                        desc20.putDouble( idGrn, 0.000000 );
                                        var idBl = charIDToTypeID( "Bl  " );
                                        desc20.putDouble( idBl, 0.000000 );
                                    var idRGBC = charIDToTypeID( "RGBC" );
                                    desc19.putObject( idClr, idRGBC, desc20 );
                                    var idType = charIDToTypeID( "Type" );
                                    var idClry = charIDToTypeID( "Clry" );
                                    var idUsrS = charIDToTypeID( "UsrS" );
                                    desc19.putEnumerated( idType, idClry, idUsrS );
                                    var idLctn = charIDToTypeID( "Lctn" );
                                    desc19.putInteger( idLctn, 0 );
                                    var idMdpn = charIDToTypeID( "Mdpn" );
                                    desc19.putInteger( idMdpn, 50 );
                                var idClrt = charIDToTypeID( "Clrt" );
                                list3.putObject( idClrt, desc19 );
                                    var desc21 = new ActionDescriptor();
                                    var idClr = charIDToTypeID( "Clr " );
                                        var desc22 = new ActionDescriptor();
                                        var idRd = charIDToTypeID( "Rd  " );
                                        desc22.putDouble( idRd, 0.000000 );
                                        var idGrn = charIDToTypeID( "Grn " );
                                        desc22.putDouble( idGrn, 0.000000 );
                                        var idBl = charIDToTypeID( "Bl  " );
                                        desc22.putDouble( idBl, 0.000000 );
                                    var idRGBC = charIDToTypeID( "RGBC" );
                                    desc21.putObject( idClr, idRGBC, desc22 );
                                    var idType = charIDToTypeID( "Type" );
                                    var idClry = charIDToTypeID( "Clry" );
                                    var idUsrS = charIDToTypeID( "UsrS" );
                                    desc21.putEnumerated( idType, idClry, idUsrS );
                                    var idLctn = charIDToTypeID( "Lctn" );
                                    desc21.putInteger( idLctn, 4096 );
                                    var idMdpn = charIDToTypeID( "Mdpn" );
                                    desc21.putInteger( idMdpn, 50 );
                                var idClrt = charIDToTypeID( "Clrt" );
                                list3.putObject( idClrt, desc21 );
                            desc18.putList( idClrs, list3 );
                            var idTrns = charIDToTypeID( "Trns" );
                                var list4 = new ActionList();
                                    var desc23 = new ActionDescriptor();
                                    var idOpct = charIDToTypeID( "Opct" );
                                    var idPrc = charIDToTypeID( "#Prc" );
                                    desc23.putUnitDouble( idOpct, idPrc, 0.000000 );
                                    var idLctn = charIDToTypeID( "Lctn" );
                                    desc23.putInteger( idLctn, 0 );
                                    var idMdpn = charIDToTypeID( "Mdpn" );
                                    desc23.putInteger( idMdpn, 50 );
                                var idTrnS = charIDToTypeID( "TrnS" );
                                list4.putObject( idTrnS, desc23 );
                                    var desc24 = new ActionDescriptor();
                                    var idOpct = charIDToTypeID( "Opct" );
                                    var idPrc = charIDToTypeID( "#Prc" );
                                    desc24.putUnitDouble( idOpct, idPrc, 100.000000 );
                                    var idLctn = charIDToTypeID( "Lctn" );
                                    desc24.putInteger( idLctn, 2048 );
                                    var idMdpn = charIDToTypeID( "Mdpn" );
                                    desc24.putInteger( idMdpn, 50 );
                                var idTrnS = charIDToTypeID( "TrnS" );
                                list4.putObject( idTrnS, desc24 );
                                    var desc25 = new ActionDescriptor();
                                    var idOpct = charIDToTypeID( "Opct" );
                                    var idPrc = charIDToTypeID( "#Prc" );
                                    desc25.putUnitDouble( idOpct, idPrc, 0.000000 );
                                    var idLctn = charIDToTypeID( "Lctn" );
                                    desc25.putInteger( idLctn, 4096 );
                                    var idMdpn = charIDToTypeID( "Mdpn" );
                                    desc25.putInteger( idMdpn, 50 );
                                var idTrnS = charIDToTypeID( "TrnS" );
                                list4.putObject( idTrnS, desc25 );
                            desc18.putList( idTrns, list4 );
                        var idGrdn = charIDToTypeID( "Grdn" );
                        desc17.putObject( idGrad, idGrdn, desc18 );
                    var idgradientLayer = stringIDToTypeID( "gradientLayer" );
                    desc16.putObject( idType, idgradientLayer, desc17 );
                var idcontentLayer = stringIDToTypeID( "contentLayer" );
                desc15.putObject( idUsng, idcontentLayer, desc16 );
            executeAction( idMk, desc15, DialogModes.NO );
            // move layer below;
            var theGradient = myDocument.activeLayer;
            theGradient.move(theLayer, ElementPlacement.PLACEAFTER);
            // clipping mask;
            theLayer.grouped = true
            };
            
            • 3. Re: How can I create a new layer that is a gradient?
              Levianth Level 1

              Thanks for the replies,

              My main concern with using something from the listener plugin is that this code needs to work on any image size, and the rate of gradient change will be different depending on what size the image is, is there a way to get that working with something recorded by ScriptingListener.plugin?

              • 4. Re: How can I create a new layer that is a gradient?
                c.pfaffenbichler Level 9

                Have you tested the Script?

                 

                It should create a gradient from 0% to 100% to 0% regardless of the dimensions of the image.

                If that is not what you want then please elaborate (screenshots may help illustrate your point).

                • 5. Re: How can I create a new layer that is a gradient?
                  Levianth Level 1

                  Just tested it and it works great thanks!

                   

                  ideally, the gradient the script would output would be a diamond similar to the one given in the attached image.

                  Sorry for the trouble, I'm just unsure how to modify the script output by the listener.

                   

                  http://dl.dropbox.com/u/677380/gradient%20fill.jpg

                  • 6. Re: How can I create a new layer that is a gradient?
                    Levianth Level 1

                    lawl jkay i did it ^_______________________________^ kuwai~