Calculate a speed of colour-shifting

Look at Reply #2 for an easier explanation!

public Background(Color cc, int aa, int tt) {
		c = cc;
		s = ???;
	}

cc is colour, aa is colour deviation and tt is time in seconds. The game updates 60 times/second.

For example, if I passed in Color.PINK, 50, 3 I would basically say this:
My background is in the colour pink by default.
Its colour is going to continuously shift shades.
The time for a shift from the darkest shade to the brightest is going to take 3 seconds.

How would I calculate the speed s with known deviation and time?

Well from that small info you give us, i would assume that you calculate the delta time between the game updates.

You could use this delta time to calculate the time for each color shift like this:


public void gameLoop() {
      // calculate the delta here
      shiftTime += delta // then add it up on a variable
      if(shiftTime >= 3) {
        // You do not need to make a new Background every time you shift colors
        // just make a method that shifts the color for your background.
        background.shiftColor(Color, deviation)

If you don’t calculate the delta time here is how you do that:


public void gameLoop() {
      long loopTime = System.nanoTime();
      long optimalLoopTime = 1000000000/60 // 60 is your target FPS
      while(running) {
            long now = System.nanoTime();
            long updateLength = now - loopTime;
            lastLoopTime = now;
            double delta = updateLength / ((double)OPTIMAL_TIME);
            // render your stuff
      }
}

Code taken from: Game Loops!

Excuse my messy describing, this is to clear things up:

public static void update() {
		boolean bool = false;
		r += s;
		if (r > cmax) {
			debug1 = System.currentTimeMillis();
			r = cmax;
			s = -s;
			bool = true;
		} else if (r < cmin) {
			debug2 = System.currentTimeMillis();
			r = cmin;
			s = -s;
			bool = true;
		}
		c = new Color((int) r,c.getGreen(),c.getBlue());
		if (bool) System.out.println(debug1-debug2);
	}

s is speed, c is colour, r is amount of red in the original colour
cmax & cmin make the interval r is allowed to be inside.

In the Background class, this method is called 60 times/second.

What I want is the console to print out when r has reached a peak from another.
I’m successful doing so, but I don’t know what the speed’s value is supposed to be.

Edit: A simpler way of saying this:
I want value r to increase s in the time t.
The update method increases r with s and is called 60 times/second.
What is the value of s?

I’m not really sure what you’re trying to accomplish; but best guess is that you are having trouble shifting smoothly from one value to another.

You should look into easing functions and linear interpolation. Easing functions look like this:

The code might look like this:

void update() {
    timer += delta;
    
    //easing start/end values
    float start = 1f; //starting brightness
    float end = 0.45f; //ending brightness
    
    //returns a value between start and end, based on time passed and easing function
    float value = (timer > duration) 
    			? end
				: Easing.LINEAR.ease(timer, start, (end-start), duration);
   	
   	//starting color
   	Color orig = Color.PINK;
   	
   	Color result = new Color((int)(orig.getRed() * val), (int)(orig.getGreen() * val),
   						(int)(orig.getBlue() * val), orig.getAlpha());
   	setColor(result);
   	draw( ... );
}

Linear interpolation is also useful to know; it’s a simple way of “mixing” two values based on an interpolation factor.
For example, here is a basic color lerp:

More info on lerp:

For more advanced color shifting you will probably want to work in HSL or HSV color space. That way, you adjust only one aspect (hue, saturation, or brightness). Then you convert to RGB before rendering.

All of this stuff will transfer nicely when/if you pick up GLSL.

Check Reply #2 if you want a more proper explanation of my problem.


Also see InterpolationTest for nice graphs of each interpolation.

Are you or are you not trying to animate one color to another? Just use the code I posted, or any other easing/interpolation utility.

Using a “speed” variable and a series of if-else statements is not the correct approach…

See Reply #2 for a much more narrowed down question. I simply want to make my value rise to a specific value over a specific time in seconds. It’s all about how to calculate the speed really.

With my posted example you can narrow down the time between the frames.

if shiftTime == 3, 3 seconds have passed. You then can call your update method inside or do whatever you want to. This way you get pretty good control of how often the update() happens, just alter the 3 to whatever you want. You then can alter your r value with a fixed amount, and just change the speed of the update() to happen :).

[quote]I simply want to make my value rise to a specific value over a specific time in seconds. It’s all about how to calculate the speed really.
[/quote]

  • face palm *

This is what my earlier posted code does.