Skip to main content
Known Participant
January 2, 2020
Answered

Get current solid layer color hex code in alert

  • January 2, 2020
  • 2 replies
  • 2000 views

i need to Get current solid layer(shape) color hex code in alert

This topic has been closed for replies.
Correct answer greless

function GetStrokeRGB()
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("AGMStrokeStyleInfo"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var objAGMStrokeStyleInfo=executeActionGet(r).getObjectValue(stringIDToTypeID("AGMStrokeStyleInfo"));

if(!objAGMStrokeStyleInfo.getBoolean(stringIDToTypeID("strokeEnabled")))
return;

var objsolidColorLayer =objAGMStrokeStyleInfo.getObjectValue(stringIDToTypeID("strokeStyleContent"));
var objRGBColor =objsolidColorLayer.getObjectValue(stringIDToTypeID("color")) ;
alert("Stroke\nred:"+objRGBColor.getDouble(stringIDToTypeID("red"))+"\ngrain:"+objRGBColor.getDouble(stringIDToTypeID("grain"))+"\nblue:"+objRGBColor.getDouble(stringIDToTypeID("blue")));
}

function GetFillRGB()
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("AGMStrokeStyleInfo"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var objAGMStrokeStyleInfo=executeActionGet(r).getObjectValue(stringIDToTypeID("AGMStrokeStyleInfo"));

if(!objAGMStrokeStyleInfo.getBoolean(stringIDToTypeID("fillEnabled")))
return;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("adjustment"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var objAdjustment=executeActionGet(r).getList(stringIDToTypeID("adjustment"));

var objsolidColorLayer =objAdjustment.getObjectValue(0);

var objRGBColor =objsolidColorLayer.getObjectValue(stringIDToTypeID("color")) ;
alert("Fill\nred:"+objRGBColor.getDouble(stringIDToTypeID("red"))+"\ngrain:"+objRGBColor.getDouble(stringIDToTypeID("grain"))+"\nblue:"+objRGBColor.getDouble(stringIDToTypeID("blue")));
}

GetFillRGB();
GetStrokeRGB();

2 replies

Chuck Uebele
Community Expert
Community Expert
January 3, 2020

To add to 龚亮g64532194 solution, you can get the hex value by adding this code instead of the alert for the RBG fill value:

 

 

var shapeColor = new SolidColor()
shapeColor.rgb.red = objRGBColor.getDouble(stringIDToTypeID("red"));
shapeColor.rgb.green = objRGBColor.getDouble(stringIDToTypeID("grain"));
shapeColor.rgb.blue = objRGBColor.getDouble(stringIDToTypeID("blue"));
alert('shape fill color = ' + shapeColor.rgb.hexValue);

 

 

rob day
Community Expert
Community Expert
January 2, 2020

I think you will have to make the solid color layer visible, add a color sampler, and get its color. This samples the pixel at 100x, 100y

 

 

 

var sc=app.activeDocument.colorSamplers.add([100,100]);
alert("Color HEX: #"+sc.color.rgb.hexValue);
sc.remove()

 

 

 

JJMack
Community Expert
Community Expert
January 2, 2020

You can test if the Active Layer is a solid fill using LayerKind

However Shapes layers  can have a Gradient fill, Pattern fill, Solid Color fill, and no color fill.  With Action manager code you may be able to find out if the Active layer has any of these fills.  I do not see a LayerKind SHAPE.  R-bin may be able to help you with the Action Manager code need the get the information about the Active Layer.

JJMack