Skip to main content
Known Participant
March 9, 2024
Answered

Color Point To layer fill color ( Photoshop Scripts )

  • March 9, 2024
  • 1 reply
  • 664 views

There will be a color point in one layer and I want to fill the entire layer with that color. . . Can there be scripts for that... Please help me, Sirs

This topic has been closed for replies.
Correct answer Stephen Marsh

Yes . always on a white background . . . Thank you so much Sir @Stephen Marsh 

 


@Thiha31203570tbl0 

 

Try this code:

 

/*
Fill Layer From Random Blob Colour.jsx
v1.0 - 10th March 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/color-point-to-layer-fill-color-photoshop-scripts/td-p/14477613
*/

#target photoshop

app.activeDocument.suspendHistory("Undo script...", "main()");

function main() {

  // Set the rulers to px
  var savedRuler = app.preferences.rulerUnits;
  app.preferences.rulerUnits = Units.PIXELS;

  // Set color range selection to inverted highlights
  var idcolorRange = stringIDToTypeID("colorRange");
  var desc965 = new ActionDescriptor();
  var idcolors = stringIDToTypeID("colors");
  var idcolors = stringIDToTypeID("colors");
  var idhighlights = stringIDToTypeID("highlights");
  desc965.putEnumerated(idcolors, idcolors, idhighlights);
  var idhighlightsFuzziness = stringIDToTypeID("highlightsFuzziness");
  desc965.putInteger(idhighlightsFuzziness, 0);
  var idhighlightsLowerLimit = stringIDToTypeID("highlightsLowerLimit");
  desc965.putInteger(idhighlightsLowerLimit, 255);
  var idinvert = stringIDToTypeID("invert");
  desc965.putBoolean(idinvert, true);
  var idcolorModel = stringIDToTypeID("colorModel");
  desc965.putInteger(idcolorModel, 0);
  executeAction(idcolorRange, desc965, DialogModes.NO);
  app.activeDocument.selection.contract(2);

  // Float selection to a new layer
  var idcopyToLayer = stringIDToTypeID("copyToLayer");
  executeAction(idcopyToLayer, undefined, DialogModes.NO);
  removeWhite();
  executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
  app.activeDocument.activeLayer.rasterize(RasterizeType.ENTIRELAYER);
  app.activeDocument.activeLayer.applyMinimum(50);

  // Layer bounds
  var layerBounds = app.activeDocument.activeLayer.bounds;
  var hor = layerBounds[2] - layerBounds[0];
  var ver = layerBounds[3] - layerBounds[1];
  var hCentre = hor / 2 + layerBounds[0];
  var vCentre = ver / 2 + layerBounds[1];

  // Main block to do stuff
  if (app.activeDocument.colorSamplers.length == 10) {

    var sampler10x = app.activeDocument.colorSamplers[9].position[0].value;
    var sampler10y = app.activeDocument.colorSamplers[9].position[1].value;
    app.activeDocument.colorSamplers[9].remove();

    fillFromSampler();
    mergeDown();

    app.activeDocument.colorSamplers.add([sampler10x, sampler10y]);

  } else {

    fillFromSampler();
    mergeDown();

  }

  // Reset the original ruler units
  app.preferences.rulerUnits = savedRuler;


  // Functions 
  function mergeDown() {
    var idmergeLayersNew = stringIDToTypeID("mergeLayersNew");
    var desc1092 = new ActionDescriptor();
    executeAction(idmergeLayersNew, desc1092, DialogModes.NO);
  }

  function fillFromSampler() {
    var theColorSampler = app.activeDocument.colorSamplers.add([hCentre, vCentre]);
    var s2t = function (s) {
      return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor.putEnumerated(s2t("using"), s2t("fillContents"), s2t("color"));
    descriptor2.putDouble(s2t("red"), Math.round(theColorSampler.color.rgb.red));
    descriptor2.putDouble(s2t("grain"), Math.round(theColorSampler.color.rgb.green));
    descriptor2.putDouble(s2t("blue"), Math.round(theColorSampler.color.rgb.blue));
    descriptor.putObject(s2t("color"), s2t("RGBColor"), descriptor2);
    descriptor.putUnitDouble(s2t("opacity"), s2t("percentUnit"), 100.000000);
    descriptor.putEnumerated(s2t("mode"), s2t("blendMode"), s2t("normal"));
    executeAction(s2t("fill"), descriptor, DialogModes.NO);
    theColorSampler.remove();
  }

  function removeWhite() {
    var s2t = function (s) {
      return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    var descriptor3 = new ActionDescriptor();
    var descriptor4 = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    var reference2 = new ActionReference();
    reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("null"), reference);
    reference2.putEnumerated(s2t("channel"), s2t("channel"), s2t("gray"));
    descriptor3.putReference(s2t("channel"), reference2);
    descriptor3.putInteger(s2t("srcBlackMin"), 0);
    descriptor3.putInteger(s2t("srcBlackMax"), 0);
    descriptor3.putInteger(s2t("srcWhiteMin"), 254); // Remove white
    descriptor3.putInteger(s2t("srcWhiteMax"), 254); // Remove white
    descriptor3.putInteger(s2t("destBlackMin"), 0);
    descriptor3.putInteger(s2t("destBlackMax"), 0);
    descriptor3.putInteger(s2t("destWhiteMin"), 255);
    descriptor3.putInteger(s2t("desaturate"), 255);
    list.putObject(s2t("blendRange"), descriptor3);
    descriptor2.putList(s2t("blendRange"), list);
    descriptor4.putUnitDouble(s2t("scale"), s2t("percentUnit"), 100.000000);
    descriptor2.putObject(s2t("layerEffects"), s2t("layerEffects"), descriptor4);
    descriptor.putObject(s2t("to"), s2t("layer"), descriptor2);
    executeAction(s2t("set"), descriptor, DialogModes.NO);
  }
}

 

1 reply

Stephen Marsh
Community Expert
Community Expert
March 9, 2024

@Thiha31203570tbl0 

 

Questions:

 

* Will the colour blob always be in the same general position and size?

 

* Do you wish to fill a standard artLayer with a raster fill, or do you wish to replace the existing layer with a vector solid fill layer?

 

EDIT: Here is an example where the colour blob has coloured pixels in the centre of the document and the fill is a simple raster fill replacing the pixel content in the current layer.

 

/*
Fill From Sampler.jsx
v1.0 - 10th March 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/color-point-to-layer-fill-color-photoshop-scripts/td-p/14477613
*/

#target photoshop

var savedRuler = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var docHcenter = app.activeDocument.width.value / 2;
var docVcenter = app.activeDocument.height.value / 2;

if (app.activeDocument.colorSamplers.length == 10) {
    var sampler10x = app.activeDocument.colorSamplers[9].position[0].value;
    var sampler10y = app.activeDocument.colorSamplers[9].position[1].value;
    app.activeDocument.colorSamplers[9].remove();
    fillFromSampler();
    app.activeDocument.colorSamplers.add([sampler10x, sampler10y]);
} else {
    fillFromSampler();
}

app.preferences.rulerUnits = savedRuler;


function fillFromSampler() {
         var theColorSampler = app.activeDocument.colorSamplers.add([docHcenter, docVcenter]);
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var descriptor2 = new ActionDescriptor();
    descriptor.putEnumerated(s2t("using"), s2t("fillContents"), s2t("color"));
    descriptor2.putDouble(s2t("red"), Math.round(theColorSampler.color.rgb.red));
    descriptor2.putDouble(s2t("grain"), Math.round(theColorSampler.color.rgb.green));
    descriptor2.putDouble(s2t("blue"), Math.round(theColorSampler.color.rgb.blue));
    descriptor.putObject(s2t("color"), s2t("RGBColor"), descriptor2);
    descriptor.putUnitDouble(s2t("opacity"), s2t("percentUnit"), 100.000000);
    descriptor.putEnumerated(s2t("mode"), s2t("blendMode"), s2t("normal"));
    executeAction(s2t("fill"), descriptor, DialogModes.NO);
    theColorSampler.remove();
}

 

Known Participant
March 9, 2024

Thank You Sir @Stephen Marsh  

* That colour blob always be in the randomly position and size.

*i need wish to fill a standard artLayer with a raster fill . 

Stephen Marsh
Community Expert
Community Expert
March 9, 2024
quote

Thank You Sir @Stephen Marsh  

* That colour blob always be in the randomly position and size.

*i need wish to fill a standard artLayer with a raster fill . 


By @Thiha31203570tbl0


Random position and size... OK, I'll work with that in mind then!

 

Will it always be on a white background?