jMonkeyEngine – Setting a Custom RGBA Based on Hex Color Codes

As I progress through a project I am working on with jMonkeyEngine (which is really cool by the way … jMonkeyEngine, that is), I came across the need to use a custom color for my project.

So I quickly went into the API, found the ColorRGBA class, and went from there. I am working with a color generator (ColorSchemeDesigner) to help me pick out colors which work well together. The colors are given to me as the RGBA color codes, used mostly for web design. These codes include hex values to keep each R, G, B, and A values within two digits.

Now, to create a new color based on this color code in jMonkey’s ColorRGBA class, you need to convert any hex values to floats, and then clamp them. I figured this out after a few minutes of hacking around, and thought I’d share my quick little experience. So say you have a color code FFC040.

FF, in decimal, is 255 = R
C0, in decimal, is 192 = G
40 = B
Alpha can be set to any value you want between 0 and 255 = A

For the purposes of our work in jMonkey, the RGBA color space becomes (255, 192, 40, 255) – I chose 255 for Alpha because I want a fully opaque rendering.

Now you just have to clamp the values. All that means is dividing each value by 255.

So to create a new RGBA color in jMonkey, simply write:

ColorRGBA myColor = new ColorRGBA(1f, .75f, .16f, 1f);

And everything should work just right!

P.S. If you don’t know how to convert from hex to dec, just search for a converter online.

Posted in Mini Tuts

Leave a Reply

Your email address will not be published. Required fields are marked *