簡體   English   中英

Javascript Photoshop:用於更改現有文本圖層的文本顏色的語法

[英]Javascript Photoshop: Syntax for changing text color of an already existing text layer

我創建了一個photoshop動作,該動作將一串白色文本添加到照片中。 在此操作中,我編寫了一個腳本,該腳本可擴展畫布的大小,並在額外的畫布空間內調整文本的大小以適合圖像的尺寸,從而在照片上創建“文本標簽”。 但是,在執行動作和腳本之后,選項卡中文本的顏色通常是...不是白色。

我可以使用什么腳本語法將文本層的顏色更改為白色,以確保它不會隨機更改顏色?

作為參考,這是我的腳本中用於編輯文本層的部分:

#target photoshop


var originalRulerUnits = app.preferences.rulerUnits;  
app.preferences.rulerUnits = Units.INCHES; 

var doc = app.activeDocument;  
var layer = doc.activeLayer;  

app.preferences.rulerUnits = Units.PIXELS; 

//create the color for the tab extension
var black = new SolidColor();  
black.rgb.hexValue = "111111";
app.backgroundColor = black;


//*****************************FOR EXTENDING CANVAS AND POSITIONING/RESIZING THE TEXT LAYER*********************************

var orgWidth = doc.width;
var orgHeight = doc.height;

if(orgWidth>orgHeight) //if document is landscape, resize text to fill a smaller portion of the tab
{
    layer.resize(((orgWidth-orgWidth*.2)/(layer.bounds[2]-layer.bounds[0]))*100,(orgWidth*.017/(layer.bounds[3]-layer.bounds[1]))*100,AnchorPosition.TOPLEFT);     

    doc.resizeCanvas(doc.width, (orgHeight + orgWidth*.04), AnchorPosition.TOPCENTER);

    layer.translate(undefined, new UnitValue(0-layer.bounds[1].as('px'),'px'));  
    layer.translate(undefined, orgHeight); 
    layer.translate(undefined, (orgWidth*.02-((layer.bounds[3]-layer.bounds[1])/2.3)));    


}
else //otherwise, resize text to fill a larger portion of the tab
{
    layer.resize(((orgWidth-orgWidth*.05)/(layer.bounds[2]-layer.bounds[0]))*100,(orgWidth*.017/(layer.bounds[3]-layer.bounds[1]))*100,AnchorPosition.TOPLEFT); 

    doc.resizeCanvas(doc.width, (orgHeight + orgWidth*.04), AnchorPosition.TOPCENTER);

    layer.translate(undefined, new UnitValue(0-layer.bounds[1].as('px'),'px'));  
    layer.translate(undefined, orgHeight); 
    layer.translate(undefined, (orgWidth*.02-((layer.bounds[3]-layer.bounds[1])/2.3))); 



}


var layerWidth = Number(layer.bounds[2] - layer.bounds[0]);
var dX = (orgWidth - layerWidth) / 2 - Number(layer.bounds[0]);
layer.translate(dX, undefined);


doc.flatten(); //flatten the text into the photo

app.preferences.rulerUnits = originalRulerUnits;  

您可能更容易編寫文本添加代碼。 這是我為這種場合編寫的一個方便的函數。 文字居中對齊,但您可以進行調整。 (我本來應該包含該功能的那部分,但看起來像是意大利面條)

var x = (app.activeDocument.width.value)/2;
var y = (app.activeDocument.height.value)/2;

createText("Arial-BoldMT", 10.0, 255,255,255, "Gwen Stefanni is bananas", x, y)


// function CREATE TEXT(typeface, size, R, G, B, text content, text X pos, text Y pos)
// --------------------------------------------------------
function createText(fface, size, colR, colG, colB, content, tX, tY)
{

  // Add a new layer in the new document
  var artLayerRef = app.activeDocument.artLayers.add()

  // Specify that the layer is a text layer
  artLayerRef.kind = LayerKind.TEXT

  //This section defines the color of the hello world text
  textColor = new SolidColor();
  textColor.rgb.red = colR;
  textColor.rgb.green = colG;
  textColor.rgb.blue = colB;

  //Get a reference to the text item so that we can add the text and format it a bit
  textItemRef = artLayerRef.textItem
  textItemRef.font = fface;
  textItemRef.contents = content;
  textItemRef.color = textColor;
  textItemRef.size = size
  textItemRef.position = new Array(tX, tY) //pixels from the left, pixels from the top
  var just = Justification.CENTER;

  activeDocument.activeLayer.textItem.justification = just;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM