文章目录
PS是一款栅格图像编辑软件,模块众多。提供了脚本,动态连接库等多种扩展机制,本文演示如何通过脚本实现色调和颜色/明度相关功能,展示从互联网收集而来的一个小插件,供大家学习交流,请勿用于商业用途。
1.插件界面
- 1.插件界面
- 2.关键代码
- 2.1 色调
- 2.2 颜色/明度
- 3.代码转执行
- 4.作者答疑
本文界面是一系列功能的一部分,将逐步展示,但是功能界面是共同的,如下图所示:
大家可通过源代码阅读,来掌握相关技巧,源代码如下:
//==================== Toning ==============
//
function Toning() {
// Make
function step1(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putClass(PSString.contentLayer);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
var desc3 = new ActionDescriptor();
var desc4 = new ActionDescriptor();
desc4.putDouble(PSKey.Red, 4.64202328585088);
desc4.putDouble(PSKey.Green, 11.4007780887187);
desc4.putDouble(PSEnum.Blue, 148.000006377697);
desc3.putObject(PSKey.Color, PSClass.RGBColor, desc4);
desc2.putObject(PSKey.Type, PSString.solidColorLayer, desc3);
desc1.putObject(PSKey.Using, PSString.contentLayer, desc2);
executeAction(PSEvent.Make, desc1, dialogMode);
};
// Set
function step2(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putString(PSKey.Name, "Tint_edit");
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Set
function step3(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putEnumerated(PSKey.Color, PSKey.Color, PSEnum.Blue);
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Set
function step4(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putEnumerated(PSKey.Mode, PSType.BlendMode, PSEnum.Exclusion);
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Set
function step5(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(PSKey.Opacity, PSUnit.Percent, 50);
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Select
function step6(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Channel, PSClass.Channel, PSClass.Mask);
desc1.putReference(PSString.Null, ref1);
desc1.putBoolean(PSKey.MakeVisible, false);
executeAction(PSEvent.Select, desc1, dialogMode);
};
// Select
function step7(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Channel, PSClass.Channel, PSEnum.RGB);
desc1.putReference(PSString.Null, ref1);
desc1.putBoolean(PSKey.MakeVisible, false);
executeAction(PSEvent.Select, desc1, dialogMode);
};
// Set
function step8(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSString.contentLayer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
var desc3 = new ActionDescriptor();
desc3.putDouble(PSKey.Red, 5.00381469726563);
desc3.putDouble(PSKey.Green, 11.0037231445313);
desc3.putDouble(PSEnum.Blue, 149.001617431641);
desc2.putObject(PSKey.Color, PSClass.RGBColor, desc3);
desc1.putObject(PSKey.To, PSString.solidColorLayer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
step1(); // Make
step2(); // Set
step3(); // Set
step4(); // Set
step5(); // Set
step6(); // Select
step7(); // Select
step8(true, true); // Set
};
cTID = function (s) {
return app.charIDToTypeID(s);
};
sTID = function (s) {
return app.stringIDToTypeID(s);
};
//
// Toning.loadSymbols
// Loading up the symbol definitions like this makes it possible
// to include several of these generated files in one master file
// provided a prefix is specified other than the default. It also
// skips the definitions if PSConstants has already been loaded.
//
Toning.loadSymbols = function () {
var dbgLevel = $.level;
$.level = 0;
try {
PSConstants;
return; // only if PSConstants is defined
} catch (e) {
} finally {
$.level = dbgLevel;
}
var needDefs = true;
$.level = 0;
try {
PSClass;
needDefs = false;
} catch (e) {
} finally {
$.level = dbgLevel;
}
if (needDefs) {
PSClass = function () {
};
PSEnum = function () {
};
PSEvent = function () {
};
PSForm = function () {
};
PSKey = function () {
};
PSType = function () {
};
PSUnit = function () {
};
PSString = function () {
};
}
// We may still end up duplicating some of the following definitions
// but at least we don't redefine PSClass, etc... and wipe out others
PSClass.AdjustmentLayer = cTID('AdjL');
PSClass.Channel = cTID('Chnl');
PSClass.Curves = cTID('Crvs');
PSClass.CurvesAdjustment = cTID('CrvA');
PSClass.HueSatAdjustmentV2 = cTID('Hst2');
PSClass.HueSaturation = cTID('HStr');
PSClass.Layer = cTID('Lyr ');
PSClass.Mask = cTID('Msk ');
PSClass.Point = cTID('Pnt ');
PSClass.RGBColor = cTID('RGBC');
PSEnum.BlackAndWhite = cTID('BanW');
PSEnum.Blue = cTID('Bl ');
PSEnum.Composite = cTID('Cmps');
PSEnum.Exclusion = cTID('Xclu');
PSEnum.Hue = cTID('H ');
PSEnum.Luminosity = cTID('Lmns');
PSEnum.RGB = cTID('RGB ');
PSEnum.Target = cTID('Trgt');
PSEnum.Yellow = cTID('Yllw');
PSEvent.Hide = cTID('Hd ');
PSEvent.Make = cTID('Mk ');
PSEvent.Rasterize = cTID('Rstr');
PSEvent.Select = cTID('slct');
PSEvent.Set = cTID('setd');
PSKey.Adjustment = cTID('Adjs');
PSKey.Color = cTID('Clr ');
PSKey.Colorize = cTID('Clrz');
PSKey.Curve = cTID('Crv ');
PSKey.Cyan = cTID('Cyn ');
PSKey.Green = cTID('Grn ');
PSKey.Group = cTID('Grup');
PSKey.Horizontal = cTID('Hrzn');
PSKey.Lightness = cTID('Lght');
PSKey.Magenta = cTID('Mgnt');
PSKey.MakeVisible = cTID('MkVs');
PSKey.Mode = cTID('Md ');
PSKey.Name = cTID('Nm ');
PSKey.Opacity = cTID('Opct');
PSKey.Red = cTID('Rd ');
PSKey.Start = cTID('Strt');
PSKey.To = cTID('T ');
PSKey.Type = cTID('Type');
PSKey.Using = cTID('Usng');
PSKey.Vertical = cTID('Vrtc');
PSString.Null = sTID('null');
PSString.contentLayer = sTID('contentLayer');
PSString.presetKind = sTID('presetKind');
PSString.presetKindCustom = sTID('presetKindCustom');
PSString.presetKindDefault = sTID('presetKindDefault');
PSString.presetKindType = sTID('presetKindType');
PSString.solidColorLayer = sTID('solidColorLayer');
PSString.tintColor = sTID('tintColor');
PSString.useTint = sTID('useTint');
PSType.BlendMode = cTID('BlnM');
PSType.Ordinal = cTID('Ordn');
PSUnit.Percent = cTID('#Prc');
};
Toning.loadSymbols(); // load up our symbols
//=========================================
// Toning.main
//=========================================
//
Toning.main = function () {
Toning();
};
Toning.main();
// EOF
2.2 颜色/明度
大家可通过源代码阅读,来掌握相关技巧,源代码如下:
//
//==================== Color+Luminosity ==============
//
function Color_Luminosity() {
// Set
function step1(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putString(PSKey.Name, "Luminosity");
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Set
function step2(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putEnumerated(PSKey.Mode, PSType.BlendMode, PSEnum.Luminosity);
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Layer Via Copy
function step3(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
executeAction(sTID('copyToLayer'), undefined, dialogMode);
};
// Set
function step4(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putString(PSKey.Name, "Color");
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Set
function step5(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putEnumerated(PSKey.Mode, PSType.BlendMode, PSClass.Color);
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Select
function step6(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putName(PSClass.Layer, "Luminosity");
desc1.putReference(PSString.Null, ref1);
desc1.putEnumerated(PSString.selectionModifier, PSString.selectionModifierType, PSString.addToSelectionContinuous);
desc1.putBoolean(PSKey.MakeVisible, false);
executeAction(PSEvent.Select, desc1, dialogMode);
};
// Make
function step7(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putClass(PSString.layerGroup);
desc1.putReference(PSString.Null, ref1);
var ref2 = new ActionReference();
ref2.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSKey.From, ref2);
executeAction(PSEvent.Make, desc1, dialogMode);
};
// Set
function step8(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putString(PSKey.Name, "Color+Luminosity");
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Set
function step9(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(PSClass.Layer, PSType.Ordinal, PSEnum.Target);
desc1.putReference(PSString.Null, ref1);
var desc2 = new ActionDescriptor();
desc2.putEnumerated(PSClass.Color, PSClass.Color, PSEnum.Blue);
desc1.putObject(PSKey.To, PSClass.Layer, desc2);
executeAction(PSEvent.Set, desc1, dialogMode);
};
// Select
function step10(enabled, withDialog) {
if (enabled != undefined && !enabled)
return;
var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putName(PSClass.Layer, "Luminosity");
desc1.putReference(PSString.Null, ref1);
desc1.putBoolean(PSKey.MakeVisible, false);
executeAction(PSEvent.Select, desc1, dialogMode);
};
step1(); // Set
step2(); // Set
step3(); // Layer Via Copy
step4(); // Set
step5(); // Set
step6(); // Select
step7(); // Make
step8(); // Set
step9(); // Set
step10(); // Select
};
cTID = function (s) {
return app.charIDToTypeID(s);
};
sTID = function (s) {
return app.stringIDToTypeID(s);
};
//
// Color_Luminosity.loadSymbols
// Loading up the symbol definitions like this makes it possible
// to include several of these generated files in one master file
// provided a prefix is specified other than the default. It also
// skips the definitions if PSConstants has already been loaded.
//
Color_Luminosity.loadSymbols = function () {
var dbgLevel = $.level;
$.level = 0;
try {
PSConstants;
return; // only if PSConstants is defined
} catch (e) {
} finally {
$.level = dbgLevel;
}
var needDefs = true;
$.level = 0;
try {
PSClass;
needDefs = false;
} catch (e) {
} finally {
$.level = dbgLevel;
}
if (needDefs) {
PSClass = function () {
};
PSEnum = function () {
};
PSEvent = function () {
};
PSForm = function () {
};
PSKey = function () {
};
PSType = function () {
};
PSUnit = function () {
};
PSString = function () {
};
}
// We may still end up duplicating some of the following definitions
// but at least we don't redefine PSClass, etc... and wipe out others
PSClass.Color = cTID('Clr ');
PSClass.Layer = cTID('Lyr ');
PSEnum.Blue = cTID('Bl ');
PSEnum.Luminosity = cTID('Lmns');
PSEnum.Target = cTID('Trgt');
PSEvent.Make = cTID('Mk ');
PSEvent.Select = cTID('slct');
PSEvent.Set = cTID('setd');
PSKey.From = cTID('From');
PSKey.MakeVisible = cTID('MkVs');
PSKey.Mode = cTID('Md ');
PSKey.Name = cTID('Nm ');
PSKey.To = cTID('T ');
PSString.Null = sTID('null');
PSString.addToSelectionContinuous = sTID('addToSelectionContinuous');
PSString.layerGroup = sTID('layerSection');
PSString.selectionModifier = sTID('selectionModifier');
PSString.selectionModifierType = sTID('selectionModifierType');
PSType.BlendMode = cTID('BlnM');
PSType.Ordinal = cTID('Ordn');
};
Color_Luminosity.loadSymbols(); // load up our symbols
//=========================================
// Color_Luminosity.main
//=========================================
//
Color_Luminosity.main = function () {
Color_Luminosity();
};
Color_Luminosity.main();
// EOF
3.代码转执行
使用记事本或者notepad++等文本编辑器,将代码拷贝至文本文件,然后修改后缀名为js或者jsx,最后再PS软件菜单文件》脚本》浏览,点执行即可,也可以将脚本放在预设文件夹内,会显示再脚本下的二级子菜单。如下图所示:
如有疑问,请留言。