Advertisement
JoshuaDavis

p5js + lerp + 3 different color tempos

Mar 21st, 2023 (edited)
1,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let stageW     = 900;
  2. let stageH     = 900;
  3. let numAssets  = 5;
  4.  
  5. let paramsClrs = [];
  6. let paramsClrsProbability;
  7. let paramsClrsMax = [100, 1000, 4000];
  8.  
  9. let clrs = [
  10.     ['#ffffff', '#af939f', '#5b274b', '#066505', '#b5e655'],
  11.     ['#ffffff', '#8f7788', '#5b274b', '#412049', '#245e79'],
  12.     ['#af939f', '#8f7788', '#5b274b', '#e7070e', '#ff530d']
  13. ];
  14.  
  15. function buildColors(_i, _whichClr, _paramsClrsProbability) {
  16.     let whichClr      = clrs[_whichClr];
  17.     let whichClrLen   = whichClr.length;
  18.     let clrCnt        = -1;
  19.     let clrMaxPerLerp = floor( _paramsClrsProbability / whichClrLen );
  20.  
  21.     for (let j = 0; j < _paramsClrsProbability; ++j) {
  22.         if( j%clrMaxPerLerp==0 ) {
  23.             clrCnt = (clrCnt+1)%whichClrLen;
  24.         }
  25.         let c1 = color( whichClr[clrCnt] );
  26.         let c2 = color( whichClr[(clrCnt+1)%whichClrLen] );
  27.         paramsClrs[_i].push(  lerpColor( c1, c2, map(j, (clrCnt*clrMaxPerLerp), (((clrCnt+1))*clrMaxPerLerp), 0.0, 1.0) ) );
  28.     }
  29. }
  30.  
  31. function preload() {
  32.     for (let i = 0; i < numAssets; ++i) {
  33.  
  34.         paramsClrs[i] = [];
  35.         let _tempProb = random(1);
  36.  
  37.         if      (_tempProb  < 0.60) paramsClrsProbability = paramsClrsMax[2]; // get 4000 colors / 60% of the time
  38.         else if (_tempProb  < 0.80) paramsClrsProbability = paramsClrsMax[1]; // get 1000 colors / 20% of the time
  39.         else                        paramsClrsProbability = paramsClrsMax[0]; // get 100 colors  / 20% of the time
  40.  
  41.         buildColors(i, int(random(clrs.length)), paramsClrsProbability);
  42.     }
  43. }
  44.  
  45. function setup() {
  46.     myCanvas = createCanvas(stageW, stageH);
  47.     print (paramsClrs);
  48. }
  49.  
  50. function draw() {
  51.  
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement