Skip to content

Instantly share code, notes, and snippets.

@PPartisan
Created September 30, 2015 08:29
Show Gist options
  • Save PPartisan/b90a3ad1ee6d5b4b3492 to your computer and use it in GitHub Desktop.
Save PPartisan/b90a3ad1ee6d5b4b3492 to your computer and use it in GitHub Desktop.
Utility methods for creating fade animations between two colours
import android.graphics.Color;
public final class PaintUtils {
private PaintUtils() { throw new AssertionError(); }
public static int createInterimColor(int colorFrom, int colorTo, float percentage) {
int[] colorFromArray, colorToArray;
colorFromArray = new int[] {
Color.red(colorFrom),
Color.green(colorFrom),
Color.blue(colorFrom)
};
colorToArray = new int[] {
Color.red(colorTo),
Color.green(colorTo),
Color.blue(colorTo)
};
return createInterimColor(colorFromArray, colorToArray, percentage);
}
public static int createInterimColor(int[] colorFromInRgb, int[] colorToInRgb, float percentage) {
int r, g, b;
int minValue;
float adjustedPercentage;
minValue = Math.min(colorFromInRgb[0], colorToInRgb[0]);
adjustedPercentage = (colorFromInRgb[0] >= colorToInRgb[0]) ? (1F - percentage) : percentage;
r = (int) (minValue + ((Math.abs(colorFromInRgb[0] - colorToInRgb[0])) * adjustedPercentage));
minValue = Math.min(colorFromInRgb[1], colorToInRgb[1]);
adjustedPercentage = (colorFromInRgb[1] >= colorToInRgb[1]) ? (1F - percentage) : percentage;
g = (int) (minValue + ((Math.abs(colorFromInRgb[1] - colorToInRgb[1])) * adjustedPercentage));
minValue = Math.min(colorFromInRgb[2], colorToInRgb[2]);
adjustedPercentage = (colorFromInRgb[2] >= colorToInRgb[2]) ? (1F - percentage) : percentage;
b = (int) (minValue + ((Math.abs(colorFromInRgb[2] - colorToInRgb[2])) * adjustedPercentage));
return Color.rgb(r, g, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment