Skip to main content
Known Participant
October 25, 2018
Answered

Building RGB values from the layer name?

  • October 25, 2018
  • 3 replies
  • 2090 views

Hello all,

I've been working on this script and I am stuck. I'm not getting an error, but I don't get the results I'm hoping for.

The objective:

Name the active layer with an RGB value, tell Photoshop to replace the active layer with a new solid fill layer, built with the active layer RGB numbers. Any help is greatly appreciated. ~ Joe D

This is my script at this point:

#target photoshop

//sets global variables for active doc and layer

var doc = app.activeDocument;

var lyr = doc.activeLayer;

var lyrName = lyr.name;

// Check the layer name for eleven chracters or less

if (lyrName.length <= 11) {

  var lyrColor = new RGBColor()

  lyrColor.RGB = lyrName;

  pasteLayernameToRGBField(lyrColor);

} else {

  alert("Name the layer with RGB mix (xxx,xxx,xxx)- include commas");

}

function pasteLayernameToRGBField(color) {

  var desc1 = new ActionDescriptor();

  var ref1 = new ActionReference();

  ref1.putEnumerated( stringIDToTypeID( "contentLayer" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

  desc1.putReference( charIDToTypeID( "null" ), ref1 );

  var desc2 = new ActionDescriptor();

  var desc3 = new ActionDescriptor();

  desc3.putString( charIDToTypeID( "Rd  " ), color.red );

  desc3.putString( charIDToTypeID( "Grn " ), color.green );

  desc3.putString( charIDToTypeID( "Bl  " ), color.blue );

  desc2.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), desc3 );

  desc1.putObject( charIDToTypeID( "T   " ), stringIDToTypeID( "solidColorLayer" ), desc2 );

  executeAction( charIDToTypeID( "setd" ), desc1, DialogModes.NO );

}

This topic has been closed for replies.
Correct answer Chuck Uebele

Made a few changes:

#target photoshop 

     

    //sets global variables for active doc and layer 

    var doc = app.activeDocument; 

    var lyr = doc.activeLayer; 

    var lyrName = lyr.name; 

     

    // Check the layer name for eleven chracters or less 

    if (lyrName.length <= 11) { 

      //var lyrColor = new RGBColor() 

      var lyrColor = new SolidColor() 

      //lyrColor.RGB = lyrName; 

      lyrColor.rgb.red = lyrName.split(',')[0]; 

      lyrColor.rgb.green = lyrName.split(',')[1]; 

      lyrColor.rgb.blue = lyrName.split(',')[2]; 

     

      pasteLayernameToRGBField(lyrColor); 

    } else { 

      alert("Name the layer with RGB mix (xxx,xxx,xxx)- include commas"); 

    } 

     

    function pasteLayernameToRGBField(color) { 

        var idMk = charIDToTypeID( "Mk  " );

            var desc12 = new ActionDescriptor();

            var idnull = charIDToTypeID( "null" );

                var ref2 = new ActionReference();

                var idcontentLayer = stringIDToTypeID( "contentLayer" );

                ref2.putClass( idcontentLayer );

            desc12.putReference( idnull, ref2 );

            var idUsng = charIDToTypeID( "Usng" );

                var desc13 = new ActionDescriptor();

                var idType = charIDToTypeID( "Type" );

                    var desc14 = new ActionDescriptor();

                    var idClr = charIDToTypeID( "Clr " );

                        var desc15 = new ActionDescriptor();

                        var idRd = charIDToTypeID( "Rd  " );

                        desc15.putDouble( idRd, color.rgb.red );

                        var idGrn = charIDToTypeID( "Grn " );

                        desc15.putDouble( idGrn, color.rgb.green );

                        var idBl = charIDToTypeID( "Bl  " );

                        desc15.putDouble( idBl, color.rgb.blue );

                    var idRGBC = charIDToTypeID( "RGBC" );

                    desc14.putObject( idClr, idRGBC, desc15 );

                var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );

                desc13.putObject( idType, idsolidColorLayer, desc14 );

            var idcontentLayer = stringIDToTypeID( "contentLayer" );

            desc12.putObject( idUsng, idcontentLayer, desc13 );

        executeAction( idMk, desc12, DialogModes.NO );

    } 

3 replies

Chuck Uebele
Community Expert
Community Expert
October 29, 2018

Oh, if that layer is already a solid fill layer, try this:

#target photoshop

var doc = activeDocument;

var lay = doc.activeLayer;

var layNum = new Array()

var run = true;

checkNum (lay.name.split(','))

if(run){

    changeColor ()

    }

function checkNum(numbs){

        for (var i=0;i<numbs.length;i++){

        try{layNum = Number(numbs)

            if(layNum>255||layNum<0|| isNaN ( layNum)){throw(e)}

            }

        catch(e){

            if(i==0){alert(numbs + ' is not a valid red value number')}

            if(i==1){alert(numbs + ' is not a valid green value number')}

            if(i==2){alert(numbs + ' is not a valid blue value number')}

            run = false;

            }

        }

    }

function changeColor(){

    var idsetd = charIDToTypeID( "setd" );

        var desc5 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref2 = new ActionReference();

            var idcontentLayer = stringIDToTypeID( "contentLayer" );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref2.putEnumerated( idcontentLayer, idOrdn, idTrgt );

        desc5.putReference( idnull, ref2 );

        var idT = charIDToTypeID( "T   " );

            var desc6 = new ActionDescriptor();

            var idClr = charIDToTypeID( "Clr " );

                var desc7 = new ActionDescriptor();

                var idRd = charIDToTypeID( "Rd  " );

                desc7.putDouble( idRd, layNum[0] );

                var idGrn = charIDToTypeID( "Grn " );

                desc7.putDouble( idGrn, layNum[1] );

                var idBl = charIDToTypeID( "Bl  " );

                desc7.putDouble( idBl, layNum[2] );

            var idRGBC = charIDToTypeID( "RGBC" );

            desc6.putObject( idClr, idRGBC, desc7 );

        var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );

        desc5.putObject( idT, idsolidColorLayer, desc6 );

    executeAction( idsetd, desc5, DialogModes.NO );

    }

thejoed1Author
Known Participant
October 29, 2018

Chuck! That totally works. Super clean. Thank you so much.

~ Joe D

Chuck Uebele
Community Expert
Community Expert
October 25, 2018

In my updated script, I converted the layer name to a solid color, but you can just split the layer name and use the values in the function and get the same result.

thejoed1Author
Known Participant
October 25, 2018

Thanks for being there Chuck, that is the nugget I need.

Cheers!

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
October 25, 2018

Made a few changes:

#target photoshop 

     

    //sets global variables for active doc and layer 

    var doc = app.activeDocument; 

    var lyr = doc.activeLayer; 

    var lyrName = lyr.name; 

     

    // Check the layer name for eleven chracters or less 

    if (lyrName.length <= 11) { 

      //var lyrColor = new RGBColor() 

      var lyrColor = new SolidColor() 

      //lyrColor.RGB = lyrName; 

      lyrColor.rgb.red = lyrName.split(',')[0]; 

      lyrColor.rgb.green = lyrName.split(',')[1]; 

      lyrColor.rgb.blue = lyrName.split(',')[2]; 

     

      pasteLayernameToRGBField(lyrColor); 

    } else { 

      alert("Name the layer with RGB mix (xxx,xxx,xxx)- include commas"); 

    } 

     

    function pasteLayernameToRGBField(color) { 

        var idMk = charIDToTypeID( "Mk  " );

            var desc12 = new ActionDescriptor();

            var idnull = charIDToTypeID( "null" );

                var ref2 = new ActionReference();

                var idcontentLayer = stringIDToTypeID( "contentLayer" );

                ref2.putClass( idcontentLayer );

            desc12.putReference( idnull, ref2 );

            var idUsng = charIDToTypeID( "Usng" );

                var desc13 = new ActionDescriptor();

                var idType = charIDToTypeID( "Type" );

                    var desc14 = new ActionDescriptor();

                    var idClr = charIDToTypeID( "Clr " );

                        var desc15 = new ActionDescriptor();

                        var idRd = charIDToTypeID( "Rd  " );

                        desc15.putDouble( idRd, color.rgb.red );

                        var idGrn = charIDToTypeID( "Grn " );

                        desc15.putDouble( idGrn, color.rgb.green );

                        var idBl = charIDToTypeID( "Bl  " );

                        desc15.putDouble( idBl, color.rgb.blue );

                    var idRGBC = charIDToTypeID( "RGBC" );

                    desc14.putObject( idClr, idRGBC, desc15 );

                var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );

                desc13.putObject( idType, idsolidColorLayer, desc14 );

            var idcontentLayer = stringIDToTypeID( "contentLayer" );

            desc12.putObject( idUsng, idcontentLayer, desc13 );

        executeAction( idMk, desc12, DialogModes.NO );

    } 

pixxxelschubser
Community Expert
Community Expert
October 26, 2018

For a bit more security and flexibility

// Check the layer name for eleven chracters or less   

var val = lyrName.replace(" ","").split(",");

if (val[0].match(/^\d{3}$/) != null && val[1].match(/^\d{3}$/) != null && val[2].match(/^\d{3}$/) != null) {

  var lyrColor = new SolidColor();

  lyrColor.rgb.red = val[0];   

  lyrColor.rgb.green = val[1];   

  lyrColor.rgb.blue = val[2];   

  pasteLayernameToRGBField(lyrColor);   

} else {   

  alert("Name the layer with RGB mix (xxx,xxx,xxx)- include commas");   

}   

Have fun

thejoed1Author
Known Participant
October 26, 2018

pixel schubser - thanks for your version.

As I have been working with both the solutions I received, I realize that I was not clear with my initial post. Have a look at the code I posted below. This is my working version for HEX values, It is what I am hoping to accomplish with RGB values. What do you guys think?

Thanks for all your help.

/* HEX_layernameMakesColor.js makes the solid fill layer the hex color indicated in the layer name.*/

#target photoshop

//sets global variables for active doc and layer

var doc = app.activeDocument;

var lyr = doc.activeLayer;

var lyrName = lyr.name;

// Check the layer name for six characters and a # symbol at the front

if (lyrName.length == 6) {

  var lyrcolor = new RGBColor()

  lyrcolor.hexValue = lyrName;

  pasteLayernameToHexField(lyrcolor);

} else {

  alert("Name the layer with six characters, no # sign.");

}

function pasteLayernameToHexField(color) {

  var desc1 = new ActionDescriptor();

  var ref1 = new ActionReference();

  ref1.putEnumerated( stringIDToTypeID( "contentLayer" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

  desc1.putReference( charIDToTypeID( "null" ), ref1 );

  var desc2 = new ActionDescriptor();

  var desc3 = new ActionDescriptor();

  desc3.putDouble( charIDToTypeID( "Rd  " ), color.red );

  desc3.putDouble( charIDToTypeID( "Grn " ), color.green );

  desc3.putDouble( charIDToTypeID( "Bl  " ), color.blue );

  desc2.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), desc3 );

  desc1.putObject( charIDToTypeID( "T  " ), stringIDToTypeID( "solidColorLayer" ), desc2 );

  executeAction( charIDToTypeID( "setd" ), desc1, DialogModes.NO );

}