JoshuaDavis

using lerpColor() for color tables

Jul 21st, 2021 (edited)
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. // this sketch is for plain ole Processing (JAVA) - which was then ported to P5JS (JavaScript)
  2.  
  3. // I should note that it creates a perfect animated color loop...
  4.  
  5. // gray blends to light gray / 0 -> 1
  6. // light gray blends to red  / 1 -> 2
  7. // red blends to purple      / 2 -> 3
  8. // purple blends to red      / 3 -> 4
  9. // red blends BACK to gray   / 4 -> 0
  10.  
  11. int       stageW  = 1000;
  12. int       stageH  = 1000;
  13. color     clrBG   = #ECECEC;
  14.  
  15. //                  gray             light gray     red          purple        red
  16. color[][] clr1    = { {102,102,102}, {153,153,153}, {236,31,39}, {102,47,144}, {236,31,39} };
  17. int       clr1Len = clr1.length;
  18.  
  19. int       clr1Num = 300;
  20. int       clr1Cnt = -1;
  21. int       clr1Blk = floor(clr1Num/clr1Len);
  22.  
  23. int       clr2Num = 900;
  24. int       clr2Cnt = -1;
  25. int       clr2Blk = floor(clr2Num/clr1Len);
  26.  
  27. color[]   clr1A   = new color[clr1Num];
  28. color[]   clr1B   = new color[clr2Num];
  29.  
  30. void settings() {
  31.     size(stageW, stageH, P3D);
  32. }
  33.  
  34. void setup() {
  35.     background(clrBG);
  36.  
  37.     for (int i = 0; i < clr1Num; ++i) {
  38.         if( i%clr1Blk==0 ) clr1Cnt = (clr1Cnt+1)%clr1Len;
  39.         color _c1 = color(clr1[(clr1Cnt)][0], clr1[(clr1Cnt)][1], clr1[(clr1Cnt)][2]);
  40.         color _c2 = color(clr1[(clr1Cnt+1)%clr1Len][0], clr1[(clr1Cnt+1)%clr1Len][1], clr1[(clr1Cnt+1)%clr1Len][2]);
  41.         clr1A[i] = lerpColor( _c1, _c2, map(i, (clr1Cnt*clr1Blk), (((clr1Cnt+1))*clr1Blk), 0.0, 1.0) );
  42.     }
  43.  
  44.     for (int i = 0; i < clr2Num; ++i) {
  45.         if( i%clr2Blk==0 ) clr2Cnt = (clr2Cnt+1)%clr1Len;
  46.         color _c1 = color(clr1[(clr2Cnt)][0], clr1[(clr2Cnt)][1], clr1[(clr2Cnt)][2]);
  47.         color _c2 = color(clr1[(clr2Cnt+1)%clr1Len][0], clr1[(clr2Cnt+1)%clr1Len][1], clr1[(clr2Cnt+1)%clr1Len][2]);
  48.         clr1B[i] = lerpColor( _c1, _c2, map(i, (clr2Cnt*clr2Blk), (((clr2Cnt+1))*clr2Blk), 0.0, 1.0) );
  49.     }
  50.  
  51. }
  52.  
  53. void draw() {
  54.     background( clr1B[(floor(frameCount*0.5))%clr2Num] );
  55.  
  56.     strokeWeight(0);
  57.     noStroke();
  58.  
  59.     // 300 colors
  60.  
  61.     for (int i = 0; i < clr1Num; ++i) {
  62.         fill( clr1A[i] );
  63.         rect(5+(i*1), 0, 1, 100);
  64.     }
  65.  
  66.     for (int i = 0; i < clr1Num; ++i) {
  67.         fill( clr1A[(i+(floor(frameCount*3)))%clr1Num] );
  68.         rect(5+(i*1), 105, 1, 100);
  69.     }
  70.  
  71.     // 900 colors
  72.  
  73.     for (int i = 0; i < clr2Num; ++i) {
  74.         fill( clr1B[i] );
  75.         rect(5+(i*1), 210, 1, 100);
  76.     }
  77.  
  78.     for (int i = 0; i < clr2Num; ++i) {
  79.         fill( clr1B[(i+(floor(frameCount*2)))%clr2Num] );
  80.         rect(5+(i*1), 315, 1, 100);
  81.     }
  82.  
  83.     // single squares with color flow
  84.  
  85.     fill( clr1A[(floor(frameCount*5))%clr1Num] );
  86.     rect(5, 425, 445, 480);
  87.  
  88.     fill( clr1B[(floor(frameCount*2))%clr2Num] );
  89.     rect(460, 425, 445, 480);
  90. }
  91.  
  92.  
  93.  
Add Comment
Please, Sign In to add comment