Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get properties value of Gradient Layer?

Participant ,
Sep 06, 2013 Sep 06, 2013

Hi all,

I want to get the properties of gradient layer i.e. its name, color value and mode?

Also the properties like angle, style and scale that appears in Gradient Fill panel, when we create the Gradient layer?

Thanks

TOPICS
Actions and scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Sep 06, 2013 Sep 06, 2013

There are a lot of settings for a gradientFill adjustment layer. I think there are always at least two color stops( even if they are the same color ). And I am not sure what you mean by 'mode'.

Here is one way to get the settings from the layer.

function getGradientFillAdjustmentInfo(){

    if(app.documents.length==0 || app.activeDocument.activeLayer.kind != LayerKind.GRADIENTFILL ) return;

    var gradientInfo = {};

    gradientInfo.toString = function(){return "GradientInfo";}

    var ref = new Acti

...
Translate
Adobe
Guru ,
Sep 06, 2013 Sep 06, 2013

There are a lot of settings for a gradientFill adjustment layer. I think there are always at least two color stops( even if they are the same color ). And I am not sure what you mean by 'mode'.

Here is one way to get the settings from the layer.

function getGradientFillAdjustmentInfo(){

    if(app.documents.length==0 || app.activeDocument.activeLayer.kind != LayerKind.GRADIENTFILL ) return;

    var gradientInfo = {};

    gradientInfo.toString = function(){return "GradientInfo";}

    var ref = new ActionReference();

    ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

    var desc = executeActionGet(ref).getList(charIDToTypeID("Adjs")).getObjectValue(0);

    if(desc.hasKey(charIDToTypeID("Dthr"))) gradientInfo.dither = desc.getBoolean(charIDToTypeID("Dthr"));

    if(desc.hasKey(charIDToTypeID("Rvrs"))) gradientInfo.reverse = desc.getBoolean(charIDToTypeID("Rvrs"));

    if(desc.hasKey(charIDToTypeID("Angl"))) gradientInfo.angle = desc.getDouble(charIDToTypeID("Angl"));

    if(desc.hasKey(charIDToTypeID("Type"))) gradientInfo.type = typeIDToStringID(desc.getEnumerationValue(charIDToTypeID("Type")));

    if(desc.hasKey(charIDToTypeID("Scl "))) gradientInfo.scale = desc.getDouble(charIDToTypeID("Scl "));

    if(desc.hasKey(charIDToTypeID("Ofst"))){

                gradientInfo.offset = [desc.getObjectValue(charIDToTypeID("Ofst")).getUnitDoubleValue(charIDToTypeID("Hrzn")),

                                                desc.getObjectValue(charIDToTypeID("Ofst")).getUnitDoubleValue(charIDToTypeID("Vrtc"))];

    }

    desc = desc.getObjectValue(charIDToTypeID("Grad"));

    gradientInfo.name = desc.getString(charIDToTypeID("Nm  "));

    gradientInfo.gradientForm = typeIDToStringID(desc.getEnumerationValue(charIDToTypeID("GrdF")));

    var colorList = desc.getList(charIDToTypeID("Clrs"));

    var transList = desc.getList(charIDToTypeID("Trns"));

    gradientInfo.numberOfColorStops = colorList.count;

    gradientInfo.colorStops = [];

    for(var colorIndex=0;colorIndex<colorList.count;colorIndex++){

        colorStopDesc = colorList.getObjectValue(colorIndex);

        var colorStop = {};

        colorStop.toString = function(){return "ColorStop";}

        colorStop.location = colorStopDesc.getInteger(charIDToTypeID("Lctn"));

        colorStop.midpoint = colorStopDesc.getInteger(charIDToTypeID("Mdpn"));

        colorStop.type = typeIDToStringID(colorStopDesc.getEnumerationValue(charIDToTypeID("Type")));

        colorStop.color = getColorFromDescriptor( colorStopDesc.getObjectValue(charIDToTypeID("Clr ")), typeIDToCharID(colorStopDesc.getClass(charIDToTypeID("Clr "))));

        gradientInfo.colorStops.push(colorStop);

    }  

    gradientInfo.numberOfTransStops = transList.count;

    gradientInfo.transStops = [];

    for(var transIndex=0;transIndex<transList.count;transIndex++){

        transStopDesc = transList.getObjectValue(transIndex);

        var transStop = {};

        transStop.toString = function(){return "TransparencyStop";}

        transStop.opacity = transStopDesc.getUnitDoubleValue(charIDToTypeID("Opct"));

        transStop.location = transStopDesc.getInteger(charIDToTypeID("Lctn"));

        transStop.midpoint = transStopDesc.getInteger(charIDToTypeID("Mdpn"));

        gradientInfo.transStops.push(transStop);

    }  

    return gradientInfo;

};

function getColorFromDescriptor( colorDesc, keyClass ){

    var colorObject = new SolidColor();

    switch( keyClass ){

        case "Grsc":

            colorObject.grey.grey = color.getDouble(charIDToTypeID('Gry '));

            break;

        case "RGBC":

            colorObject.rgb.red = colorDesc.getDouble(charIDToTypeID('Rd  '));

            colorObject.rgb.green = colorDesc.getDouble(charIDToTypeID('Grn '));

            colorObject.rgb.blue = colorDesc.getDouble(charIDToTypeID('Bl  '));

            break;

        case "CMYC":

            colorObject.cmyk.cyan = colorDesc.getDouble(charIDToTypeID('Cyn '));

            colorObject.cmyk.magenta = colorDesc.getDouble(charIDToTypeID('Mgnt'));

            colorObject.cmyk.yellow = colorDesc.getDouble(charIDToTypeID('Ylw '));

            colorObject.cmyk.black = colorDesc.getDouble(charIDToTypeID('Blck'));

            break;

        case "LbCl":

            colorObject.lab.l = colorDesc.getDouble(charIDToTypeID('Lmnc'));

            colorObject.lab.a = colorDesc.getDouble(charIDToTypeID('A   '));

            colorObject.lab.b = colorDesc.getDouble(charIDToTypeID('B   '));

        case "HSBC":

            colorObject.hsb.hue = colorDesc.getDouble(charIDToTypeID('H   '));

            colorObject.hsb.saturation = colorDesc.getDouble(charIDToTypeID('Strt'));

            colorObject.hsb.brightness = colorDesc.getDouble(charIDToTypeID('Brgh'));

            break;

        default: return null;

    }

    return colorObject;

};

function alertProperties( obj ){

    var str = obj +" properties:\r\r";

    for (var p in obj) {

        if(typeof obj

== "function") continue;

        str += p + ': ' + obj

+"\r";

    }

    alert(str);

};

var someObjectName = getGradientFillAdjustmentInfo();

alertProperties( someObjectName );

alertProperties( someObjectName.colorStops[0] );

alertProperties( someObjectName.transStops[0] );

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 06, 2013 Sep 06, 2013
LATEST

Thanks Michael, This is the same thing I am looking for.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines