JoshuaDavis

using lerpColor() for color tables / p5js

Jul 28th, 2021 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // this is for P5JS
  2.  
  3. // I should note that it creates a perfect animated color loop...
  4.  
  5. // clr1 array example
  6. // gray blends to light gray / 0 -> 1
  7. // light gray blends to red  / 1 -> 2
  8. // red blends to purple      / 2 -> 3
  9. // purple blends to red      / 3 -> 4
  10. // red blends BACK to gray   / 4 -> 0
  11.  
  12. let stageW       = 1080;
  13. let stageH       = 1080;
  14.  
  15. let clrLen;
  16. let whichClr;
  17.  
  18. let clr1=[ [102,102,102], [153,153,153], [236,31,39], [102,47,144], [236,31,39] ];
  19. let clr2=[ [51,51,51], [255,51,0], [255,255,255] ];
  20. let clr3=[ [255,0,0], [255,255,255], [0,255,0], [0,0,255] ];
  21.  
  22. let clr1Num = 300;
  23. let clr1Cnt = -1;
  24. let clr1Blk;
  25.  
  26. let clr2Num = 900;
  27. let clr2Cnt = -1;
  28. let clr2Blk;
  29.  
  30. let clrA = []; // store 300 color gradients
  31. let clrB = []; // store 900 color gradients
  32.  
  33. function setup() {
  34.     createCanvas(stageW, stageH, WEBGL);
  35.     background('#111111');
  36.  
  37.     let _r = int( random(3) );
  38.  
  39.     if(_r==0) {
  40.         whichClr = clr1;
  41.     }
  42.     else if(_r==1) {
  43.         whichClr = clr2;
  44.     }
  45.     else {
  46.         whichClr = clr3;
  47.     }
  48.    
  49.     setColorTables();
  50. }
  51.  
  52. function draw() {
  53.     background('#111111');
  54.  
  55.     strokeWeight(0);
  56.     noStroke();
  57.  
  58.     // 300 COLORS
  59.  
  60.     push();
  61.         translate( -((stageW/2)-20), -((stageH/2)-20) );
  62.         for (let i = 0; i < clr1Num; ++i) {
  63.             fill( clrA[i] );
  64.             rect(5+(i*1), 0, 1, 100);
  65.         }
  66.     pop();
  67.  
  68.     push();
  69.         translate( -((stageW/2)-20), -((stageH/2)-130) );
  70.         for (let i = 0; i < clr1Num; ++i) {
  71.             fill( clrA[ (i+(frameCount*5))%clrA.length ] );
  72.             rect(5+(i*1), 0, 1, 100);
  73.         }
  74.     pop();
  75.  
  76.     // 900 COLORS
  77.  
  78.     push();
  79.         translate( -((stageW/2)-20), -((stageH/2)-320) );
  80.         for (let i = 0; i < clr2Num; ++i) {
  81.             fill( clrB[i] );
  82.             rect(5+(i*1), 0, 1, 100);
  83.         }
  84.     pop();
  85.  
  86.     push();
  87.         translate( -((stageW/2)-20), -((stageH/2)-430) );
  88.         for (let i = 0; i < clr2Num; ++i) {
  89.             fill( clrB[ (i+(frameCount*2))%clrB.length ] );
  90.             rect(5+(i*1), 0, 1, 100);
  91.         }
  92.     pop();
  93. }
  94.  
  95. function setColorTables() {
  96.     clrLen = whichClr.length;
  97.     clr1Blk = floor(clr1Num/clrLen);
  98.     clr2Blk = floor(clr2Num/clrLen);
  99.     for (let i = 0; i < clr1Num; ++i) {
  100.         if( i%clr1Blk==0 ) clr1Cnt = (clr1Cnt+1)%clrLen;
  101.         let _c1 = color( whichClr[(clr1Cnt)][0], whichClr[(clr1Cnt)][1], whichClr[(clr1Cnt)][2] );
  102.         let _c2 = color(whichClr[(clr1Cnt+1)%clrLen][0], whichClr[(clr1Cnt+1)%clrLen][1], whichClr[(clr1Cnt+1)%clrLen][2]);
  103.         clrA.push(  lerpColor( _c1, _c2, map(i, (clr1Cnt*clr1Blk), (((clr1Cnt+1))*clr1Blk), 0.0, 1.0) ) );
  104.     }
  105.     for (let i = 0; i < clr2Num; ++i) {
  106.         if( i%clr2Blk==0 ) clr2Cnt = (clr2Cnt+1)%clrLen;
  107.         let _c1 = color(whichClr[(clr2Cnt)][0], whichClr[(clr2Cnt)][1], whichClr[(clr2Cnt)][2]);
  108.         let _c2 = color(whichClr[(clr2Cnt+1)%clrLen][0], whichClr[(clr2Cnt+1)%clrLen][1], whichClr[(clr2Cnt+1)%clrLen][2]);
  109.         clrB.push( lerpColor( _c1, _c2, map(i, (clr2Cnt*clr2Blk), (((clr2Cnt+1))*clr2Blk), 0.0, 1.0) ) );
  110.     }
  111. }
Add Comment
Please, Sign In to add comment