How do I interpolate four colors?

I need to interpolate four colors in my microphone renderer.

  • 1 color is white if the user is silent.

  • 2 colors if the user speaks softly.

  • 3 color is yellow if the user speaks normally.

  • 4 colors if the user speaks loudly.

Im create a Paint void to change the color, but I don't know how to interpolate these four colors, I only know how to do it with two colors. What do I need to do?

 // used to take colors mix according to proportion
  private int interpolateColor(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);
  }

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

      

+3


source to share


1 answer


Assuming you've already defined the user's current "volume" in a float, where 0 is completely disabled and 1 is the maximum, then I would do something like this:

float v = userVolume();

if (v < SOFT_CUTOFF)
    color = interpolateColor(SILENT_COLOR, SOFT_COLOR, v/SOFT_CUTOFF);
else if (v < NORMAL_CUTOFF)
    color = interpolateColor(SOFT_COLOR, NORMAL_COLOR, (v-SOFT_CUTOFF)/(NORMAL_CUTOFF-SOFT_CUTOFF));
else
    color = interpolateColor(NORMAL_COLOR, LOUD_COLOR, (v-NORMAL_CUTOFF)/(1-NORMAL_CUTOFF));

      



SOFT_CUTOFF and NORMAL_CUTOFF should be set to values ​​between 0 and 1, which determine in what ratio they should be full color.

+1


source







All Articles