Javascript 用色彩平衡调整色彩

Javascript 用色彩平衡调整色彩,javascript,colors,photoshop-script,Javascript,Colors,Photoshop Script,在JavaScript中,我可以使用如下函数调整图像的颜色平衡 colorBalanceLayer(-50,0,0) function colourBalanceLayer(cya, mag, yel) { // cyan, magenta, yellow values are between -100 & +100 var id713 = charIDToTypeID( "ClrB" ); var desc162 = new ActionDescriptor();

在JavaScript中,我可以使用如下函数调整图像的颜色平衡

colorBalanceLayer(-50,0,0)

function colourBalanceLayer(cya, mag, yel)
{
  // cyan, magenta, yellow values are between -100 & +100
    var id713 = charIDToTypeID( "ClrB" );
    var desc162 = new ActionDescriptor();
    var id714 = charIDToTypeID( "ShdL" );
    var list37 = new ActionList();
    list37.putInteger( 0 );
    list37.putInteger( 0 );
    list37.putInteger( 0 );
    desc162.putList( id714, list37 );
    var id715 = charIDToTypeID( "MdtL" );
    var list38 = new ActionList();
    list38.putInteger( cya );
    list38.putInteger( mag );
    list38.putInteger( yel );
    desc162.putList( id715, list38 );
    var id716 = charIDToTypeID( "HghL" );
    var list39 = new ActionList();
    list39.putInteger( 0 );
    list39.putInteger( 0 );
    list39.putInteger( 0 );
    desc162.putList( id716, list39 );
    var id717 = charIDToTypeID( "PrsL" );
    desc162.putBoolean( id717, true );
    executeAction( id713, desc162, DialogModes.NO );
}

这很好,在我的示例中,在图像的中间色调中添加了大量青色。我想知道的是:如果我想将RGB颜色修改为-50青色的类似值(如上面的示例),我将如何进行?最好将颜色更改为CMKY,适当调整,然后再更改回RGB。只有我在某个地方读到,我最好先从RGB转到L*ab(我知道怎么做)。

如果你想在RGB颜色上使用该功能,你必须将其转换为CMY,因为
colorBalanceLayer
设计用于CMY颜色。可通过以下功能轻松完成:

// r, g, b and c, m, y are in the range of 0 to 255

function rgb_cmy(r, g, b) {
    return [].map.call(arguments, function(v) {return 255 - v;});
}

// return value is an array of the form: [cyan, magenta, yellow]
rgb_cmy(c, m, y) // returns an array of the form: [red, green, blue]
处理后,使用相同的功能转换回RGB:

// r, g, b and c, m, y are in the range of 0 to 255

function rgb_cmy(r, g, b) {
    return [].map.call(arguments, function(v) {return 255 - v;});
}

// return value is an array of the form: [cyan, magenta, yellow]
rgb_cmy(c, m, y) // returns an array of the form: [red, green, blue]
我们是否应该将L*ab转换为中间步骤编号

RGB和CMY是两个大小相同的不同颜色空间:这意味着:它们都包含相同数量的颜色。但这两个空间并不完全覆盖彼此。这意味着:RGB包含一些不能精确表示为CMY颜色的颜色,反之亦然。因此,每次转换都会引入一些不精确性

L*ab颜色空间比其他颜色空间大得多,完全覆盖RGB和CMY。这意味着:每个RGB(或CMY)颜色都可以精确地表示为L*ab颜色,但有许多L*ab颜色无法用RGB或CMY表示

RGB颜色与CMY不完全匹配,出现以下情况:

RGB --inaccurate--> CMY --inaccurate--> RGB // output !== input
RGB --accurate--> L*ab --inaccurate--> CMY --accurate--> L*ab --inaccurate--> RGB
我们看到同样的误差也被引入

EDIT
colorBalanceLayer
内部调用一些其他函数。我看不出他们到底在做什么,所以我只能作为草稿给出以下内容

当然,可以在RGB颜色中更改青色、洋红或黄色,而无需任何转换。为此,我们需要知道两件事:

1) CMY与印刷颜料一起工作,RGB与发射光一起工作,因此它们在亮度上是互补的。这意味着:向CMY添加一些值会使颜色更暗,向RGB添加一些值会使颜色更亮

2) RGB和CMY的色轮旋转60度,因此它们的颜色互补。这意味着:两者的相反颜色构成互补对青色/红色、品红/绿色和黄色/蓝色

因此,以下操作会导致相同的结果(CMY左侧,RGB右侧):


可以调整您的函数,使其也能与RGB一起工作。

回答我自己的问题:可以返回“平衡”颜色的值。这不是一个优雅的解决方案:

colourBalanceLayer("C0FFEE", 100,0,0);

function colourBalanceLayer(hexcol, cya, mag, yel)
{

  var pixH = 10;
  var pixV = 10;

  // create a document to work with
  var docRef = app.documents.add(pixH *2, pixV *2, 72, "colours")
  var newDoc = app.activeDocument;

  // =======================================================
  var id70 = charIDToTypeID( "Fl  " );
  var desc18 = new ActionDescriptor();
  var id71 = charIDToTypeID( "From" );
  var desc19 = new ActionDescriptor();
  var id72 = charIDToTypeID( "Hrzn" );
  var id73 = charIDToTypeID( "#Pxl" );
  desc19.putUnitDouble( id72, id73, 10.000000 );
  var id74 = charIDToTypeID( "Vrtc" );
  var id75 = charIDToTypeID( "#Pxl" );
  desc19.putUnitDouble( id74, id75, 9.000000 );
  var id76 = charIDToTypeID( "Pnt " );
  desc18.putObject( id71, id76, desc19 );
  var id77 = charIDToTypeID( "Tlrn" );
  desc18.putInteger( id77, 32 );
  var id78 = charIDToTypeID( "AntA" );
  desc18.putBoolean( id78, true );
  var id79 = charIDToTypeID( "Usng" );
  var id80 = charIDToTypeID( "FlCn" );
  var id81 = charIDToTypeID( "FrgC" );
  desc18.putEnumerated( id79, id80, id81 );
  executeAction( id70, desc18, DialogModes.NO );

  // cyan, magenta, yellow values are between -100 & +100
  var id713 = charIDToTypeID( "ClrB" );
  var desc162 = new ActionDescriptor();
  var id714 = charIDToTypeID( "ShdL" );
  var list37 = new ActionList();
  list37.putInteger( 0 );
  list37.putInteger( 0 );
  list37.putInteger( 0 );
  desc162.putList( id714, list37 );
  var id715 = charIDToTypeID( "MdtL" );
  var list38 = new ActionList();
  list38.putInteger( cya );
  list38.putInteger( mag );
  list38.putInteger( yel );
  desc162.putList( id715, list38 );
  var id716 = charIDToTypeID( "HghL" );
  var list39 = new ActionList();
  list39.putInteger( 0 );
  list39.putInteger( 0 );
  list39.putInteger( 0 );
  desc162.putList( id716, list39 );
  var id717 = charIDToTypeID( "PrsL" );
  desc162.putBoolean( id717, true );
  executeAction( id713, desc162, DialogModes.NO );

  // =======================================================
  var id40 = charIDToTypeID( "setd" );
  var desc11 = new ActionDescriptor();
  var id41 = charIDToTypeID( "null" );
  var ref5 = new ActionReference();
  var id42 = charIDToTypeID( "Clr " );
  var id43 = charIDToTypeID( "FrgC" );
  ref5.putProperty( id42, id43 );
  desc11.putReference( id41, ref5 );
  var id44 = charIDToTypeID( "T   " );
  var desc12 = new ActionDescriptor();
  var id45 = charIDToTypeID( "Rd  " );
  desc12.putDouble( id45, 255.000000 );
  var id46 = charIDToTypeID( "Grn " );
  desc12.putDouble( id46, 255.000000 );
  var id47 = charIDToTypeID( "Bl  " );
  desc12.putDouble( id47, 255.000000 );
  var id48 = charIDToTypeID( "RGBC" );
  desc11.putObject( id44, id48, desc12 );
  executeAction( id40, desc11, DialogModes.NO );

  var selRegion = null;
  selRegion = Array(Array(pixH, pixV),
  Array(pixH + 1, pixV),
  Array(pixH + 1, pixV + 1),
  Array(pixH, pixV + 1),
  Array(pixH, pixV));
  newDoc.selection.select(selRegion);
  var histR = newDoc.channels.getByName("Red").histogram;
  var histG = newDoc.channels.getByName("Green").histogram;
  var histB = newDoc.channels.getByName("Blue").histogram;
  var returnColor = new RGBColor();

  for (iHistR in histR)
  {
  if (histR[iHistR]!=0)
    {
      returnColor.red = iHistR;
    }
  }
  for (iHistG in histG)
  {
  if(histG[iHistG]!=0)
    {
      returnColor.green = iHistG;
    }
  }
  for (iHistB in histB)
  {
    if(histB[iHistB]!=0)
    {
      returnColor.blue = iHistB;
    }
  }

  alert(returnColor.red+","+returnColor.green+","+returnColor.blue);

  return returnColor;
}

谢谢你的解释。现在我需要做的就是关联颜色平衡变化的值。RGB128128128+100红色变为156,97,97&60,60,60,变为91,33,33。看起来红移了30次,其他的红移了30次。我必须进一步调查以获得更准确的值。@ghoullfool更改颜色平衡必须保持颜色的亮度稳定。因此,如果
red+=50
应该有
green-=25
blue-=25