Would it be possible to make a randomize masks colors script?
I know you can set the prefs to do this but it uses the colors from the labels which I don't want to do.
Randomize Masks Colors?
Moderator: byronnash
Hi,
var curItem = app.project.activeItem; // current composition
var curLayer = curItem.selectedLayers[0]; // current layer
var num = curLayer.Masks.numProperties; // number of mask on the current layer
var colorR = Math.round( Math.random()*254);
var colorV = Math.round( Math.random()*254);
var colorB = Math.round( Math.random()*254);
curLayer.property("Masks").property(num).color = [colorR/255,colorV/255,colorB/255];
var curItem = app.project.activeItem; // current composition
var curLayer = curItem.selectedLayers[0]; // current layer
var num = curLayer.Masks.numProperties; // number of mask on the current layer
var colorR = Math.round( Math.random()*254);
var colorV = Math.round( Math.random()*254);
var colorB = Math.round( Math.random()*254);
curLayer.property("Masks").property(num).color = [colorR/255,colorV/255,colorB/255];
Code: Select all
function changeMaskColors() {
app.beginUndoGroup ( "Change Mask Colors");
if (!app.project || !(app.project.activeItem instanceof CompItem) || app.project.activeItem.selectedLayers < 1) {
alert ("Please select at least one layer");
return;
}
var curItem = app.project.activeItem; // current composition
var curLayers = curItem.selectedLayers; // currently selected layers
var curLayer;
var numMasks;
var colorR, colorG, colorB;
for (i = 0; i < curLayers.length; i++) {
curLayer = curLayers[i];
numMasks = curLayer.Masks.numProperties; // number of masks on the current layer
for (j = 1; j <= numMasks; j++) {
colorR = Math.round( Math.random()*254);
colorG = Math.round( Math.random()*254);
colorB = Math.round( Math.random()*254);
curLayer.property("Masks").property(j).color = [colorR/255,colorG/255,colorB/255];
}
}
app.endUndoGroup ();
}
changeMaskColors();
You can play around with the brightness and saturation levels:
Code: Select all
function rgbToHsv(r, g, b){
r = r/255, g = g/255, b = b/255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, v = max;
var d = max - min;
s = max == 0 ? 0 : d / max;
if(max == min){
h = 0; // achromatic
}else{
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, v];
}
function changeMaskColors() {
app.beginUndoGroup ( "Change Mask Colors");
if (!app.project || !(app.project.activeItem instanceof CompItem) || app.project.activeItem.selectedLayers < 1) {
alert ("Please select at least one layer");
return;
}
var curItem = app.project.activeItem; // current composition
var curLayers = curItem.selectedLayers; // currently selected layers
var curLayer;
var numMasks;
var colorR, colorG, colorB;
var saturation = .7; // set values 0 to 1
var brightness = .7; // set values .01 to .99
var myHsl = [];
var maskColor;
var maskHue = [0,0,0];
if (saturation < 0 || saturation > 1) {
alert ("Invalid saturation number");
return;
}
if (brightness <= 0 || brightness >= 1) {
alert ("Invalid brightness number");
return;
}
for (i = 0; i < curLayers.length; i++) {
curLayer = curLayers[i];
numMasks = curLayer.Masks.numProperties; // number of masks on the current layer
for (j = 1; j <= numMasks; j++) {
if (j > 1) {
maskColor = curLayer.property("Masks").property(j-1).color;
maskHue = rgbToHsl(maskColor[0], maskColor[1], maskColor[2]);
}
colorR = colorG = colorB = 0;
myHsl = [0,0,0];
while (myHsl[2] < brightness || myHsl[1] < saturation || Math.abs(myHsl[0] - maskHue[0]) < 1/numMasks ) {
colorR = Math.round( Math.random()*254);
colorG = Math.round( Math.random()*254);
colorB = Math.round( Math.random()*254);
myHsl = rgbToHsv (colorR, colorG, colorB);
}
curLayer.property("Masks").property(j).color = [colorR/255,colorG/255,colorB/255];
}
}
app.endUndoGroup ();
}
changeMaskColors();
Here's another variation using a spectrum of colors instead of completely random colors:
Code: Select all
function hsvToRgb(h, s, v){
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch(i % 6){
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [r, g, b];
}
function rainbowMasks() {
app.beginUndoGroup ( "Rainbow Mask Colors");
if (!app.project || !(app.project.activeItem instanceof CompItem) || app.project.activeItem.selectedLayers < 1) {
alert ("Please select at least one layer");
return;
}
var curItem = app.project.activeItem; // current composition
var curLayers = curItem.selectedLayers; // currently selected layers
var curLayer;
var numMasks;
var color;
var minSat = .7; // set this to the lowest saturation you would like
var minVal = .7; // set this to the lowest brightness you would like
var rainbow = false; // set this to true for a pure color spectrum, set this to false for more variation
var startingHue;
var maskHue, maskSat, maskVal;
if (minSat < 0 || minSat > 1) {
alert ("Invalid minSat color");
return;
}
if (minVal < 0 || minVal > 1) {
alert ("Invalid minVal color");
return;
}
for (i = 0; i < curLayers.length; i++) {
startingHue = Math.random();
curLayer = curLayers[i];
numMasks = curLayer.Masks.numProperties; // number of masks on the current layer
for (j = 1; j <= numMasks; j++) {
maskSat = maskVal = 0;
maskHue = (j-1)/numMasks + startingHue;
if (maskHue > 1) maskHue -= 1;
if (!rainbow) {
while (maskVal < minVal || maskSat < minSat) {
maskVal = Math.random();
maskSat = Math.random();
}
} else {
maskVal = 1;
maskSat = 1;
}
color = hsvToRgb(maskHue, maskSat, maskVal);
curLayer.property("Masks").property(j).color = color;
}
}
app.endUndoGroup ();
}
rainbowMasks();