@Test public void testInterpolateColor() { assertEquals(RED, Gradient.interpolateColor(RED, RED, 0.5f)); assertEquals(BLUE, Gradient.interpolateColor(BLUE, BLUE, 0.5f)); assertEquals(GREEN, Gradient.interpolateColor(GREEN, GREEN, 0.5f)); int result = Gradient.interpolateColor(RED, BLUE, 0); assertEquals(RED, result); result = Gradient.interpolateColor(RED, BLUE, 1); assertEquals(BLUE, result); assertEquals( Gradient.interpolateColor(BLUE, RED, 0.5f), Gradient.interpolateColor(RED, BLUE, 0.5f)); assertEquals( Gradient.interpolateColor(BLUE, RED, 0.8f), Gradient.interpolateColor(RED, BLUE, 0.2f)); assertEquals( Gradient.interpolateColor(BLUE, RED, 0.2f), Gradient.interpolateColor(RED, BLUE, 0.8f)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { assertEquals(-65434, Gradient.interpolateColor(RED, BLUE, 0.2f)); assertEquals(Color.MAGENTA, Gradient.interpolateColor(RED, BLUE, 0.5f)); assertEquals(-10092289, Gradient.interpolateColor(RED, BLUE, 0.8f)); assertEquals(Color.YELLOW, Gradient.interpolateColor(RED, GREEN, 0.5f)); assertEquals(Color.CYAN, Gradient.interpolateColor(BLUE, GREEN, 0.5f)); } }
static int interpolateColor(int color1, int color2, float ratio) { int alpha = (int) ((Color.alpha(color2) - Color.alpha(color1)) * ratio + Color.alpha(color1)); float[] hsv1 = new float[3]; Color.RGBToHSV(Color.red(color1), Color.green(color1), Color.blue(color1), hsv1); float[] hsv2 = new float[3]; Color.RGBToHSV(Color.red(color2), Color.green(color2), Color.blue(color2), hsv2); if (hsv1[0] - hsv2[0] > 180) { hsv2[0] += 360; } else if (hsv2[0] - hsv1[0] > 180) { hsv1[0] += 360; } float[] result = new float[3]; for (int i = 0; i < 3; i++) { result[i] = (hsv2[i] - hsv1[i]) * (ratio) + hsv1[i]; } return Color.HSVToColor(alpha, result); }
Gradient { static int interpolateColor(int color1, int color2, float ratio) { int alpha = (int) ((Color.alpha(color2) - Color.alpha(color1)) * ratio + Color.alpha(color1)); float[] hsv1 = new float[3]; Color.RGBToHSV(Color.red(color1), Color.green(color1), Color.blue(color1), hsv1); float[] hsv2 = new float[3]; Color.RGBToHSV(Color.red(color2), Color.green(color2), Color.blue(color2), hsv2); if (hsv1[0] - hsv2[0] > 180) { hsv2[0] += 360; } else if (hsv2[0] - hsv1[0] > 180) { hsv1[0] += 360; } float[] result = new float[3]; for (int i = 0; i < 3; i++) { result[i] = (hsv2[i] - hsv1[i]) * (ratio) + hsv1[i]; } return Color.HSVToColor(alpha, result); } }
Gradient { static int interpolateColor(int color1, int color2, float ratio) { int alpha = (int) ((Color.alpha(color2) - Color.alpha(color1)) * ratio + Color.alpha(color1)); float[] hsv1 = new float[3]; Color.RGBToHSV(Color.red(color1), Color.green(color1), Color.blue(color1), hsv1); float[] hsv2 = new float[3]; Color.RGBToHSV(Color.red(color2), Color.green(color2), Color.blue(color2), hsv2); if (hsv1[0] - hsv2[0] > 180) { hsv2[0] += 360; } else if (hsv2[0] - hsv1[0] > 180) { hsv1[0] += 360; } float[] result = new float[3]; for (int i = 0; i < 3; i++) { result[i] = (hsv2[i] - hsv1[i]) * (ratio) + hsv1[i]; } return Color.HSVToColor(alpha, result); } Gradient(int[] colors, float[] startPoints);  Gradient(int[] colors, float[] startPoints, int colorMapSize); }
Gradient { static int interpolateColor(int color1, int color2, float ratio) { int alpha = (int) ((Color.alpha(color2) - Color.alpha(color1)) * ratio + Color.alpha(color1)); float[] hsv1 = new float[3]; Color.RGBToHSV(Color.red(color1), Color.green(color1), Color.blue(color1), hsv1); float[] hsv2 = new float[3]; Color.RGBToHSV(Color.red(color2), Color.green(color2), Color.blue(color2), hsv2); if (hsv1[0] - hsv2[0] > 180) { hsv2[0] += 360; } else if (hsv2[0] - hsv1[0] > 180) { hsv1[0] += 360; } float[] result = new float[3]; for (int i = 0; i < 3; i++) { result[i] = (hsv2[i] - hsv1[i]) * (ratio) + hsv1[i]; } return Color.HSVToColor(alpha, result); } Gradient(int[] colors, float[] startPoints);  Gradient(int[] colors, float[] startPoints, int colorMapSize);  }
Gradient { static int interpolateColor(int color1, int color2, float ratio) { int alpha = (int) ((Color.alpha(color2) - Color.alpha(color1)) * ratio + Color.alpha(color1)); float[] hsv1 = new float[3]; Color.RGBToHSV(Color.red(color1), Color.green(color1), Color.blue(color1), hsv1); float[] hsv2 = new float[3]; Color.RGBToHSV(Color.red(color2), Color.green(color2), Color.blue(color2), hsv2); if (hsv1[0] - hsv2[0] > 180) { hsv2[0] += 360; } else if (hsv2[0] - hsv1[0] > 180) { hsv1[0] += 360; } float[] result = new float[3]; for (int i = 0; i < 3; i++) { result[i] = (hsv2[i] - hsv1[i]) * (ratio) + hsv1[i]; } return Color.HSVToColor(alpha, result); } Gradient(int[] colors, float[] startPoints);  Gradient(int[] colors, float[] startPoints, int colorMapSize);  final int mColorMapSize; public int[] mColors; public float[] mStartPoints; }