JoshuaDavis icon

p5js + lerp + 3 different color tempos

JoshuaDavis | PRO | 03/21/23 06:00:35 PM UTC (Edited) | 0 ⭐ | 1592 👁️ | Never ⏰ | []
JavaScript |

1.55 KB

|

None

|

0 👍

/

0 👎

let stageW     = 900;
let stageH     = 900;
let numAssets  = 5;
 
let paramsClrs = [];
let paramsClrsProbability;
let paramsClrsMax = [100, 1000, 4000];
 
let clrs = [
    ['#ffffff', '#af939f', '#5b274b', '#066505', '#b5e655'],
    ['#ffffff', '#8f7788', '#5b274b', '#412049', '#245e79'],
    ['#af939f', '#8f7788', '#5b274b', '#e7070e', '#ff530d']
];
 
function buildColors(_i, _whichClr, _paramsClrsProbability) {
    let whichClr      = clrs[_whichClr];
    let whichClrLen   = whichClr.length;
    let clrCnt        = -1;
    let clrMaxPerLerp = floor( _paramsClrsProbability / whichClrLen );
 
    for (let j = 0; j < _paramsClrsProbability; ++j) {
        if( j%clrMaxPerLerp==0 ) {
            clrCnt = (clrCnt+1)%whichClrLen;
        }
        let c1 = color( whichClr[clrCnt] );
        let c2 = color( whichClr[(clrCnt+1)%whichClrLen] );
        paramsClrs[_i].push(  lerpColor( c1, c2, map(j, (clrCnt*clrMaxPerLerp), (((clrCnt+1))*clrMaxPerLerp), 0.0, 1.0) ) );
    }
}
 
function preload() {
    for (let i = 0; i < numAssets; ++i) {
 
        paramsClrs[i] = [];
        let _tempProb = random(1);
 
        if      (_tempProb  < 0.60) paramsClrsProbability = paramsClrsMax[2]; // get 4000 colors / 60% of the time
        else if (_tempProb  < 0.80) paramsClrsProbability = paramsClrsMax[1]; // get 1000 colors / 20% of the time
        else                        paramsClrsProbability = paramsClrsMax[0]; // get 100 colors  / 20% of the time
 
        buildColors(i, int(random(clrs.length)), paramsClrsProbability);
    }
}
 
function setup() {
    myCanvas = createCanvas(stageW, stageH);
    print (paramsClrs);
}
 
function draw() {
 
}
 

Comments