AE ENHANCERS

Expressions/Scripts/Presets
It is currently Sun May 19, 2013 8:14 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Randomize Masks Colors?
PostPosted: Fri May 18, 2012 8:07 pm 
Offline

Joined: Sun May 01, 2005 1:52 pm
Posts: 128
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.


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Tue May 22, 2012 12:39 pm 
Offline

Joined: Tue May 15, 2012 1:54 am
Posts: 7
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];


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Tue Jun 19, 2012 6:29 pm 
Offline

Joined: Sun May 01, 2005 1:52 pm
Posts: 128
Thanks for the code.

It works, but it only works on the last mask of the layer.

Is there a way I can get it to work on all masks of a layer?

Thanks


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Tue Jun 19, 2012 8:28 pm 
Offline

Joined: Fri Jul 23, 2010 11:49 pm
Posts: 29
Code:
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();


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Fri Jun 29, 2012 10:27 am 
Offline

Joined: Sun May 01, 2005 1:52 pm
Posts: 128
This script works great!
Thank you.

Can ask one more thing?
Is there a way to get it to make the colors as bright as possible?

I tried adding numbers and I can get brighter but not as bright as I'd like.

Thanks again.


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Fri Jun 29, 2012 1:33 pm 
Offline

Joined: Fri Jul 23, 2010 11:49 pm
Posts: 29
You can play around with the brightness and saturation levels:

Code:
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();


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Sat Jun 30, 2012 12:10 am 
Offline

Joined: Fri Jul 23, 2010 11:49 pm
Posts: 29
Here's another variation using a spectrum of colors instead of completely random colors:

Code:
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();


Top
 Profile  
 
 Post subject: Re: Randomize Masks Colors?
PostPosted: Wed Jul 11, 2012 5:56 pm 
Offline

Joined: Sun May 01, 2005 1:52 pm
Posts: 128
That works perfectly!

Thanks! You rock!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group