generating similar colors

edited January 2016 in Questions about Code

Hi, what I want to do is having a color (let's say red) and automatically generate other colors which are different shades of red.. I have no idea where to start..do you know examples or particular algorithms that I should look at?

Basically I have this code..the position of the circle changes (x axis) randomly but is still floating around the center..I would like to apply this concept to colors..

    float xloc = (float)randomGaussian();

    int mean = width/2;
    int ds = 60;

    float x = ds * xloc + mean;


    fill(0,10);
    noStroke();
    ellipse(x, height/2,15,15);

thanks

Answers

  • Answer ✓

    See if this works for you

    long t=0;
    int r=180, g=200, b=140;
    float c1, c2, c3;
    
    void setup(){
      size(300,300);
    }
    
    void draw(){
      rectMode(CORNERS);
      fill(r,g,b);
      rect(50, 50, 250, 100);
    
      if (millis()-t>400){
        c1 = random(-20,20);
        c2 = random(-20,20);
        c3 = random(-20,20);
        t=millis();
      }
    
      fill(r+c1, g+c2, b+c3);
      rect(100, 200, 200, 250);
    }
    

    By changing the range of random(-20,20); you can control how close the resulting shade will be to the original color

  • Hi, thank you..actually I mixed my example with your code and using the randomGaussian seems to do the trick..what do you think about this solution?

        long t=0;
        int r=180, g=200, b=140;
        float c1, c2, c3;
    
        void setup(){
          size(300,300);
        }
    
        void draw(){
          rectMode(CORNERS);
          fill(r,g,b);
          rect(50, 50, 250, 100);
    
           float myG = (float) randomGaussian();
    
          if (millis()-t>100){
            c1 = 50*myG + r;
            c2 = 50*myG + g;
            c3 = 50*myG + b;
            t=millis();
          }
    
          fill(c1, c2, c3);
          rect(100, 200, 200, 250);
        }
    
  • It seems to work fine!

Sign In or Register to comment.