Is there a way to compute a color along a gradient?
For example, I have a label that I'm using as a sort of status bar with gradient background that has green (#6bba70) at one end and red (#a90329) at the other. What I'd like to do is to figure out what color is at a certain point along the gradient. For example, if the process is 27% complete, I'd like to get the color that is "27% along the gradient". Like this:
I thought maybe that I could find the numerical shift and it would give me the correct color. The code I wrote (that doesn't work):
public static String getHTML(Date d, int shift) throws SQLException, ClassNotFoundException {
String returnValue = "";
returnValue += " <style type=\"text/css\">\n"
+ " \n"
+ " .status{\n"
+ " filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='" + getColor(77) + "',GradientType=1 );\n"
+ " width: " + 500 * .77 + "px;\n"
+ " height: 5px;\n"
+ " border: thin black solid;\n"
+ " }\n"
+ " .status2{\n"
+ " filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='" + getColor(100) + "',GradientType=1 );\n"
+ " width: " + 500 + "px;\n"
+ " height: 5px;\n"
+ " border: thin black solid;\n"
+ " }\n"
+ " .status3{\n"
+ " filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='" + getColor(33) + "',GradientType=1 );\n"
+ " width: " + 500 * .33 + "px;\n"
+ " height: 5px;\n"
+ " border: thin black solid;\n"
+ " }\n"
+ " .status4{\n"
+ " filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='" + getColor(69) + "',GradientType=1 );\n"
+ " width: " + 500 * .69 + "px;\n"
+ " height: 5px;\n"
+ " border: thin black solid;\n"
+ " }\n"
+ " </style>"
+ " <body>\n"
+ " 77% <label class=\"status\"></label><br><br>\n"
+ " 100% <label class=\"status2\"></label><br><br>\n"
+ " 33% <label class=\"status3\"></label><br><br>\n"
+ " 69% <label class=\"status4\"></label><br><br>\n"
+ " </body>";
return returnValue;
}
private static String getColor(double percentage) {
String colorValue = "";
int[] rgb1 = getRGB("a90329");
int[] rgb2 = getRGB("6bba70");
if (percentage < 100) {
int range0 = (rgb1[0] > rgb2[0] ? rgb1[0] : rgb2[0]) - (rgb1[0] < rgb2[0] ? rgb1[0] : rgb2[0]);
int range1 = (rgb1[1] > rgb2[1] ? rgb1[1] : rgb2[1]) - (rgb1[1] < rgb2[1] ? rgb1[1] : rgb2[1]);
int range2 = (rgb1[2] > rgb2[2] ? rgb1[2] : rgb2[2]) - (rgb1[2] < rgb2[2] ? rgb1[2] : rgb2[2]);
int r = (int) (rgb2[0] + Math.round(range0 * percentage / 100));
int g = (int) (rgb2[1] + Math.round(range1 * percentage / 100));
int b = (int) (rgb2[2] + Math.round(range2 * percentage / 100));
System.out.println(Integer.toString(r) + "," + Integer.toString(g) + "," + Integer.toString(b));
colorValue = String.format("#%02x%02x%02x", r, g, b);
} else {
colorValue = "#a90329";
}
return colorValue;
}
public static int[] getRGB(final String rgb) {
final int[] ret = new int[3];
for (int i = 0; i < 3; i++) {
ret[i] = Integer.parseInt(rgb.substring(i * 2, i * 2 + 2), 16);
}
return ret;
}
That produced:
Nowhere near what I wanted. Anybody have an idea of how I can achieve what I wanted?