Loading...
Logo
Processing Forum
I have changed the example sketch of Linear Gradient, so the gradient moves.

Unfortunately, there are horizontal scan lines when I run it.  P2D improves it a little, yet I was hoping OPENGL would improve the appearance, but it does not work.  Just a blank grey screen.  I am guessing OPENGL does not handle the set() function?

OPENGL works fine on othe Processing sketches on my mac.

Here's the code:

Copy code
  1. import processing.opengl.*;

  2. int w, h;

  3. int Y_AXIS = 1;
  4. int X_AXIS = 2;

  5. int b1dir = 1;
  6. int b2dir = 1;

  7. float x1;
  8. float y1;
  9. int x1dir;
  10. int y1dir;

  11. color b1;
  12. color b2;
  13. color b3;

  14. void setup(){
  15.   w = screen.width;
  16.   h = screen.height;
  17.   size(w, h, OPENGL);
  18.   
  19.   //smooth();
  20.   
  21.   x1 = 0;
  22.   y1 = 600;
  23.   x1dir = 1;
  24.   y1dir = -1;
  25.   
  26.   b1 = color(69, 139, 116);
  27.   b2 = color(205, 38, 38);
  28.   b3 = color(0, 0, 0);
  29. }

  30. void draw() {
  31.   background(0);
  32.   
  33.   setGradient(0, int(y1), width, 100, b3, b1, Y_AXIS);
  34.   
  35.   setGradient(0, int(y1) + 100, width, 100, b1, b2, Y_AXIS);
  36.   
  37.   setGradient(0, int(y1) + 200, width, 100, b2, b3, Y_AXIS);
  38.   
  39.   y1 += y1dir * 1;
  40.   
  41.   if(y1 <= -200) {
  42.       y1 = 600;
  43.     }
  44. }

  45. void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ){
  46.   // calculate differences between color components 
  47.   float deltaR = red(c2)-red(c1);
  48.   float deltaG = green(c2)-green(c1);
  49.   float deltaB = blue(c2)-blue(c1);

  50.   // choose axis
  51.   if(axis == Y_AXIS){
  52.     /*nested for loops set pixels
  53.      in a basic table structure */
  54.     // column
  55.     for (int i=x; i<=(x+w); i++){
  56.       // row
  57.       for (int j = y; j<=(y+h); j++){
  58.         color c = color(
  59.         (red(c1)+(j-y)*(deltaR/h)),
  60.         (green(c1)+(j-y)*(deltaG/h)),
  61.         (blue(c1)+(j-y)*(deltaB/h)) 
  62.           );
  63.         set(i, j, c);
  64.       }
  65.     }  
  66.   }  
  67.   else if(axis == X_AXIS){
  68.     // column 
  69.     for (int i=y; i<=(y+h); i++){
  70.       // row
  71.       for (int j = x; j<=(x+w); j++){
  72.         color c = color(
  73.         (red(c1)+(j-x)*(deltaR/h)),
  74.         (green(c1)+(j-x)*(deltaG/h)),
  75.         (blue(c1)+(j-x)*(deltaB/h)) 
  76.           );
  77.         set(j, i, c);
  78.       }
  79.     }  
  80.   }
  81. }

Replies(2)

Yep, set() isn't supported in the OPENGL renderer. I'm guessing that the scanlines are there because the difference between each individual row of pixels is high enough to show a marked difference. Does increasing the width of the gradient fix the issue?
It did help, though the problem was not completely alleviated, still it was fine for my application.

Thanx!