Skip to main content
darkie1973
Inspiring
July 13, 2017
Answered

Utilize foreground/background colour in gradient fill

  • July 13, 2017
  • 2 replies
  • 1523 views

Hello everyone!

I am trying to make a gradient fill from a path or selection, the code (which I pulled from the scriptlistener), is filling gradient ONLY with the colour that was utilized when I recorded the action;  I would like the gradient fill to utilize the foreground/background colours that are currently designated in PS whenever the script is ran.  Anyone have any idea of how to do this?  I'm still quite new to scripting (I'm primarily a painter), but I have been able (with LOTS of help) generate a few scripts that are useful to my workflow.  I am using CS6; here is what I have thus far:

// Make Path from selection

var idMk = charIDToTypeID( "Mk  " );

    var desc405 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref221 = new ActionReference();

        var idPath = charIDToTypeID( "Path" );

        ref221.putClass( idPath );

    desc405.putReference( idnull, ref221 );

    var idFrom = charIDToTypeID( "From" );

        var ref222 = new ActionReference();

        var idcsel = charIDToTypeID( "csel" );

        var idfsel = charIDToTypeID( "fsel" );

        ref222.putProperty( idcsel, idfsel );

    desc405.putReference( idFrom, ref222 );

    var idTlrn = charIDToTypeID( "Tlrn" );

    var idPxl = charIDToTypeID( "#Pxl" );

    desc405.putUnitDouble( idTlrn, idPxl, 1.000000 );

executeAction( idMk, desc405, DialogModes.NO );

// Make Gradient fill from Path

var idMk = charIDToTypeID( "Mk  " );

    var desc1188 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref965 = new ActionReference();

        var idcontentLayer = stringIDToTypeID( "contentLayer" );

        ref965.putClass( idcontentLayer );

    desc1188.putReference( idnull, ref965 );

    var idUsng = charIDToTypeID( "Usng" );

        var desc1189 = new ActionDescriptor();

        var idType = charIDToTypeID( "Type" );

            var desc1190 = new ActionDescriptor();

            var idType = charIDToTypeID( "Type" );

            var idGrdT = charIDToTypeID( "GrdT" );

            var idLnr = charIDToTypeID( "Lnr " );

            desc1190.putEnumerated( idType, idGrdT, idLnr );

            var idAlgn = charIDToTypeID( "Algn" );

            desc1190.putBoolean( idAlgn, false );

            var idGrad = charIDToTypeID( "Grad" );

                var desc1191 = new ActionDescriptor();

                var idNm = charIDToTypeID( "Nm  " );

                desc1191.putString( idNm, """$$$/DefaultGradient/ForegroundToBackground=Foreground to Background""" );

                var idGrdF = charIDToTypeID( "GrdF" );

                var idGrdF = charIDToTypeID( "GrdF" );

                var idCstS = charIDToTypeID( "CstS" );

                desc1191.putEnumerated( idGrdF, idGrdF, idCstS );

                var idIntr = charIDToTypeID( "Intr" );

                desc1191.putDouble( idIntr, 4096.000000 );

                var idClrs = charIDToTypeID( "Clrs" );

                    var list42 = new ActionList();

                        var desc1192 = new ActionDescriptor();

                        var idClr = charIDToTypeID( "Clr " );

                            var desc1193 = new ActionDescriptor();

                            var idRd = charIDToTypeID( "Rd  " );

                            desc1193.putDouble( idRd, 163.000005 );

                            var idGrn = charIDToTypeID( "Grn " );

                            desc1193.putDouble( idGrn, 39.003892 );

                            var idBl = charIDToTypeID( "Bl  " );

                            desc1193.putDouble( idBl, 16.000001 );

                        var idRGBC = charIDToTypeID( "RGBC" );

                        desc1192.putObject( idClr, idRGBC, desc1193 );

                        var idType = charIDToTypeID( "Type" );

                        var idClry = charIDToTypeID( "Clry" );

                        var idUsrS = charIDToTypeID( "UsrS" );

                        desc1192.putEnumerated( idType, idClry, idUsrS );

                        var idLctn = charIDToTypeID( "Lctn" );

                        desc1192.putInteger( idLctn, 0 );

                        var idMdpn = charIDToTypeID( "Mdpn" );

                        desc1192.putInteger( idMdpn, 50 );

                    var idClrt = charIDToTypeID( "Clrt" );

                    list42.putObject( idClrt, desc1192 );

                        var desc1194 = new ActionDescriptor();

                        var idClr = charIDToTypeID( "Clr " );

                            var desc1195 = new ActionDescriptor();

                            var idRd = charIDToTypeID( "Rd  " );

                            desc1195.putDouble( idRd, 84.000003 );

                            var idGrn = charIDToTypeID( "Grn " );

                            desc1195.putDouble( idGrn, 103.000001 );

                            var idBl = charIDToTypeID( "Bl  " );

                            desc1195.putDouble( idBl, 46.000001 );

                        var idRGBC = charIDToTypeID( "RGBC" );

                        desc1194.putObject( idClr, idRGBC, desc1195 );

                        var idType = charIDToTypeID( "Type" );

                        var idClry = charIDToTypeID( "Clry" );

                        var idUsrS = charIDToTypeID( "UsrS" );

                        desc1194.putEnumerated( idType, idClry, idUsrS );

                        var idLctn = charIDToTypeID( "Lctn" );

                        desc1194.putInteger( idLctn, 4096 );

                        var idMdpn = charIDToTypeID( "Mdpn" );

                        desc1194.putInteger( idMdpn, 50 );

                    var idClrt = charIDToTypeID( "Clrt" );

                    list42.putObject( idClrt, desc1194 );

                desc1191.putList( idClrs, list42 );

                var idTrns = charIDToTypeID( "Trns" );

                    var list43 = new ActionList();

                        var desc1196 = new ActionDescriptor();

                        var idOpct = charIDToTypeID( "Opct" );

                        var idPrc = charIDToTypeID( "#Prc" );

                        desc1196.putUnitDouble( idOpct, idPrc, 100.000000 );

                        var idLctn = charIDToTypeID( "Lctn" );

                        desc1196.putInteger( idLctn, 0 );

                        var idMdpn = charIDToTypeID( "Mdpn" );

                        desc1196.putInteger( idMdpn, 50 );

                    var idTrnS = charIDToTypeID( "TrnS" );

                    list43.putObject( idTrnS, desc1196 );

                        var desc1197 = new ActionDescriptor();

                        var idOpct = charIDToTypeID( "Opct" );

                        var idPrc = charIDToTypeID( "#Prc" );

                        desc1197.putUnitDouble( idOpct, idPrc, 100.000000 );

                        var idLctn = charIDToTypeID( "Lctn" );

                        desc1197.putInteger( idLctn, 4096 );

                        var idMdpn = charIDToTypeID( "Mdpn" );

                        desc1197.putInteger( idMdpn, 50 );

                    var idTrnS = charIDToTypeID( "TrnS" );

                    list43.putObject( idTrnS, desc1197 );

                desc1191.putList( idTrns, list43 );

            var idGrdn = charIDToTypeID( "Grdn" );

            desc1190.putObject( idGrad, idGrdn, desc1191 );

        var idgradientLayer = stringIDToTypeID( "gradientLayer" );

        desc1189.putObject( idType, idgradientLayer, desc1190 );

    var idcontentLayer = stringIDToTypeID( "contentLayer" );

    desc1188.putObject( idUsng, idcontentLayer, desc1189 );

executeAction( idMk, desc1188, DialogModes.NO );

Any help is greatly appreciated!

Thanks,

-Eric

This topic has been closed for replies.
Correct answer pixxxelschubser

What c.pfaffenbichler​ means:

Replace the color values in your code with the values for foreground an background colors eg like this

// Make Gradient fill from Path

if (activeDocument.mode == "DocumentMode.RGB") {

var fgCol = app.foregroundColor.rgb;

var bkCol = app.backgroundColor.rgb;

(ref1 = new ActionReference()).putClass(stringIDToTypeID('contentLayer'));

(desc1 = new ActionDescriptor()).putReference(charIDToTypeID('null'), ref1);

(desc3 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Angl'), charIDToTypeID('#Ang'), 90)

desc3.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('GrdT'), charIDToTypeID('Lnr '));

(desc4 = new ActionDescriptor()).putString(charIDToTypeID('Nm  '), '''$$$/DefaultGradient/ForegroundToBackground=Foreground to Background''')

desc4.putEnumerated(charIDToTypeID('GrdF'), charIDToTypeID('GrdF'), charIDToTypeID('CstS'))

desc4.putDouble(charIDToTypeID('Intr'), 4096);

var desc6 = new ActionDescriptor();

desc6.putDouble(charIDToTypeID('Rd  '), fgCol.red);

desc6.putDouble(charIDToTypeID('Grn '), fgCol.green);

desc6.putDouble(charIDToTypeID('Bl  '), fgCol.blue);

(desc5 = new ActionDescriptor()).putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc6)

desc5.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'))

desc5.putInteger(charIDToTypeID('Lctn'), 0)

desc5.putInteger(charIDToTypeID('Mdpn'), 50);

(list1 = new ActionList()).putObject(stringIDToTypeID('colorStop'), desc5);

var desc8 = new ActionDescriptor();

desc8.putDouble(charIDToTypeID('Rd  '), bkCol.red);

desc8.putDouble(charIDToTypeID('Grn '), bkCol.green);

desc8.putDouble(charIDToTypeID('Bl  '), bkCol.blue);

(desc7 = new ActionDescriptor()).putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc8)

desc7.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'))

desc7.putInteger(charIDToTypeID('Lctn'), 4096)

desc7.putInteger(charIDToTypeID('Mdpn'), 50)

list1.putObject(charIDToTypeID('Clrt'), desc7)

desc4.putList(charIDToTypeID('Clrs'), list1);

(desc9 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100)

desc9.putInteger(charIDToTypeID('Lctn'), 0)

desc9.putInteger(charIDToTypeID('Mdpn'), 50);

(list2 = new ActionList()).putObject(charIDToTypeID('TrnS'), desc9);

(desc10 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100)

desc10.putInteger(charIDToTypeID('Lctn'), 4096)

desc10.putInteger(charIDToTypeID('Mdpn'), 50)

list2.putObject(charIDToTypeID('TrnS'), desc10)

desc4.putList(charIDToTypeID('Trns'), list2)

desc3.putObject(charIDToTypeID('Grad'), charIDToTypeID('Grdn'), desc4);

(desc2 = new ActionDescriptor()).putObject(charIDToTypeID('Type'), stringIDToTypeID('gradientLayer'), desc3)

desc1.putObject(charIDToTypeID('Usng'), stringIDToTypeID('contentLayer'), desc2)

executeAction(charIDToTypeID('Mk  '), desc1, DialogModes.NO);

}

Change the code part of your // Make Gradient fill from Path with the snippet above and

Have fun

2 replies

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
July 13, 2017

What c.pfaffenbichler​ means:

Replace the color values in your code with the values for foreground an background colors eg like this

// Make Gradient fill from Path

if (activeDocument.mode == "DocumentMode.RGB") {

var fgCol = app.foregroundColor.rgb;

var bkCol = app.backgroundColor.rgb;

(ref1 = new ActionReference()).putClass(stringIDToTypeID('contentLayer'));

(desc1 = new ActionDescriptor()).putReference(charIDToTypeID('null'), ref1);

(desc3 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Angl'), charIDToTypeID('#Ang'), 90)

desc3.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('GrdT'), charIDToTypeID('Lnr '));

(desc4 = new ActionDescriptor()).putString(charIDToTypeID('Nm  '), '''$$$/DefaultGradient/ForegroundToBackground=Foreground to Background''')

desc4.putEnumerated(charIDToTypeID('GrdF'), charIDToTypeID('GrdF'), charIDToTypeID('CstS'))

desc4.putDouble(charIDToTypeID('Intr'), 4096);

var desc6 = new ActionDescriptor();

desc6.putDouble(charIDToTypeID('Rd  '), fgCol.red);

desc6.putDouble(charIDToTypeID('Grn '), fgCol.green);

desc6.putDouble(charIDToTypeID('Bl  '), fgCol.blue);

(desc5 = new ActionDescriptor()).putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc6)

desc5.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'))

desc5.putInteger(charIDToTypeID('Lctn'), 0)

desc5.putInteger(charIDToTypeID('Mdpn'), 50);

(list1 = new ActionList()).putObject(stringIDToTypeID('colorStop'), desc5);

var desc8 = new ActionDescriptor();

desc8.putDouble(charIDToTypeID('Rd  '), bkCol.red);

desc8.putDouble(charIDToTypeID('Grn '), bkCol.green);

desc8.putDouble(charIDToTypeID('Bl  '), bkCol.blue);

(desc7 = new ActionDescriptor()).putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc8)

desc7.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'))

desc7.putInteger(charIDToTypeID('Lctn'), 4096)

desc7.putInteger(charIDToTypeID('Mdpn'), 50)

list1.putObject(charIDToTypeID('Clrt'), desc7)

desc4.putList(charIDToTypeID('Clrs'), list1);

(desc9 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100)

desc9.putInteger(charIDToTypeID('Lctn'), 0)

desc9.putInteger(charIDToTypeID('Mdpn'), 50);

(list2 = new ActionList()).putObject(charIDToTypeID('TrnS'), desc9);

(desc10 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100)

desc10.putInteger(charIDToTypeID('Lctn'), 4096)

desc10.putInteger(charIDToTypeID('Mdpn'), 50)

list2.putObject(charIDToTypeID('TrnS'), desc10)

desc4.putList(charIDToTypeID('Trns'), list2)

desc3.putObject(charIDToTypeID('Grad'), charIDToTypeID('Grdn'), desc4);

(desc2 = new ActionDescriptor()).putObject(charIDToTypeID('Type'), stringIDToTypeID('gradientLayer'), desc3)

desc1.putObject(charIDToTypeID('Usng'), stringIDToTypeID('contentLayer'), desc2)

executeAction(charIDToTypeID('Mk  '), desc1, DialogModes.NO);

}

Change the code part of your // Make Gradient fill from Path with the snippet above and

Have fun

Inspiring
July 14, 2017

???

Le July 14, 2017 à 1:40:44 AM, c.pfaffenbichler (forums_noreply@adobe.com) a écrit:

Utilize foreground/background colour in gradient fill

c.pfaffenbichler marked pixxxel schubser's reply on Utilize foreground/background colour in gradient fill as helpful. View the full reply

Marked as helpful:

What c.pfaffenbichler means:

 

Replace the color values in your code with the values for foreground an background colors eg like this

// Make Gradient fill from Path

if (activeDocument.mode == "DocumentMode.RGB") {

var fgCol = app.foregroundColor.rgb;

var bkCol = app.backgroundColor.rgb;

(ref1 = new ActionReference()).putClass(stringIDToTypeID('contentLayer'));

(desc1 = new ActionDescriptor()).putReference(charIDToTypeID('null'), ref1);

(desc3 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Angl'), charIDToTypeID('#Ang'), 90)

desc3.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('GrdT'), charIDToTypeID('Lnr '));

(desc4 = new ActionDescriptor()).putString(charIDToTypeID('Nm '), '''$$$/DefaultGradient/ForegroundToBackground=Foreground to Background''')

desc4.putEnumerated(charIDToTypeID('GrdF'), charIDToTypeID('GrdF'), charIDToTypeID('CstS'))

desc4.putDouble(charIDToTypeID('Intr'), 4096);

var desc6 = new ActionDescriptor();

desc6.putDouble(charIDToTypeID('Rd '), fgCol.red);

desc6.putDouble(charIDToTypeID('Grn '), fgCol.green);

desc6.putDouble(charIDToTypeID('Bl '), fgCol.blue);

(desc5 = new ActionDescriptor()).putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc6)

desc5.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'))

desc5.putInteger(charIDToTypeID('Lctn'), 0)

desc5.putInteger(charIDToTypeID('Mdpn'), 50);

(list1 = new ActionList()).putObject(stringIDToTypeID('colorStop'), desc5);

var desc8 = new ActionDescriptor();

desc8.putDouble(charIDToTypeID('Rd '), bkCol.red);

desc8.putDouble(charIDToTypeID('Grn '), bkCol.green);

desc8.putDouble(charIDToTypeID('Bl '), bkCol.blue);

(desc7 = new ActionDescriptor()).putObject(charIDToTypeID('Clr '), charIDToTypeID('RGBC'), desc8)

desc7.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'))

desc7.putInteger(charIDToTypeID('Lctn'), 4096)

desc7.putInteger(charIDToTypeID('Mdpn'), 50)

list1.putObject(charIDToTypeID('Clrt'), desc7)

desc4.putList(charIDToTypeID('Clrs'), list1);

(desc9 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100)

desc9.putInteger(charIDToTypeID('Lctn'), 0)

desc9.putInteger(charIDToTypeID('Mdpn'), 50);

(list2 = new ActionList()).putObject(charIDToTypeID('TrnS'), desc9);

(desc10 = new ActionDescriptor()).putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100)

desc10.putInteger(charIDToTypeID('Lctn'), 4096)

desc10.putInteger(charIDToTypeID('Mdpn'), 50)

list2.putObject(charIDToTypeID('TrnS'), desc10)

desc4.putList(charIDToTypeID('Trns'), list2)

desc3.putObject(charIDToTypeID('Grad'), charIDToTypeID('Grdn'), desc4);

(desc2 = new ActionDescriptor()).putObject(charIDToTypeID('Type'), stringIDToTypeID('gradientLayer'), desc3)

desc1.putObject(charIDToTypeID('Usng'), stringIDToTypeID('contentLayer'), desc2)

executeAction(charIDToTypeID('Mk '), desc1, DialogModes.NO);

}

 

Change the code part of your // Make Gradient fill from Path with the snippet above and

Have fun

 

Following Photoshop Scripting in these streams: Inbox

This email was sent by Adobe Community because you are a registered user.

You may unsubscribe instantly from Adobe Community, or adjust email frequency in your email preferences

c.pfaffenbichler
Community Expert
Community Expert
July 13, 2017

You can evaluate the foreground and background colors and then use the values.

alert (app.foregroundColor.rgb.red)