Skip to content

Instantly share code, notes, and snippets.

@joonaspaakko
Last active October 11, 2023 16:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonaspaakko/347c270f15bc1d11efe9 to your computer and use it in GitHub Desktop.
Save joonaspaakko/347c270f15bc1d11efe9 to your computer and use it in GitHub Desktop.
The script switches foreground color to the next color in the "colors" variable each time you run the code.
#target photoshop
// https://gist.github.com/joonaspaakko/347c270f15bc1d11efe9
var colors = [ "#0033ff", "#9a3838", "#888888", "#a6ad1e" ];
function init() {
var read = tempFile.read(),
color = colors[ read ];
setColor(
hexToRgb( color ).r,
hexToRgb( color ).g,
hexToRgb( color ).b
);
var newNumber = read === ( colors.length-1 ) ? 0 : read + 1;
tempFile.write( newNumber );
}
function setColor( r, g, b ) {
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc25 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idClr = charIDToTypeID( "Clr " );
var idFrgC = charIDToTypeID( "FrgC" );
ref5.putProperty( idClr, idFrgC );
desc25.putReference( idnull, ref5 );
var idT = charIDToTypeID( "T " );
var desc26 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc26.putDouble( idRd, r );
var idGrn = charIDToTypeID( "Grn " );
desc26.putDouble( idGrn, g );
var idBl = charIDToTypeID( "Bl " );
desc26.putDouble( idBl, b );
var idRGBC = charIDToTypeID( "RGBC" );
desc25.putObject( idT, idRGBC, desc26 );
var idSrce = charIDToTypeID( "Srce" );
desc25.putString( idSrce, """photoshopPicker""" );
executeAction( idsetd, desc25, DialogModes.NO );
}
// https://gist.github.com/Arahnoid/9923989
/**
* hexToRgb
* @param string hex Hexadicimal value
* @return number Color of selected chanel Red, Green or Blue
* @usage //alert( hexToRgb("#0033ff").g ); // "51";
* @usage //alert( hexToRgb("0033ff").g ); // "51";
*/
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
var filePath = (new File($.fileName)).parent + "/Circulate foreground color tempFile.txt",
tempFile = {
write: function( color_number ) {
var file = new File( filePath );
file.open('w');
file.writeln( color_number );
file.close();
},
read: function() {
var file = new File( filePath ),
color_number = "",
fileOpen = file.open('r');
while( !file.eof )
color_number += file.readln();
file.close();
return !fileOpen ? 0 : parseInt( color_number );
}
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment