[RESOLVED] Color() doesn't seem to be working with variables.

edited March 2018 in Questions about Code
void setup()
{
  size(1200, 600);
  noStroke();
}

float ballY = height/2;
float ballX = width/2;
float scalar;
color ballColor;
int direction = 1;
float colorInt = 0;

void draw()
{
  background(100);
  ballColor = color(sin(colorInt-(cos(colorInt+2))), sin(((6*colorInt)/5)+3), sin((colorInt*2)+1));  
  colorInt += .01;
  fill(ballColor);
  ellipse(ballX, ballY, 150, 150); 
  //bounce();
}

Hello everyone.

When I run this code, the ellipse is just straight black, instead of flowing between colors. Am I missing something?

Thanks, Ethan

P.S. Even sin(colorInt) in each part of the color() displays black. Also, assigning ballY and ballX to be height/2 and width/2 respectively doesn't seem to work correctly either.

I forgot to multiply my sin values by 255, so the color values were oscillating between -1 and 1... I feel stupid now.

Answers

  • Answer ✓

    use println to debug. for instance

    println(
      sin(colorInt-(cos(colorInt+2))), 
      sin(((6*colorInt)/5)+3), 
      sin((colorInt*2)+1)
    );
    

    and you'll see that sin returns a value between -1 and 1

    try multiplying each of the terms by 255...

  • Answer ✓

    Also, assigning ballY and ballX to be height/2 and width/2 respectively doesn't seem to work correctly either.

    width and height aren't defined until after you've called size()...

  • Answer ✓

    multiplying by 255 gives you things in the range -255 to +255, which isn't ideal

  • Thanks, koogs. I figured that out just a few minutes ago. I'm kicking myself now, lol. I've also changed the code to add 1 to each value before multiplying by 127.5 instead, so it wont be -255 to 255 anymore. Thanks for the tips.

  • @zQuax -- you may also be interested in using the built-in map() to scale outputs.

    You can also make an input range 0-1 with norm().

    Or, if the range is 0-1 already, you can interpolate values with lerp().

Sign In or Register to comment.