android color between two colors, based on percentage?

后端 未结 9 1818
悲哀的现实
悲哀的现实 2020-11-29 23:17

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         


        
9条回答
  •  执笔经年
    2020-11-29 23:50

    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 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);
      }
    

提交回复
热议问题