android color between two colors, based on percentage? android color between two colors, based on percentage? java java

android color between two colors, based on percentage?


You can try using ArgbEvaluator class from android API: http://developer.android.com/reference/android/animation/ArgbEvaluator.html :

new ArgbEvaluator().evaluate(0.75, 0x00ff00, 0xff0000);

Note that there is a bug ( http://code.google.com/p/android/issues/detail?id=36158 ) in alpha channel calculation so you should use values without alpha value.


My $0.02, I found this answer and coded up the proper solution. (Thanks to Alnitak for the HSV tip!)

For Copy+Paste:

  private float interpolate(float a, float b, float proportion) {    return (a + ((b - a) * proportion));  }  /** Returns an interpoloated color, between <code>a</code> and <code>b</code> */  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);  }


As an updated solution, you can use ColorUtils#blendARGB from the Android support or AndroidX APIs:

val startColor = ContextCompat.getColor(context, R.color.white)val endColor = ContextCompat.getColor(context, R.color.yellow)ColorUtils.blendARGB(startColor, endColor, 0.75)