Skip to main content
Participant
January 27, 2015
Question

Fill color of shapes in layer

  • January 27, 2015
  • 1 reply
  • 1230 views

How can i fill the color (solid) of shapes within an layer? For now I got:

function getLayer(name)

{

    return app.activeDocument.layers.getByName(name);

}

function fillSelection(hex)

{

    var layer = getLayer('Background');

    app.activeDocument.activeLayer = layer;

  

  app.activeDocument.selection.selectAll();

    var selection = app.activeDocument.selection;

  var sColor =  new SolidColor;

  sColor.rgb.hexValue = hex;

  app.activeDocument.selection.fill(sColor);

  app.activeDocument.selection.deselect();

}

But this is not working if i create a rectangle in the "Background"-Layer. (even not if its converted to smart object or rasterized...).

Thank you!

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
January 27, 2015

A rectangle in the background layer OK:

// A Script by JJMack's

// enable double-clicking from Mac Finder or Windows Explorer

#target photoshop // this command only works in Photoshop CS2 and higher

// bring application forward for double-click events

app.bringToFront();

// ensure at least one document open

if (!documents.length) {

  alert('There are no documents open.', 'No Document');

}

// if at least one document exists, then proceed

else {

  main();

}

///////////////////////////////////////////////////////////////////////////////

// main - main function

///////////////////////////////////////////////////////////////////////////////

function main() {

  try {

  var startRulerUnits = app.preferences.rulerUnits;

  app.preferences.rulerUnits = Units.PIXELS; // tell ps to work with pixels

  var Width = activeDocument.width.value;

  var Height = activeDocument.height.value;

  // declare local variables

  var layers = activeDocument.layers;

  activeDocument.activeLayer = layers[layers.length-1] // Target Bottom Layer

  if ( !activeDocument.activeLayer.isBackgroundLayer ) {

  app.activeDocument.artLayers.add()

  // declare local variables

  var newlayers = app.activeDocument.layers;

  // arrange layers so new layer added is at bottom

  newlayers[0].move(newlayers[newlayers.length-1], ElementPlacement.PLACEAFTER);

  // Make it a Background Layer

  activeDocument.activeLayer.isBackgroundLayer=1

  }

  fillColor = new SolidColor;

  fillColor.rgb.red = Math.random() * 255

  fillColor.rgb.green = Math.random() * 255

  fillColor.rgb.blue = Math.random() * 255

  app.activeDocument.selection.selectAll();

  app.activeDocument.selection.fill(fillColor);

  app.activeDocument.selection.deselect();

  fillColor.rgb.red = Math.random() * 255

  fillColor.rgb.green = Math.random() * 255

  fillColor.rgb.blue = Math.random() * 255

  var selectedRegion = Array(Array(.1 * Width, .1 * Height), Array(.9 * Width, .1 * Height), Array(.9 * Width, .9 * Height), Array(.1 * Width, .9 * Height));

  app.activeDocument.selection.select(selectedRegion);

  app.activeDocument.selection.fill(fillColor);

  app.activeDocument.selection.deselect();

  app.preferences.rulerUnits = startRulerUnits;

  }

  // display error message if something goes wrong

  catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

}

JJMack
Participant
January 27, 2015

Many Thanks for your detailed and commented code snipppet. But I should have been more precise in describing:

Its not only a rectangle. It could be a circle, combined with a rectangle or whatever shape you're thinking of. (and further the "Background" is bit of misleading. It could be any layer).

So, 2 simple questions:

1. Is there a way to simply select all content within a layer (so that is the shape of an object and not the whole layer)? For example converting it to a smart object or something like that and then calling a function which selects the shape of that object?

2. Would'nt it be more easy to just apply a ColorOverlay which would do the same things with one call?

JJMack
Community Expert
Community Expert
January 27, 2015

app.activeDocument.selection.fill(fillColor); would fill any shape selection on the active layer if the layer is a normal layer a special layer type. Like you can not alter pixels in a smart object layer, Color can not be used in channels and mask etc.  A layer that has a shape can be a shape layer which is a fill layer either solid or pattern and if layer that has a shape is not shape layer it has transparency. You can load a layers transparency as a selection and fill that.  Transparency can have feathering


A layer style can be added to most layer types like smart object layers, text layers I don't know about adjustment layers I don't know if a layer style is possible on them lets try... Yes you can but a color overlay style may do nothing like on a level adjustment layer

JJMack