When is it okay to use global variables, see example script

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

Guest

When is it okay to use global variables, see example script

Post by Guest »

I'm wondering when I should use a global variable. In the script attached I use a function to set and reset my initial values. Other functions call many of these variables throughout my script. If I were to pass them to the function and make them local, my function calls would be a huge string.

Plus, looking at the script it doesn't seem logical that I would NEED them to be local. Please take a look at my script and tell me if you think I should localize some of these variables. Thank you.

function changeHSB(someColor,moveAmt){
var initSat = someColor.hsb.saturation;
someColor.hsb.saturation = 100.00;
//alert("Saturation is: " + someColor.hsb.saturation);
for(s=9;s>0;s--){//you will have nine rows of saturation changes
someColor.hsb.brightness = Number(100);

for(i=8;i>0;i--){

makeSelection(x1,y1,x2,y2);
fillIt(someColor,100);//a SolidColor and opacity value
someColor.hsb.brightness -= 10;
writeText(docRef,x1,y1,someColor,moveAmt);
increaseX(moveAmt);
}
someColor.hsb.saturation-= 10.0;
newLine(moveAmt);

}
}


function makeNewDoc(){
preferences.rulerUnits = Units.INCHES;
var newDocumentRef = documents.add(34, 11, 150.0, "Color Swatches");
newDocumentRef = null;
preferences.rulerUnits = Units.PIXELS;
}


function fillIt(somecolor,opacity){
docRef.selection.fill(somecolor,ColorBlendMode.NORMAL,100,false);
}
function increaseX(incAmt){
x1+=incAmt // move selection to the right
x2+=incAmt // set size of selection, will be 100px
}
function newLine(incAmt){
y1+=incAmt;
y2+=incAmt;
x1=0;
x2=squareSize;
}
function decreaseHSB(someColor,HSorB,amt){

eval("someColor.hsb." + HSorB + "-= 10" );
}

function makeSelection(x1,y1,x2,y2){
bounds = new Array(new Array(x1,y1), new Array(x2,y1), new Array(x2,y2), new Array(x1,y2));
docRef.selection.select(bounds,SelectionType.REPLACE, null, false);
}

function resolveColorModes(img){
if(img.mode == DocumentMode.BITMAP) {

img.changeMode(ChangeMode.GRAYSCALE);

} else if (img.mode == DocumentMode.CMYK){
img.changeMode(ChangeMode.RGB);
} else if (img.mode == DocumentMode.INDEXEDCOLOR){
img.changeMode(ChangeMode.RGB);
}

}

function writeText(myDoc,textPosX,textPosY,someColor,moveIt){
var myLayerRef = myDoc.artLayers.add();
myLayerRef.kind = LayerKind.TEXT;
myLayerRef.name = "Filename";
var myTextRef = myLayerRef.textItem;

//Set your parameters below this line


// Set font size in Points
myTextRef.size = 10;

//Set font - use GetFontName.js to get exact name
myTextRef.font = "Arial";

//Set text colour in RGB values
var newColor = new SolidColor();
newColor.rgb.red = 0;
newColor.rgb.green = 0;
newColor.rgb.blue = 0;
myTextRef.color = newColor;

// Set the position of the text - percentages from left first, then from top.
myTextRef.position = new Array( textPosX, textPosY+=squareSize+15);

// Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
myLayerRef.blendMode = BlendMode.NORMAL;

// select opacity in percentage
myLayerRef.opacity = 100;

//Get value of current color
//currentColor = something
var theRed = Math.round(someColor.rgb.red);
var theGreen = Math.round(someColor.rgb.green);
var theBlue = Math.round(someColor.rgb.blue);

myTextRef.contents = "R:"+theRed+", G:"+theGreen+", B:"+theBlue;
activeDocument.flatten();
}
function moveSelection(){
x1 = 0;
x2 = 150;
y1+= (x2+hMove);//makes squares 40 pixels apart
y2+= (x2+vMove);
}
function setResetValues(){
docRef = activeDocument;
sqsAcross = 0;

squareSize = 150;
x1 = 0;
y1 = 0;
x2 = squareSize;
y2 = squareSize;
hMove = 40;
vMove = 40;
selectMoveAmount = squareSize+hMove;
opaq = 100;
maxSquares = 384;//change later to reflect diff doc sizes,this works for 36x24
numSquares = 0;
maxAcross = 24;
documentRes = 150;
}

var currentColor,sqsAcross,selectMoveAmount,squareSize,x1,y1,x2,y2,opaq,
maxsquares,numSquares,maxAcross,documentRes,hMove,vMove;



makeNewDoc();
docRef = activeDocument;
setResetValues();
var fgColor = foregroundColor;
backgroundColor = fgColor;

resolveColorModes(docRef);
changeHSB(fgColor,selectMoveAmount);