Hello, I’ve decided to post a snippet of a small algorithm I’ve come up with for looping through the spectrum of RGBA colours. I needed this as an exercise for myself (quite new to game development) and this is how I came up with a way to get my cube to keep changing colours.
It probably isn’t the most efficient or “correct” way, but it works and it might inspire somebody.
1) Declare global variables r, g, b and initialize one of them to your preferred starting colour. Haven’t tested it for white or black, but should take a couple minutes to figure that out!
private float at r = 1, g = 0, b = 0;
2) In your simpleUpdate() method, compile the following algorithm:
if(b >= 1 && r < 1){ r += 0.01; g -= 0.01; } if(r >= 1 && g < 1){ g += 0.01; b -= 0.01; } if(g >= 1 && b < 1){ b += 0.01; r -= 0.01; }
Basically, if the cube is fully one colour, the program will slowly reduce that colour and increase the next colour. Changing the “0.01” values will speed up the changes.
3) Now just apply the variables to whatever you need to colour in your simpleUpdate() method!
cubeMat.setColor(“Color”, new ColorRGBA(r, g, b, 1));
Thanks for reading, good luck and I hope this helped someone out there