I am trying to create a colour palette of Material Design that changing the lightness / luminosity by percentage with arbitrary color hex. When it comes to the implementation, I
Original problem
Your code has error working with color format.
Replace last line of hslToColor()
like shown below and you'll make it run without errors:
public static String hslToColor(int alpha, float hue, float saturation, float lightness) {
...
// !!! ERROR WAS ON THE LAST LINE:
return String.format("#%08x", resultColorInt).toUpperCase();
}
I've tested it - it works - because it makes 2 additional things:
1) Formats value to have 8 digits
2) Adds "#" prefix
Possible SECOND problem in your code
The alpha value may have values from 0 (transparent) to 255 (opaque). If you want to have opaque image you should pass 255 (0xFF).
Now you pass 1
and I think it's an error - because it's almost transparent.
So to have opaque color replace line
String baseColor = hslToColor(1 ,baseColorHSL[0] , baseColorHSL[1] , (float)0.5);
with
String baseColor = hslToColor(0xFF ,baseColorHSL[0] , baseColorHSL[1] , (float)0.5);
Annex
If one needs to get a set of colors - a bit of creativity should be applied.
To create a tint palette you have to change in a loop a) saturation or b) lightness or c) both of them.
Here is an implementation example that returns palette based on lightness change from 0.4 to 0.6 (non inclusive) in 10 steps.
"Experimental" means that you should find values for yourself.
public static ArrayList<String> returnMaterialDesignColorSet(String baseColorHex, int colorCount) {
ArrayList<String> resultList = new ArrayList<String>();
float [] baseColorHSL = colorToHsl(baseColorHex);
float lght=0.4;// initial lightness value (experimental)
float lStep=(0.6 - lght) / colorCount; // step to go up to 0.6 lightness (experimental)
for (int i = 0; i < colorCount; i++) {
String baseColor = hslToColor(1 ,baseColorHSL[0] , baseColorHSL[1] , lght);
resultList.add(baseColor);
lght += lStep;
}
return resultList;
}
The problem is in the following lines:
int resultColorInt = Color.HSVToColor(alpha, new float[] { h, s, v });
return Integer.toHexString(resultColorInt).toUpperCase();
When alpha value is less than 16 (0xF0), it will occupy only one char in the string:
// 1-char alpha
int resultColorInt = Color.HSVToColor(1, new float[]{340, 0.7f, 0.5f});
String result = Integer.toHexString(resultColorInt).toUpperCase();
// result == "1802644" - 7 chars, which is invalid color format
You need to compensate 1-char or 0-char alphas (in range 0-15) by appending 0 at the beginning of the resulting string:
// not the best code, but works
while (result.length() < 8) {
result = "0" + result;
}
// don't forget # to make it a legal color
result = "#" + result;
return result;
However, the best thing would be to avoid strings altogether. Use ints instead - they contain the same data with better performance. For your convenience, in debugger you can change ints to be displayed in HEX, instead of DEC (in Android Studio: right click in Variables view, View as -> HEX).