Colors after effects脚本颜色填充设置值

Colors after effects脚本颜色填充设置值,colors,extendscript,setvalue,Colors,Extendscript,Setvalue,我有下面的代码,我需要使用颜色表单colorpicker并将其设置为我形状的填充颜色。 我不知道如何从colorpicker获取值并将其放入 //添加ELIPSE函数 //====== 函数addElipse(){ var project=app.project; var comp=project.activeItem; if(comp==null){ comp=project.items.addComp(“CompCreated”,19201080,1,30,30); } comp.openI

我有下面的代码,我需要使用颜色表单colorpicker并将其设置为我形状的填充颜色。
我不知道如何从colorpicker获取值并将其放入

//添加ELIPSE函数
//======
函数addElipse(){
var project=app.project;
var comp=project.activeItem;
if(comp==null){
comp=project.items.addComp(“CompCreated”,19201080,1,30,30);
}
comp.openInViewer();
var shapeLayer=comp.layers.addShape();
var elipseGroup=shapeLayer.property(“Contents”).addProperty(“ADBE向量形状-椭圆”);
elipseGroup.property(“Size”).setValue([300300]);
var fillGroup=shapeLayer.property(“Contents”).addProperty(“ADBE矢量图形-填充”);
fillGroup.property(“Color”).setValue(………);}
//颜色选择器
//======
函数颜色选择器(结果颜色){
var hexToRGB=函数(十六进制){
var r=hex>>16;
变量g=hex>>8&0xFF;
变量b=十六进制&0xFF;
返回[r,g,b];};
var color_decimal=$.colorPicker();

if(color_decimal我制作了一个简单的示例脚本,还有一些功能,希望能够回答您的问题

它会更改所有选定特性的颜色值

如果属性已设置动画,它将在当前合成时间添加新的关键帧

如果找到表达式,用户需要确认属性值的更改。在这种情况下,如果由表达式设置,则颜色可能不会更改。只有属性值不同

如果所选属性不是颜色属性,当然会跳过它

mouseover
事件中,按钮的颜色将更改为第一个选定属性的颜色值

整个脚本包装在
aecolorpicker
下,因此可以轻松扩展

所有颜色转换都是由aecolorpicker.prototype.colorUtils进行的

它本来可以写得更好一点,但作为一个例子,它现在就可以了

您可以从下载并复制到“ScriptUI面板”文件夹中

快速预览:

下面是完整的脚本:

'use strict';

function aecolorpicker () {}

aecolorpicker.prototype.root = this;

aecolorpicker.prototype.ui = function (thisObj)
{
  var win = {};

  win.pal = thisObj instanceof Panel ? thisObj : new Window('palette', 'AE Colour Picker', undefined, {resizeable: true});

  if (win.pal === null) return win.pal;

  var res = "Group {orientation: 'column', alignment: ['fill', 'fill'], preferredSize: [128, 64], margin: 0, spacing: 0, \
    btnColor: Button {size:[100, 50], alignment: ['fill', 'fill'], properties: {style: 'toolbutton'}}\
  }";

  win.ui = win.pal.add(res);
  win.btnColor = win.ui.btnColor;

  win.pal.layout.layout(true);

  win.pal.onResizing = win.pal.onResize = function ()
  {
    this.layout.resize();
  };

  if (win.pal !== null && win.pal instanceof Window)
  {
    win.pal.show();
  }

  return win;
};

aecolorpicker.prototype.colorUtils = {
  "AdobeRGBA2HEX": function (rgba)
  {
    var pad2 = function (c)
    {
      return c.length == 1 ? '0' + c : '' + c;
    };

    var convertDecimalToHex = function (d)
    {
      return Math.round(parseFloat(d) * 255).toString(16);
    };

    var hex =
    [
      pad2(Math.round(rgba[0] * 255).toString(16)),
      pad2(Math.round(rgba[1] * 255).toString(16)),
      pad2(Math.round(rgba[2] * 255).toString(16))
    ];

    return '#' + hex.join('').toUpperCase();
  },

  "HEX2AdobeRGBA": function (hex)
  {
    if (hex.charAt(0) === '#') hex = hex.substr(1);

    if ((hex.length < 2) || (hex.length > 6)) return null;

    var
    values = hex.split(''),
    r,
    g,
    b;

    if (hex.length === 2)
    {
      r = parseInt(values[0].toString() + values[1].toString(), 16);
      g = r;
      b = r;
    }
    else if (hex.length === 3)
    {
      r = parseInt(values[0].toString() + values[0].toString(), 16);
      g = parseInt(values[1].toString() + values[1].toString(), 16);
      b = parseInt(values[2].toString() + values[2].toString(), 16);
    }
    else if (hex.length === 6)
    {
      r = parseInt(values[0].toString() + values[1].toString(), 16);
      g = parseInt(values[2].toString() + values[3].toString(), 16);
      b = parseInt(values[4].toString() + values[5].toString(), 16);
    }
    else
    {
      return null;
    }

    return [
      r / 255,
      g / 255,
      b / 255,
      1
    ];
  },

  "CP2AdobeRGBA": function (cpdec)
  {
    return [
      (parseInt(cpdec.toString(16), 16) >> 16) / 255,
      (parseInt(cpdec.toString(16), 16) >> 8 & 0xFF) / 255,
      (parseInt(cpdec.toString(16), 16) & 0xFF) / 255,
      1
    ];
  },

  "luminance": function (hex, lum)
  {
    // validate hex string
    hex = String(hex).replace(/[^0-9a-f]/gi, '');
    if (hex.length < 6) hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];

    lum = lum || 0;

    var result = "#", c, i;

    for (i = 0; i < 3; i++)
    {
      c = parseInt(hex.substr(i * 2, 2), 16);
      c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
      result += ('00' + c).substr(c.length);
    }

    return result;
  }
};

aecolorpicker.prototype.getFirstSelPropColorValue = function ()
{
  if (app.project.activeItem instanceof CompItem)
  {
    var curLayer, curProp;

    if (app.project.activeItem.selectedLayers.length > 0)
    {
      curLayer = app.project.activeItem.selectedLayers[0];

      if (curLayer.selectedProperties.length > 0)
      {
        if (!curLayer.selectedProperties[0].hasOwnProperty('canAddProperty')) curProp = curLayer.selectedProperties[0];
        else if (curLayer.selectedProperties.length > 1) curProp = curLayer.selectedProperties[1];
        else return null;

        /*
        6212 - color propertyType
        6418 - color propertyValueType
        */
        if (curProp.propertyValueType === 6418) return curProp.valueAtTime(app.project.activeItem.time, false);
      }
    }
  }

  return null;
};

aecolorpicker.prototype.setSelPropsColorValue = function (rgba)
{
  if (app.project.activeItem instanceof CompItem)
  {
    var curLayer, curProp;

    for (var p = 0, pLen = app.project.activeItem.selectedLayers.length; p < pLen; p++)
    {
      curLayer = app.project.activeItem.selectedLayers[p];

      for (var i = 0, iLen = curLayer.selectedProperties.length; i < iLen; i++)
      {
        curProp = curLayer.selectedProperties[i];

        if (!curProp.hasOwnProperty('canAddProperty'))
        {
          /*
          6212 - color propertyType
          6418 - color propertyValueType
          */
          if (curProp.propertyValueType === 6418)
          {
            if (curProp.expression.replace(/\s/g, '').length > 0)
            {
              var c = confirm('Expression found!\n\nLayer name: ' + curLayer.name +
              '\nLayer index: ' + curLayer.index +
              String(curProp.parentProperty.isEffect ? '\nEffect: ' : '\nProperty Group: ') + curProp.parentProperty.name +
              String(curProp.parentProperty.isEffect ? '\nEffect Index: ' : '\nProperty Group Index: ') + curProp.parentProperty.propertyIndex +
              '\nProperty: ' + curProp.name +
              '\n\nAre you sure you want to change this color value?');
              if (c)
              {
                if (curProp.numKeys > 0) curProp.setValueAtTime(app.project.activeItem.time, rgba);
                else curProp.setValue(rgba);
              }
            }
            else
            {
              if (curProp.numKeys > 0) curProp.setValueAtTime(app.project.activeItem.time, rgba);
              else curProp.setValue(rgba);
            }
          }
        }
      }
    }
  }
};

aecolorpicker.prototype.run = function()
{
  var
  __self = this,
  ui = __self.ui(__self.root);

  ui.btnColor.helpTip = 'Set the color value of the selected properties.';

  // Set the default button color
  ui.btnColor.fillBrush = ui.btnColor.graphics.newBrush(ui.btnColor.graphics.BrushType.SOLID_COLOR, [0.3, 0.3, 0.3, 1]);

  ui.btnColor.onDraw = function ()
  {
    this.graphics.rectPath(0, 0, this.size[0], this.size[1]);
    this.graphics.fillPath(this.fillBrush);
  };
  ui.btnColor.onClick = function ()
  {
    var userColorAdobeRGBA = __self.colorUtils.CP2AdobeRGBA($.colorPicker());

    this.fillBrush = this.graphics.newBrush(this.graphics.BrushType.SOLID_COLOR, userColorAdobeRGBA);
    this.notify("onDraw");
    __self.setSelPropsColorValue(userColorAdobeRGBA);
  };
  ui.btnColor.addEventListener('mouseover', function ()
  {
    var curColor = __self.getFirstSelPropColorValue() || this.fillBrush.color;

    this.fillBrush = this.graphics.newBrush(this.graphics.BrushType.SOLID_COLOR, __self.colorUtils.HEX2AdobeRGBA(__self.colorUtils.luminance(__self.colorUtils.AdobeRGBA2HEX(curColor), 0.2)));
    this.notify("onDraw");
  });
  ui.btnColor.addEventListener('mouseout', function ()
  {
    var curColor = __self.getFirstSelPropColorValue() || this.fillBrush.color;

    this.fillBrush = this.graphics.newBrush(this.graphics.BrushType.SOLID_COLOR, __self.colorUtils.HEX2AdobeRGBA(__self.colorUtils.luminance(__self.colorUtils.AdobeRGBA2HEX(curColor), -0.2)));
    this.notify("onDraw");
  });
};

var aecp = new aecolorpicker();

aecp.run();
“严格使用”;
函数aecolorpicker(){}
aecolorpicker.prototype.root=此;
aecolorpicker.prototype.ui=函数(thisObj)
{
var-win={};
win.pal=thisObj面板实例?thisObj:new Window('palete','AE color Picker',未定义,{resizeable:true});
如果(win.pal==null)返回win.pal;
var res=“组{orientation:'column',对齐:['fill','fill'],首选大小:[128,64],边距:0,间距:0\
BTN颜色:按钮{size:[100,50],对齐:['fill','fill'],属性:{style:'toolbutton'}\
}";
win.ui=win.pal.add(res);
win.btnColor=win.ui.btnColor;
win.pal.layout.layout(true);
win.pal.onresizeing=win.pal.onResize=function()
{
this.layout.resize();
};
if(win.pal!==null&&win.pal窗口实例)
{
win.pal.show();
}
回归胜利;
};
aecolorpicker.prototype.colorUtils={
“AdobeRGBA2HEX”:函数(rgba)
{
var pad2=函数(c)
{
返回c.length==1?'0'+c:''+c;
};
var=函数(d)
{
返回Math.round(parseFloat(d)*255).toString(16);
};
十六进制变量=
[
pad2(Math.round(rgba[0]*255).toString(16)),
pad2(数学四舍五入(rgba[1]*255).toString(16)),
pad2(数学四舍五入(rgba[2]*255).toString(16))
];
返回“#”+hex.join(“”).toUpperCase();
},
“HEX2AdobeRGBA”:函数(十六进制)
{
如果(十六进制字符(0)='#')十六进制=十六进制子字符(1);
如果((hex.length<2)|(hex.length>6))返回null;
变量
值=十六进制分割(“”),
R
G
B
如果(十六进制长度===2)
{
r=parseInt(值[0].toString()+值[1].toString(),16);
g=r;
b=r;
}
否则如果(十六进制长度===3)
{
r=parseInt(值[0].toString()+值[0].toString(),16);
g=parseInt(值[1].toString()+值[1].toString(),16);
b=parseInt(值[2].toString()+值[2].toString(),16);
}
否则如果(十六进制长度===6)
{
r=parseInt(值[0].toString()+值[1].toString(),16);
g=parseInt(值[2].toString()+值[3].toString(),16);
b=parseInt(值[4].toString()+值[5].toString(),16);
}
其他的
{
返回null;
}
返回[
r/255,
g/255,
b/255,
1.
];
},
“CP2AdobeRGBA”:功能(cpdec)
{
返回[
(parseInt(cpdec.toString(16),16)>>16)/255,
(parseInt(cpdec.toString(16,16)>>8&0xFF)/255,
(parseInt(cpdec.toString(16,16)和0xFF)/255,
1.
];
},
“亮度”:功能(十六进制,亮度)
{
//验证十六进制字符串
十六进制=字符串(十六进制)。替换(/[^0-9a-f]/gi');
如果(十六进制长度<6)十六进制=十六进制[0]+十六进制[0]+十六进制[1]+十六进制[1]+十六进制[2]+十六进制[2];
lum=lum | | 0;
var result=“#”,c,i;
对于(i=0;i<3;i++)
{
c=parseInt(十六进制substr(i*2,2),16);
c=Math.round(Math.min(Math.max(0,c+(c*lum)),255)).toString(16);
结果+=('00'+c).substr(c.长度);
}
返回结果;
}
};
aecolorpicker.prototype.getFirstSelPropColorValue=函数()
{
if(CompItem的app.project.activeItem实例)
{
var-curLayer,curProp;
如果(app.project.activeItem.selectedLayers.length>0)
{
curLayer=app.project.activeItem.selectedLayers[0];
如果(curLayer.selectedProperties.length>0)
{
如果(!curLayer.selectedProperties[0].hasOwnProperty('canAddProperty'))curProp=curLayer.selectedProperties[0];
否则,如果(curLayer.selectedProperties.length>1)curProp=curLayer.selectedProperties[1];
否则返回null;
/*
6212-颜色属性类型
6418-颜色属性值类型
*/
if(curProp.propertyValueType==6418)返回curProp.valueAtTime(app.project.activeItem.time,false);
'use strict';

function aecolorpicker () {}

aecolorpicker.prototype.root = this;

aecolorpicker.prototype.ui = function (thisObj)
{
  var win = {};

  win.pal = thisObj instanceof Panel ? thisObj : new Window('palette', 'AE Colour Picker', undefined, {resizeable: true});

  if (win.pal === null) return win.pal;

  var res = "Group {orientation: 'column', alignment: ['fill', 'fill'], preferredSize: [128, 64], margin: 0, spacing: 0, \
    btnColor: Button {size:[100, 50], alignment: ['fill', 'fill'], properties: {style: 'toolbutton'}}\
  }";

  win.ui = win.pal.add(res);
  win.btnColor = win.ui.btnColor;

  win.pal.layout.layout(true);

  win.pal.onResizing = win.pal.onResize = function ()
  {
    this.layout.resize();
  };

  if (win.pal !== null && win.pal instanceof Window)
  {
    win.pal.show();
  }

  return win;
};

aecolorpicker.prototype.colorUtils = {
  "AdobeRGBA2HEX": function (rgba)
  {
    var pad2 = function (c)
    {
      return c.length == 1 ? '0' + c : '' + c;
    };

    var convertDecimalToHex = function (d)
    {
      return Math.round(parseFloat(d) * 255).toString(16);
    };

    var hex =
    [
      pad2(Math.round(rgba[0] * 255).toString(16)),
      pad2(Math.round(rgba[1] * 255).toString(16)),
      pad2(Math.round(rgba[2] * 255).toString(16))
    ];

    return '#' + hex.join('').toUpperCase();
  },

  "HEX2AdobeRGBA": function (hex)
  {
    if (hex.charAt(0) === '#') hex = hex.substr(1);

    if ((hex.length < 2) || (hex.length > 6)) return null;

    var
    values = hex.split(''),
    r,
    g,
    b;

    if (hex.length === 2)
    {
      r = parseInt(values[0].toString() + values[1].toString(), 16);
      g = r;
      b = r;
    }
    else if (hex.length === 3)
    {
      r = parseInt(values[0].toString() + values[0].toString(), 16);
      g = parseInt(values[1].toString() + values[1].toString(), 16);
      b = parseInt(values[2].toString() + values[2].toString(), 16);
    }
    else if (hex.length === 6)
    {
      r = parseInt(values[0].toString() + values[1].toString(), 16);
      g = parseInt(values[2].toString() + values[3].toString(), 16);
      b = parseInt(values[4].toString() + values[5].toString(), 16);
    }
    else
    {
      return null;
    }

    return [
      r / 255,
      g / 255,
      b / 255,
      1
    ];
  },

  "CP2AdobeRGBA": function (cpdec)
  {
    return [
      (parseInt(cpdec.toString(16), 16) >> 16) / 255,
      (parseInt(cpdec.toString(16), 16) >> 8 & 0xFF) / 255,
      (parseInt(cpdec.toString(16), 16) & 0xFF) / 255,
      1
    ];
  },

  "luminance": function (hex, lum)
  {
    // validate hex string
    hex = String(hex).replace(/[^0-9a-f]/gi, '');
    if (hex.length < 6) hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];

    lum = lum || 0;

    var result = "#", c, i;

    for (i = 0; i < 3; i++)
    {
      c = parseInt(hex.substr(i * 2, 2), 16);
      c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
      result += ('00' + c).substr(c.length);
    }

    return result;
  }
};

aecolorpicker.prototype.getFirstSelPropColorValue = function ()
{
  if (app.project.activeItem instanceof CompItem)
  {
    var curLayer, curProp;

    if (app.project.activeItem.selectedLayers.length > 0)
    {
      curLayer = app.project.activeItem.selectedLayers[0];

      if (curLayer.selectedProperties.length > 0)
      {
        if (!curLayer.selectedProperties[0].hasOwnProperty('canAddProperty')) curProp = curLayer.selectedProperties[0];
        else if (curLayer.selectedProperties.length > 1) curProp = curLayer.selectedProperties[1];
        else return null;

        /*
        6212 - color propertyType
        6418 - color propertyValueType
        */
        if (curProp.propertyValueType === 6418) return curProp.valueAtTime(app.project.activeItem.time, false);
      }
    }
  }

  return null;
};

aecolorpicker.prototype.setSelPropsColorValue = function (rgba)
{
  if (app.project.activeItem instanceof CompItem)
  {
    var curLayer, curProp;

    for (var p = 0, pLen = app.project.activeItem.selectedLayers.length; p < pLen; p++)
    {
      curLayer = app.project.activeItem.selectedLayers[p];

      for (var i = 0, iLen = curLayer.selectedProperties.length; i < iLen; i++)
      {
        curProp = curLayer.selectedProperties[i];

        if (!curProp.hasOwnProperty('canAddProperty'))
        {
          /*
          6212 - color propertyType
          6418 - color propertyValueType
          */
          if (curProp.propertyValueType === 6418)
          {
            if (curProp.expression.replace(/\s/g, '').length > 0)
            {
              var c = confirm('Expression found!\n\nLayer name: ' + curLayer.name +
              '\nLayer index: ' + curLayer.index +
              String(curProp.parentProperty.isEffect ? '\nEffect: ' : '\nProperty Group: ') + curProp.parentProperty.name +
              String(curProp.parentProperty.isEffect ? '\nEffect Index: ' : '\nProperty Group Index: ') + curProp.parentProperty.propertyIndex +
              '\nProperty: ' + curProp.name +
              '\n\nAre you sure you want to change this color value?');
              if (c)
              {
                if (curProp.numKeys > 0) curProp.setValueAtTime(app.project.activeItem.time, rgba);
                else curProp.setValue(rgba);
              }
            }
            else
            {
              if (curProp.numKeys > 0) curProp.setValueAtTime(app.project.activeItem.time, rgba);
              else curProp.setValue(rgba);
            }
          }
        }
      }
    }
  }
};

aecolorpicker.prototype.run = function()
{
  var
  __self = this,
  ui = __self.ui(__self.root);

  ui.btnColor.helpTip = 'Set the color value of the selected properties.';

  // Set the default button color
  ui.btnColor.fillBrush = ui.btnColor.graphics.newBrush(ui.btnColor.graphics.BrushType.SOLID_COLOR, [0.3, 0.3, 0.3, 1]);

  ui.btnColor.onDraw = function ()
  {
    this.graphics.rectPath(0, 0, this.size[0], this.size[1]);
    this.graphics.fillPath(this.fillBrush);
  };
  ui.btnColor.onClick = function ()
  {
    var userColorAdobeRGBA = __self.colorUtils.CP2AdobeRGBA($.colorPicker());

    this.fillBrush = this.graphics.newBrush(this.graphics.BrushType.SOLID_COLOR, userColorAdobeRGBA);
    this.notify("onDraw");
    __self.setSelPropsColorValue(userColorAdobeRGBA);
  };
  ui.btnColor.addEventListener('mouseover', function ()
  {
    var curColor = __self.getFirstSelPropColorValue() || this.fillBrush.color;

    this.fillBrush = this.graphics.newBrush(this.graphics.BrushType.SOLID_COLOR, __self.colorUtils.HEX2AdobeRGBA(__self.colorUtils.luminance(__self.colorUtils.AdobeRGBA2HEX(curColor), 0.2)));
    this.notify("onDraw");
  });
  ui.btnColor.addEventListener('mouseout', function ()
  {
    var curColor = __self.getFirstSelPropColorValue() || this.fillBrush.color;

    this.fillBrush = this.graphics.newBrush(this.graphics.BrushType.SOLID_COLOR, __self.colorUtils.HEX2AdobeRGBA(__self.colorUtils.luminance(__self.colorUtils.AdobeRGBA2HEX(curColor), -0.2)));
    this.notify("onDraw");
  });
};

var aecp = new aecolorpicker();

aecp.run();