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:

[ROM] Samsung S7 Stock Firmware BRI (Taiwan台灣)

Latest ROM: G930FXXU1DQEU Download: https://kfhost.net/tpl/firmwares.php?record=B7E6DE774E3811E7963AFA163EE8F90B Reference: http://...