Last active
January 3, 2025 20:59
Changes the hue of every layer of the active document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// enable double clicking | |
#target photoshop | |
// make Photoshop the frontmost application | |
app.bringToFront(); | |
// all the strings that need localized | |
var strHistoryStepName = localize("$$$/JavaScripts/Recolor/Menu=Recolor" ); | |
var doc = app.activeDocument; | |
// Create only one history state for the entire script | |
doc.suspendHistory(strHistoryStepName, "main()"); | |
function main(){ | |
try { | |
var originalRulerUnits = app.preferences.rulerUnits; // remember current setting | |
app.preferences.rulerUnits = Units.PIXELS; // set Pixel for checking bounds | |
recolorAllLayers(doc); | |
app.preferences.rulerunits = originalRulerUnits; // restore unit | |
} catch(e) { | |
return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script | |
} | |
} | |
function recolorAllLayers(o){ | |
try{ | |
for(var i = o.artLayers.length-1; 0 <= i; i--){ // top level layers | |
var layer = o.artLayers[i]; | |
if( layer.allLocked | |
|| layer.pixelsLocked | |
|| layer.positionLocked | |
|| layer.transparentPixelsLocked ) { | |
continue; // skip locked | |
} | |
if((LayerKind.NORMAL == layer.kind)){ | |
if( (0 != layer.bounds[2]) // width = 0 | |
|| (0 != layer.bounds[3])){ // height = 0 | |
doc.activeLayer = layer; | |
hueValue = (Math.random() * 361 | 0) - 180 | |
applyHsl( hueValue, 0, 0 ); | |
} | |
} | |
} | |
for(var i = o.layerSets.length-1; 0 <= i; i--){ // LayerSet | |
var layerSet = o.layerSets[i]; | |
if(layerSet.allLocked){ | |
continue; // skip locked | |
} | |
recolorAllLayers(layerSet); // recursive call | |
} | |
}catch(e){ | |
; // do nothing | |
} | |
} | |
/** | |
* | |
* @param {number} hue | |
* @param {number} saturation | |
* @param {number} lightness | |
*/ | |
// From http://graphicdesign.stackexchange.com/questions/25619/is-there-a-way-to-automate-changing-the-hue-of-many-layers-with-a-different-valu | |
function applyHsl( hue, saturation, lightness ) { | |
var HUE_SAT_ADJUSTMENT_V2_SYM = 'Hst2'; | |
var COLORIZE_SYM = 'Clrz'; | |
var ADJUSTMENT_SYM = 'Adjs'; | |
var HUE_SYM = 'H '; | |
var SATURATION_SYM = 'Strt'; | |
var LIGHTNESS_SYM = 'Lght'; | |
var HUE_SATURATION_SYM = 'HStr'; | |
var colorizeDescriptor = new ActionDescriptor(); | |
var hueSatDescriptor = new ActionDescriptor(); | |
var hueSatAdjustmentList = new ActionList(); | |
colorizeDescriptor.putBoolean( charIDToTypeID( COLORIZE_SYM ), false ); | |
hueSatDescriptor.putInteger( charIDToTypeID( HUE_SYM ), hue ); | |
hueSatDescriptor.putInteger( charIDToTypeID( SATURATION_SYM ), saturation ); | |
hueSatDescriptor.putInteger( charIDToTypeID( LIGHTNESS_SYM ), lightness ); | |
hueSatAdjustmentList.putObject( charIDToTypeID( HUE_SAT_ADJUSTMENT_V2_SYM ), hueSatDescriptor ); | |
colorizeDescriptor.putList( charIDToTypeID( ADJUSTMENT_SYM ), hueSatAdjustmentList ); | |
executeAction( | |
charIDToTypeID( HUE_SATURATION_SYM ), | |
colorizeDescriptor, | |
DialogModes.NO | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment