Skip to content

Instantly share code, notes, and snippets.

@joshmvandercom
Created January 5, 2012 18:36
Show Gist options
  • Save joshmvandercom/1566555 to your computer and use it in GitHub Desktop.
Save joshmvandercom/1566555 to your computer and use it in GitHub Desktop.
Change path colors
var colorObject;
var hexes = ['5b7f52', '6ea041', '62a04a', '5b7f52', '6cb84e'];
var colors = [];
for (var h = 0; h < hexes.length; h++) {
colorObject = new SolidColor();
colorObject.rgb['hexValue'] = hexes[h];
colors.push(colorObject);
}
function getRandomColor() {
var n = Math.floor(Math.random() * colors.length);
return colors[n];
}
function setColorOfFillLayer(color) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( stringIDToTypeID('contentLayer'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var fillDesc = new ActionDescriptor();
var colorDesc = new ActionDescriptor();
colorDesc.putDouble( charIDToTypeID('Rd '), color.rgb.red );
colorDesc.putDouble( charIDToTypeID('Grn '), color.rgb.green );
colorDesc.putDouble( charIDToTypeID('Bl '), color.rgb.blue );
fillDesc.putObject( charIDToTypeID('Clr '), charIDToTypeID('RGBC'), colorDesc );
desc.putObject( charIDToTypeID('T '), stringIDToTypeID('solidColorLayer'), fillDesc );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
}
function selectLayerWithIndex(idx) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), idx);
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}
function getLayerWithIndex(idx) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( "Lyr " ), idx);
return executeActionGet(ref);
}
function main() {
var totalLayers = activeDocument.layers.length;
// Layer is informative, layerRef is actionable
var layer, layerRef;
var list, fillColor, solidColorLayer;
for(i=0; i<totalLayers; i++) {
layer = activeDocument.layers[i];
// selectLayerWithIndex(i);
oppositeI = (i - totalLayers + 1) * -1
if(layer.kind == 'LayerKind.SOLIDFILL') {
selectLayerWithIndex(oppositeI);
setColorOfFillLayer( getRandomColor(), oppositeI );
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment