Question

I am trying to write a method of following logic:

I've got 3 integers, r, g and b.

at the beginning:

r = 255;
g = 0;
b = 0;

now as you can see, r is 255, now g should rise one by one until it reaches 255, too

r = 255;
g = 255;
b = 0;

All three integers together asseble a color. First one was red, now it's yellow. Not it should turn green, so r should decrease until only g is 255:

r = 0;
g = 255;
b = 0;

Next color should be cyan, and so on.

This is the order:

red - yellow - green - cyan - blue - violet - red 

--> and from beginning.

I've tried to achieve this by using if-methods:

e.g.: if(r == 255 && g == 0) g++;

etc. , but i realized this is going to be long and complicated.

Does anybody has another idea of how to cycle through colors?

My goal is to change the color of a square object on each update:

public void update() {
    -->color change code here<--
    color = new Color(r, g, b, alpha);
}

so every time the update method is called (all 5 ms), the code gets called.

Any ideas?

Was it helpful?

Solution

java.awt.Color class provides a static function

getHSBColor(float h, float s, float b)

Use the HSB color space and make the hue component go from 0 to 1 while keeping the other components constant.

color = Color.getHSBColor(hue, 1, 1);

OTHER TIPS

According to your cycle you have 6 transitions made by 256 steps each. Each transition works on all three RGB channels but in a shifted way.

For example, assuming that you start from 255,0,0 you will have something like:

R 255 --- \             / ---
           \           /
  0         \ --- --- /

G 255   / --- --- \
       /           \
  0   /             \ --- ---

B 255           / --- --- \
               /           \
  0   --- --- /             \

     R   Y   G   C   B   M   R..

from this you can easily see that the whole period is 6 fragments x 256 steps. At each fragment every channel could be steady, rising or falling. At the same time you can see that green and blue channels are just as red channel just shifted.

So we can focus only on the red channel:

int redValueForStep(int step) {
  step %= 256*6; // let's make step wrap around
  int fragment = step/256; // which fragment 0-5
  int fragmentStep = step%256; // which step inside fragment 0-255

  switch (fragment) {
    case 0: case 5: 
      return 255; // first and last fragment have steady high value
    case 2: case 3:
      return 0; // fragments 2 and 3 have steady low value
    case 1: return 255-fragmentStep; // falling
    case 2: return fragmentStep; // rising
}

Once you have red channel you can easily compute the other, the green channel is red shifted by two whole fragments ahead, so you add 4 to wrap around (to avoid having negative values):

int greenValueForStep(int step) { return redValueForStep(step+256*4); }

Same thing is applicable to blue channel.

Here's an example for the changes you showed using for loops, hope this helps!

public class rgb {
    public static void main(String[] args) {
        // establish your starting conditions
        int r, g, b;
        r = 255;
        g = 0;
        b = 0;

        // increment g by 1 until it reaches 255
        for (int i = 0; i <= 255; i++) {
            g = i;
            /* Do whatever you
             * want with this
             * color here */
        }

        // de-increment r by 1 until it's 0
        for (int i = 254; i >= 0; i--) {
            r = i;
            /* Do whatever you
             * want with this
             * color here */
        }
    }
}
for (int red=0; red<256; red++)
    for (int green=0; green<256; green++)
        for (int red=0; red<256; red++) {
        // output of every combination of all red, green and blue
        }

Or if you just wanted values of 0 and 255, just make them count to 1 then multiply by 255 into another variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top