Monday, September 10, 2012

Change two colors by percentage

In Android, it is not good when most of the views has alpha value. Therefore, it is better by changing color of the view.

Below is the color interplation between a and b with given proportion:

private float interpolate(float a, float b, float proportion) {
    return (a + ((b - a) * proportion));

/** Returns an interpoloated color, between a and b */
private int interpolateColor(int a, int b, float proportion) {
    float[] hsva = new float[3];
    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);

Thanks to:

No comments:

Post a Comment