I would like to calculate the color depending on a percentage value:
float percentage = x/total;
int color;
if (percentage >= 0.95) {
color = Color.GREE
Here are 2 ways to interpolate:
private static float interpolate(final float a, final float b, final float proportion) {
return a + (b - a) * proportion;
}
/** Returns an interpoloated color, between <code>a</code> and <code>b</code> */
public static int interpolateColorHsv(final int a, final int b, final float proportion) {
final float[] hsva = new float[3];
final float[] hsvb = new float[3];
Color.colorToHSV(a, hsva);
Color.colorToHSV(b, hsvb);
for (int i = 0; i < 3; ++i) {
hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
}
return Color.HSVToColor(hsvb);
}
public static int interpolateRGB(final int colorA, final int colorB, final float bAmount) {
final float aAmount = 1.0f - bAmount;
final int red = (int) (Color.red(colorA) * aAmount + Color.red(colorB) * bAmount);
final int green = (int) (Color.green(colorA) * aAmount + Color.green(colorB) * bAmount);
final int blue = (int) (Color.blue(colorA) * aAmount + Color.blue(colorB) * bAmount);
return Color.rgb(red, green, blue);
}
Here is a pseudocode function that interpolates linearly between 2 colors (staying in RGB space
). I'm using a class called Color here instead of ints for clarity.
bAmount is between 0 and 1 (for interpolation)
Color interpolate(Color colorA, Color colorB, float bAmount) {
Color colorOut;
float aAmount = 1.0 - bAmount;
colorOut.r = colorA.r * aAmount + colorB.r * bAmount;
colorOut.g = colorA.g * aAmount + colorB.g * bAmount;
colorOut.b = colorA.b * aAmount + colorB.b * bAmount;
return colorOut;
}
Ok, after 2 hours of converting to yuv, hsv, etc pp... I give up. I now do it just like this:
public class ColorUtils {
private static int FIRST_COLOR = Color.GREEN;
private static int SECOND_COLOR = Color.YELLOW;
private static int THIRD_COLOR = Color.RED;
public static int getColor(float p) {
int c0;
int c1;
if (p <= 0.5f) {
p *= 2;
c0 = FIRST_COLOR;
c1 = SECOND_COLOR;
} else {
p = (p - 0.5f) * 2;
c0 = SECOND_COLOR;
c1 = THIRD_COLOR;
}
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
private static int ave(int src, int dst, float p) {
return src + java.lang.Math.round(p * (dst - src));
}
}
By explicity using yellow as the middle color, the generated colors are brighter :-)
Anyway.. if someone has a good other solution, I would appreciate it.