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

Add inverse color stroke script size problems

Explorer ,
Dec 03, 2023 Dec 03, 2023

Copy link to clipboard

Copied

Hey, It's me again, thanks for helping me out last time, now i got a new question.
I got this script from a friend of mine, it originally simply would invert the text layer stroke color, but i added a function that adds the stroke itself based on the text layer size (In pixels), however it works about right on new text layers, but when I try to run it in the already existing layers, it will mess up the size, at first I thought it was something about the way it measures size, but now I honestly don't know what might be the problem.

Iuigidesu_1-1701655384949.png

I had also in mind adding a function that makes it work only with one specific font, but I think the main problem is the stroke size, I really hope someone can help me out, thanks beforehand.

Here's the code in question:

// Add a stroke on a text layer
// Stroke will be the inverse of the text layer's color

function getStrokeSize(){
    try{
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
        var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).getObjectValue(stringIDToTypeID('frameFX'));
        return desc.getUnitDoubleValue(stringIDToTypeID('size'));
    }catch(e){}
};


var textRed = app.activeDocument.activeLayer.textItem.color.rgb.red
var textBlue = app.activeDocument.activeLayer.textItem.color.rgb.blue
var textGreen = app.activeDocument.activeLayer.textItem.color.rgb.green
var textRed = Math.abs(textRed - 255)
var textBlue = Math.abs(textBlue - 255)
var textGreen = Math.abs(textGreen - 255)


function newStroke(size){
function newStrokeEffect(strokeSize, strokeColor, strokePosition) {
    var effectDescriptor = new ActionDescriptor();
    var effectColor = new ActionDescriptor();
    var strokeOpacity = 100.0;      // 0 - 100 %
    var strokeBlend = "Nrml";       // Normal[Nrml], ColorBurn[CBrn], SoftLight[SftL}, Color[Clr ]

    effectDescriptor.putBoolean(charIDToTypeID("enab"), true);
    effectDescriptor.putEnumerated(charIDToTypeID("Styl"), charIDToTypeID("FStl"), charIDToTypeID(strokePosition));
    effectDescriptor.putEnumerated(charIDToTypeID("PntT"), charIDToTypeID("FrFl"), charIDToTypeID("SClr"));
    effectDescriptor.putEnumerated(charIDToTypeID("Md  "), charIDToTypeID("BlnM"), charIDToTypeID(strokeBlend));
    effectDescriptor.putUnitDouble(charIDToTypeID("Opct"), charIDToTypeID("#Prc"), strokeOpacity);
    effectDescriptor.putUnitDouble(charIDToTypeID("Sz  "), charIDToTypeID("#Pxl"), strokeSize);
    effectColor.putDouble(charIDToTypeID("Rd  "), strokeColor.rgb.red);
    effectColor.putDouble(charIDToTypeID("Grn "), strokeColor.rgb.green);
    effectColor.putDouble(charIDToTypeID("Bl  "), strokeColor.rgb.blue);
    effectDescriptor.putObject(charIDToTypeID("Clr "), charIDToTypeID("RGBC"), effectColor);
    return(effectDescriptor);
}

var tmpC = new SolidColor();
tmpC.rgb.red = textRed
tmpC.rgb.blue = textBlue
tmpC.rgb.green = textGreen

var layerOptions = new ActionDescriptor();
var refr01 = new ActionReference();
var layerProperties = new ActionDescriptor();

layerOptions.putUnitDouble(charIDToTypeID("Scl "), charIDToTypeID("#Prc"), 100.0);

var layerEffects = newStrokeEffect(size, tmpC, "OutF");

layerOptions.putObject(charIDToTypeID("FrFX"), charIDToTypeID("FrFX"), layerEffects);

refr01.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("Lefx"));
refr01.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
layerProperties.putReference(charIDToTypeID("null"), refr01);
layerProperties.putObject(charIDToTypeID("T   "), charIDToTypeID("Lefx"), layerOptions);

try {
    executeAction(charIDToTypeID("setd"), layerProperties, DialogModes.NO);
} catch(ex) {
    if (ex != "Error: User cancelled the operation")
        alert(scriptName + " newLayerEffect() exception caught? line[" + ex.line + "] "  + ex);
}

}

var textLayer = app.activeDocument.activeLayer; // Get the active text layer
var textSize = textLayer.textItem.size;  // Get the height of the text layer

var strokeSize = textSize * 0.15; //Calculates a 15% of the original font size to be used as stroke, you can change it to whatever % you want.

strokeStatus = getStrokeSize()

if (typeof strokeStatus !== "number") {
    newStroke(strokeSize); //Uses the previously calculted 15% for the new stroke
} else {
newStroke(getStrokeSize()) //If the layer already has a stroke it will do the color inverse function, but if the stroke color is already the opposite of the text color, it wont do anything
}

 

TOPICS
Actions and scripting , Windows

Views

341
Translate

Report

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
Adobe
Community Expert ,
Dec 03, 2023 Dec 03, 2023

Copy link to clipboard

Copied

Via the Line 

newStroke(getStrokeSize())

you seem to use the existing Line width, not 0.15xFontsize

Votes

Translate

Report

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
Explorer ,
Dec 04, 2023 Dec 04, 2023

Copy link to clipboard

Copied

True, I changed it, this is the new line:

newStroke(strokeSize)

But now i have a new problem, small images will work normally, while big images will mess up the size. Heres the problem:

Iuigidesu_0-1701725267287.png

I tested it with a

textSize * 0.10

 To prevent errors. [The left image is 8598x5470 (120ppi) and the one in the right is 1200x836 (72ppi)]
The left image has a 100px text and it gives me a 2px stroke, while the right one has a 30px text and gives me a 3px stroke, so I don't really know what might be the problem now.

Votes

Translate

Report

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
Community Expert ,
Dec 04, 2023 Dec 04, 2023

Copy link to clipboard

Copied

If an image’s resolution makes a difference you can either amend the Script to check the resolution (and proceed accordingly) or you can change the resolution (without »Resample« naturally). 

Votes

Translate

Report

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
Explorer ,
Dec 05, 2023 Dec 05, 2023

Copy link to clipboard

Copied

LATEST

Yeah, I figured it'll be easier to just use the document size, since im working on comics, they tend to use a specific constant stroke size based on the original size so i pretty much did the same, it now calculates the size based on the canvas height. So it now works for narration and sound effects strokes, thank you very much for the help.
Heres the full functional code, I added back a function that makes the stroke 1px bigger in case theres a existant stroke, now I'm trying to add a function for it to work only on a specific font.

// Add a stroke on a text layer
// Stroke will be the inverse of the text layer's color

function getStrokeSize(){
    try{
        var ref = new ActionReference();
        ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
        var desc = executeActionGet(ref).getObjectValue(stringIDToTypeID('layerEffects')).getObjectValue(stringIDToTypeID('frameFX'));
        return desc.getUnitDoubleValue(stringIDToTypeID('size'));
    }catch(e){}
};


var textRed = app.activeDocument.activeLayer.textItem.color.rgb.red
var textBlue = app.activeDocument.activeLayer.textItem.color.rgb.blue
var textGreen = app.activeDocument.activeLayer.textItem.color.rgb.green
var textRed = Math.abs(textRed - 255)
var textBlue = Math.abs(textBlue - 255)
var textGreen = Math.abs(textGreen - 255)


function newStroke(size){
function newStrokeEffect(strokeSize, strokeColor, strokePosition) {
    var effectDescriptor = new ActionDescriptor();
    var effectColor = new ActionDescriptor();
    var strokeOpacity = 100.0;      // 0 - 100 %
    var strokeBlend = "Nrml";       // Normal[Nrml], ColorBurn[CBrn], SoftLight[SftL}, Color[Clr ]

    effectDescriptor.putBoolean(charIDToTypeID("enab"), true);
    effectDescriptor.putEnumerated(charIDToTypeID("Styl"), charIDToTypeID("FStl"), charIDToTypeID(strokePosition));
    effectDescriptor.putEnumerated(charIDToTypeID("PntT"), charIDToTypeID("FrFl"), charIDToTypeID("SClr"));
    effectDescriptor.putEnumerated(charIDToTypeID("Md  "), charIDToTypeID("BlnM"), charIDToTypeID(strokeBlend));
    effectDescriptor.putUnitDouble(charIDToTypeID("Opct"), charIDToTypeID("#Prc"), strokeOpacity);
    effectDescriptor.putUnitDouble(charIDToTypeID("Sz  "), charIDToTypeID("#Pxl"), strokeSize);
    effectColor.putDouble(charIDToTypeID("Rd  "), strokeColor.rgb.red);
    effectColor.putDouble(charIDToTypeID("Grn "), strokeColor.rgb.green);
    effectColor.putDouble(charIDToTypeID("Bl  "), strokeColor.rgb.blue);
    effectDescriptor.putObject(charIDToTypeID("Clr "), charIDToTypeID("RGBC"), effectColor);
    return(effectDescriptor);
}

var tmpC = new SolidColor();
tmpC.rgb.red = textRed
tmpC.rgb.blue = textBlue
tmpC.rgb.green = textGreen

var layerOptions = new ActionDescriptor();
var refr01 = new ActionReference();
var layerProperties = new ActionDescriptor();

layerOptions.putUnitDouble(charIDToTypeID("Scl "), charIDToTypeID("#Prc"), 100.0);

var layerEffects = newStrokeEffect(size, tmpC, "OutF");

layerOptions.putObject(charIDToTypeID("FrFX"), charIDToTypeID("FrFX"), layerEffects);

refr01.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("Lefx"));
refr01.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
layerProperties.putReference(charIDToTypeID("null"), refr01);
layerProperties.putObject(charIDToTypeID("T   "), charIDToTypeID("Lefx"), layerOptions);

try {
    executeAction(charIDToTypeID("setd"), layerProperties, DialogModes.NO);
} catch(ex) {
    if (ex != "Error: User cancelled the operation")
        alert(scriptName + " newLayerEffect() exception caught? line[" + ex.line + "] "  + ex);
}

}

var canvasHeight = app.activeDocument.height;  // Get the height of the canvas, I used height to prevent messing it up on double spreads.
var strokeSize = canvasHeight * 0.0025; // It now calculates the stroke size based on the resolution, instead of the pixels.

strokeStatus = getStrokeSize()

if (typeof strokeStatus !== "number") {
    newStroke(strokeSize);
} else {
    newStroke(getStrokeSize() + 1) //In case the stroke is to small, you can run it again and it'll make the stroke 1 pixel bigger.
}

Votes

Translate

Report

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
Community Expert ,
Dec 05, 2023 Dec 05, 2023

Copy link to clipboard

Copied

If you should have problems amending the Script, please provide the sample images (feel free to remove anything except the Type Layers). 

Votes

Translate

Report

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