A frequent top referral topic to this forum is this:
And whilst it’s trivial once you stop to think about it - I actually frequent the internet a lot to get color palettes - and they are often in various formats.
I suggest adding the following static methods to the ColorRGBA class to make life easier.
- Convert any valid hex code to ColorRGBA
- Convert any RGBA-255 color to ColorRGBA
- Provide an actual random color generator (the current one will return the same color in the same tick of time).
package com.jayfella.devkit.theme;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
public class ColorUtils {
/**
* Provides a ColorRGBA color from hexadecimal values.
* Input may be in 3,4,6 or 8 length format, with or without a hashtag.
* Examples are:
* "f2d" - single-digit rgb hex code (alpha will be 1.0)
* "f2df" - single-digit rgba hex code
* "ff22dd" - two-digit rgb hex code (alpha will be 1.0)
* "ff22ddff" - two-digit rgba hex code
*
* @param hexString the hex string to convert.
* @return the ColorRGBA equivalent of the hexadecimal color.
*/
public static ColorRGBA fromHex(String hexString) {
int startIndex = hexString.startsWith("#") ? 1 : 0;
boolean noAlpha = hexString.substring(startIndex).length() == 3 || hexString.substring(startIndex).length() == 6;
boolean singular = hexString.substring(startIndex).length() == 3 || hexString.substring(startIndex).length() == 4;
String rString = singular
? hexString.substring(startIndex, startIndex + 1) + hexString.substring(startIndex, startIndex + 1)
: hexString.substring(startIndex, startIndex + 2);
String gString = singular
? hexString.substring(startIndex + 1, startIndex + 2) + hexString.substring(startIndex + 1, startIndex + 2)
: hexString.substring(startIndex + 2, startIndex + 4);
String bString = singular
? hexString.substring(startIndex + 2, startIndex + 3) + hexString.substring(startIndex + 2, startIndex + 3)
: hexString.substring(startIndex + 4, startIndex + 6);
String aString = noAlpha
? "FF"
: singular
? hexString.substring(startIndex + 3, startIndex + 4) + hexString.substring(startIndex + 3, startIndex + 4)
: hexString.substring(startIndex + 6, startIndex + 8);
int r = Integer.parseInt(rString, 16);
int g = Integer.parseInt(gString, 16);
int b = Integer.parseInt(bString, 16);
int a = Integer.parseInt(aString, 16);
// System.out.println("INPUT: " + rString + ", " + gString + ", " + bString + ", " + aString);
// System.out.println("RGBA: " + r + ", " + g + ", " + b + ", " + a);
return fromRGBA255(r, g, b, a);
}
/**
* Converts a color from RGBA 255 values.
* @param r the red value in 0-255 range.
* @param g the green value in 0-255 range.
* @param b the blue value in 0-255 range.
* @param a the alpha value in 0-255 range.
* @return the ColorRGBA equivalent of the RGBA 255 color.
*/
public static ColorRGBA fromRGBA255(int r, int g, int b, int a) {
return new ColorRGBA(r / 255.0F, g / 255.0F, b / 255.0F, a / 255.0F);
}
/**
* Provides a random color with an alpha value of 1.0
* @return a random color.
*/
public static ColorRGBA random() {
return random(1.0f);
}
/**
* Provides a random color with the given alpha value.
* @param alpha the opacity value between 0.0 and 1.0
* @return a random color with the given alpha value.
*/
public static ColorRGBA random(float alpha) {
return new ColorRGBA(FastMath.nextRandomFloat(), FastMath.nextRandomFloat(), FastMath.nextRandomFloat(), alpha);
}
}
And a test case I wrote to make sure the hex converter works.
import com.jayfella.devkit.theme.ColorUtils;
import com.jme3.math.ColorRGBA;
public class TestColorConverter {
public static void main(String... args) {
// test colors
ColorRGBA test1 = ColorUtils.fromHex("ff22ddff");
ColorRGBA test3 = ColorUtils.fromHex("ff22dd");
ColorRGBA test2 = ColorUtils.fromHex("f2df");
ColorRGBA test4 = ColorUtils.fromHex("f2d");
ColorRGBA test5 = ColorUtils.fromHex("#ff22ddff");
ColorRGBA test6 = ColorUtils.fromHex("#ff22dd");
ColorRGBA test7 = ColorUtils.fromHex("#f2df");
ColorRGBA test8 = ColorUtils.fromHex("#f2d");
System.out.println("Test 1: " + test1);
System.out.println("Test 2: " + test2);
System.out.println("Test 3: " + test3);
System.out.println("Test 4: " + test4);
System.out.println("Test 5: " + test5);
System.out.println("Test 6: " + test6);
System.out.println("Test 7: " + test7);
System.out.println("Test 8: " + test8);
}
}
> Task :devkit-theme:TestColorConverter.main()
Test 1: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 2: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 3: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 4: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 5: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 6: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 7: Color[1.0, 0.13333334, 0.8666667, 1.0]
Test 8: Color[1.0, 0.13333334, 0.8666667, 1.0]
Edit: Added a test case to make sure the converter works on all intended inputs.