Today was not very about code so I almost haven't coded.
But I haven't shown everything about updating my Minecraft mods to the latest version. One of them is a client mod about displaying the key presses on the screen, someone requested it so I made it.
While updating the mod I also wanted to add a new feature which was rainbow text!
But I was a bit lost as I didn't know how to do that, there's multiple methods:
• combining the font texture with a rainbow texture in the OpenGL shader, but that was unpractical with the mess which is Minecraft rendering (and I don't know enough about it) and making a "moving" texture would be hard with my limited knowledge about shaders.
• drawing every character a different color: that's possible, with the latest MC update as they dropped the immediate rendering (OpenGL 1.0) it's not a big deal to do that way as it won't destroy performance
So I chose the second method which is less smooth than the first one but it still gives a convincing effect.
How does it work?
Simple, when I want to draw a string, I'll use a special function if I want the rainbow effect, which will draw each character separately. To get the color I use a HSB to RGB method which allows me to make easy colors as it goes from 0.0 to 1.0 (from red to purple, like a rainbow but circular), and to make the color "move" I tied it to the current time in milliseconds and to make the color independent from each character I also tied the position of each character:
public static int getRainbowRGB(double x, double y)
{
float speed = 2600.0F;
return Color.HSBtoRGB((float) ((System.currentTimeMillis() - x * 10.0D - y * 10.0D) % speed) / speed,
(float) AuroraKeystrokes.get().config.getRainbowSaturation(),
0.9F);
}
A bit difficult to find it at first but when found it gives a cool effect :)