使用色彩平衡修改颜色

时间:2014-10-01 08:24:16

标签: 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();
    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 );
}

这很好用,在我的例子中为图像的中间色添加了青色。我想知道的是:如果我想用-50青色的类似值修改RGB颜色(如上例所示)我该如何处理呢?是否最好将颜色更改为CMKY,进行适当调整,然后再更改为RGB。只有我读过一些我最好从RGB到L * ab的地方(我知道该怎么做)。

2 个答案:

答案 0 :(得分:3)

如果要在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:

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表示。

具有不完全适合CMY跟随的RGB颜色:

RGB --inaccurate--> CMY --inaccurate--> RGB // output !== input
RGB --accurate--> L*ab --inaccurate--> CMY --accurate--> L*ab --inaccurate--> RGB

我们看到引入了相同的不准确之处。

编辑colorBalanceLayer内,您可以调用其他一些功能。我看不出他们到底做了什么,所以我只能将以下内容作为草稿。

当然可以在RGB颜色内更改青色,品红色或黄色而无需任何转换。为此,我们需要了解两件事:

1)CMY适用于印刷颜料,RGB发光,因此它们可以通过亮度互补。这意味着:向CMY添加一些值会使颜色变暗,为RGB添加一些值会使颜色更亮。

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

因此,以下操作会导致相同的结果(CMY left-,RGB right-hand):

cyan += 20   ===   red -= 20;
magenta  -= 30   ===   green += 30;
yellow += 40   ===   blue -= 40;

也许可以调整你的功能,这样它们也适用于RGB。

答案 1 :(得分:0)

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

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;
}